why isn't my code working?link list - c

q2 to demonstrate linked list operations:insertion,display & deletion//
the compiler code blocks is telling the two error at lines
73 and 83 which i have marked expected ; before'{' token
and expected declaration or statement at the end of the input*/
but it is also telling that in function create:
these two error are there !how is that possible when it is referring them to be in the main()
#include <stdio.h>
#include <stdlib.h>
struct list
{
int a;
char name[20];
int roll;
struct list *next;
};
struct list *create(struct list *ptr)
{
int v,n;
printf("\nenter the value of the inputs");
scanf("%d",&n);
struct list *temp;
printf("\ndo u want to continue(y/n)");
scanf("%d",&v);
while(1)
{
if(v=='y')
{
ptr=(struct list*)malloc(sizeof(struct list));
printf("\nenter the roll number of the student");
scanf("%d",&ptr->roll);
printf("\nenter the name of the student");
gets(ptr->name);
printf("\nenter the marks of the student");
scanf("%d",&ptr->a);
ptr->next=NULL;
}
else
if(v=='n')
{
break;
}
return(ptr);
}
}
void display(struct list *ptr)
{
struct list *temp;
temp=ptr;
while(temp!=NULL)
{
printf("\nthe roll number of the student is%d",temp->roll);
printf("\nthe name of the student is%d",temp->name);
printf("\nthe marks of the student is%d",temp->a);
temp=temp->next;
}
}
void del(struct list *ptr,int c)
{
struct list *temp;
struct list *gtemp;
gtemp=temp=ptr;
while(temp->roll!=c)
{
gtemp=temp;
temp=temp->next;
}
gtemp->next=temp->next;
free(temp);
}
main()
{ //73
struct list *ptr;
int c;
ptr=NULL;
ptr=create(ptr);
display(ptr);
printf("\nenter the value of roll number");
scanf("%d",&c);
del(ptr,c);
display(ptr);
}//83

regarding the first expected ; before'{' token error, instead of
main()
you should use the full signature
int main(int argc, char **argv)
for the second error, you should first indent your code properly.

check the {} combinations properly. you have mostly written display() function difination inside create{} function. so add the {} pairs properly. add keep code well indented
add one closing '}' before display() function defination.
and
printf("\nthe name of the student is%d",temp->name);
it should be %s for strings.

Related

How to fix the problem of file.exe stop running after one insertion in linked list

#include<stdio.h>
#include<stdlib.h>
struct node{
int id;
char name[50];
float point;
char grade;
struct node *next;
};
struct node *head;
void DataEntry(struct node *);
void GetData(struct node *);
void DataDisp();
void Insert()
{
struct node *p;
p=(struct node*)malloc(sizeof(struct node));
DataEntry(p);
if(head==NULL)
{
head=p;
}
else
{
struct node *q;
q=head;
while(q!=NULL)
{
q=q->next;
}
q->next=p;
p->next=NULL;
}
}
void DataEntry(struct node *p)
{
printf("Enter value of Id employee\n");
scanf("%d",&p->id);
printf("Enter name of the person\n");
scanf("%s",p->name);
printf("Enter points got by Employee\n");
scanf("%f",&p->point);
printf("Enter grade of the person\n");
fflush(stdin);
scanf("%c",&p->grade);
}
void GetData(struct node *q)
{
printf("The data entered by you is as follows\n");
printf("Id is %d\n",q->id);
printf("Name is %s\n",q->name);
printf("Point is %f\n",q->point);
printf("Grade is %c\n",q->grade);
}
void DataDisp()
{
struct node *z;
z=head;
if(z==NULL)
{
printf("List is Empty\n");
return;
}
while(z!=NULL)
{
GetData(z);
z=z->next;
}
}
int main()
{
Insert();
DataDisp();
}
I have implemented a singly linked list, I insert data in a list using Insert() function and displaying the data using DataDisp() function in DataDisp() function I have used GetData() function which is accessing data from linked list nodes. The problem here is I can do the first insertion and it displays data also using DataDisp() but after that, there is error file.exe stop running. I think there is a segmentation fault; I tried my best to solve the problem but could not run it successfully. Please help.
ONE MORE THING IS TRY TO PREVENT USE OF "FFLUSH" KEYWORD.THIS IS NOT A PORTABLE WAY OF CLEANING THE INPUT BUFFER, AND MAY NOT WORK WITH ALL COMPILER.

error: used struct type value where scalar is required for(;*ptr;ptr++)

Can you please explain the reason behind this error.
Error code:
used struct type value where scalar is required for(;*ptr;ptr++)"for the below code?
Are there any reason why we aren't allowed struct variables in for loop?
#include<stdio.h>
#include<stdlib.h>
struct student
{
char name[20]; // Given
int math; // Marks in math (Given)
int phy; // Marks in Physics (Given)
int che; // Marks in Chemistry (Given)
int total; // Total marks (To be filled)
int rank; // Rank of student (To be filled)
};
static int count=1;
void enter_data(struct student* arr,int num);
void print_data(struct student* arr,int num);
int main()
{
int num=1;
char ch;
printf("Enter the number of student records you want to input?..\n");
scanf("%d",&num);
struct student *arr=(struct student*)malloc(num*sizeof(struct student));
printf("Do you want to enter the student record...(Y/N)\n");
scanf(" %c",&ch);
if(ch=='y'||ch=='Y')
{
enter_data(arr,num);
printf("The created record is .....\n");
print_data(arr,num);
}
else
return 0;
}
void enter_data(struct student* arr,int num)
{
int i;
struct student* ptr=arr;
for(;count<=num;ptr++,count++)
{
printf("Enter the name of the candidate...\n");
scanf("%s",ptr->name);
printf("Enter the marks scored in maths,phy,che....\n");
scanf("%d%d%d",&ptr->math,&ptr->phy,&ptr->che);
((ptr->total)=(ptr->math)+(ptr->phy)+(ptr->che));
}
}
void print_data(struct student* arr,int num)
{
int i;
struct student* ptr=arr;
for(;*ptr!=NULL;ptr++)//error here
{
printf("Name of the candidate...%s\n",ptr->name);
printf("Enter the marks scored in maths...%d\t physics... %d\tche... %d\n total=%d\n",ptr->math,ptr->phy,ptr->che,ptr->total);
}
}
Increment the pointer that reference's the values at particular address not the value at the address of that pointer. *ptr!=NULL means you are comparing struct student to NULL
for(;ptr!=NULL;ptr++)//error fixed here
{
printf("Name of the candidate...%s\n",ptr->name);
printf("Enter the marks scored in maths...%d\t physics... %d\tche... %d\n total=%d\n",ptr->math,ptr->phy,ptr->che,ptr->total);
}

Single linkedlist creation in C

#include<stdio.h>
#include<stdlib.h>
struct list
{
int a;
int b;
int c;
struct list *next;
};
struct list* addlistele(struct list*,int,int,int);
/* List c element */
void listc()
{
printf(" soon...\n");
}
void printlist(list)
{
struct list* temp;
temp=list;
while(temp!=NULL)
{
printf("a:%d,b;%d,c:%d\n",temp->a,temp->b,temp->c);
temp=temp->next;
}
}
/* List element */
struct list* addlistele(struct list* listadd,int b,int d,int m)
{
int i;
struct list* temp;
struct list* addelement=(struct list*)malloc(sizeof(struct list));
addelement->a=b;
addelement->b=d;
addelement->c=m;
addelement->next=NULL;
if(listadd==NULL)
{
printf("entering");
return addelement;
}
else
{
temp=listadd;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=addelement;
}
return listadd;
}
int main()
{
int ch,i,a,b,c;
struct list *element,*list;
element=(struct list*)malloc(sizeof(struct list));
printf("Choose any one of the option \n");
printf("1.List All \n 2.List c \n");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("Enter values \n");
for(i=0;i<2;i++)
{
scanf("%d %d %d \n",&a,&b,&c);
list=addlistele(element,a,b,c);
}
printlist(list);
break;
case 2:listc(); break;
default:break;
}
}
Hi all, i have written the code like the above one.In that when i gave inputs
> Choose any one of the option
> 1.List All
> 2.List c 1 Enter values 2 3 4 1 2 3
The output is
a:0,b;0,c:0
a:2,b;3,c:4
a:1,b;2,c:3
and also it is not adding the element first i mean it is not entering into this loop
if(listadd==NULL)
{
printf("entering");
return addelement;
}
how to make the head element to be NULL and also i don't know how 0 is coming first.Could anybody can tell me what will be the issue?
In main() you create head element and pass it to the addlistele() function.
element=(struct list*)malloc(sizeof(struct list));
...
list=addlistele(element,a,b,c);
You are seeing this first element which does not have valid values that you expected.
Solution would be you malloc() the element in the function rather than in main() and do not allocate element in main().
replace
struct list *element,*list;
element=(struct list*)malloc(sizeof(struct list));
with
struct list *list=NULL;
then
replace
list=addlistele(element,a,b,c);
with
list=addlistele(list,a,b,c);
Also
replace
void printlist(list)
with
void printlist(struct list *list)
and
replace scanf("%d %d %d \n",&a,&b,&c); with scanf("%d %d %d",&a,&b,&c);

Why my doubly linked list's C implementation creating duplicate values?

I coded for doubly linked list implementation in C. In that, after making insertion of values, i am getting duplication of values. i.e. the last value given by me duplicated in all list items.
My code is as follows
header.h
#include<stdio.h>
#include<stdlib.h>
typedef struct doubly_list
{
int id;
char *name;
struct doubly_list *next;
struct doubly_list *prev;
}node;
void insertfirst(node **,int ,char *);
void insertlast(node **,int ,char *);
doubly_list_insert.c
#include"header.h"
void insertfirst(node **head,int id,char *name)
{
node *tmp=(node *)malloc(sizeof(node));
if(NULL == tmp)
{
printf("\nMemory allocation failed\n");
exit(1);
}
tmp->id=id;
tmp->name=name;
tmp->prev=NULL;
if(*head== NULL)
{
tmp->next=NULL;
*head=tmp;
}
else
{
tmp->next=*head;
(*head)->prev=tmp;
*head=tmp;
}
}
void insertlast(node **head,int id,char *name)
{
if(*head==NULL)
{
insertfirst(head,id,name);
return;
}
node *last=*head;
node *tmp=(node *)malloc(sizeof(node));
if(NULL == tmp)
{
printf("\nMemory allocation failed\n");
exit(1);
}
tmp->id=id;
tmp->name=name;
tmp->next=NULL;
while(last->next!=NULL)
{
last=last->next;
}
last->next=tmp;
tmp->prev=last;
}
doubly_list_traverse.c
#include"header.h"
void traverse(node *head)
{
node *tmp=head;
if(head==NULL)
{
printf("\nList is empty\n");
exit(1);
}
while(tmp!=NULL)
{
printf("%d --> %s\n",tmp->id,tmp->name);
tmp=tmp->next;
}
}
And, here comes the main file,
main.c
#include"header.h"
int main()
{
int choice;
int id;
char name[15];
node *root=NULL;
system("clear");
while(1)
{
printf("\n1.Insert First\n");
printf("\n2.Insert Last\n");
printf("\n3.Traverse\n");
printf("\n4.Exit\n");
printf("\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter the employee id : ");
scanf("%d",&id);
printf("\nEnter the employee name : ");
scanf("%s",name);
insertfirst(&root,id,name);
break;
case 2:
printf("\nEnter the employee id : ");
scanf("%d",&id);
printf("\nEnter the employee name : ");
scanf("%s",name);
insertlast(&root,id,name);
break;
case 3:
traverse(root);
break;
case 4:
return 0;
break;
default:
printf("\nPlease enter valid choices\n");
}
}
}
During execution its getting input from me properly,if i insert only one data either first or last.
But if i insert a second one, there comes the problem.
In my case, the id value remains the same. But the 2nd input's name value is duplicated in 1st value.
Why this is happening? Is it anything wrong in passing arguments?
When you create a new node, you set the node name by just copying the pointer to the name. You have to copy the string not the pointer. The strdup function is perfect for this:
tmp->name=strdup(name);
Remember to free the name when you free the nodes.
Edit
What happens when you call insertfirst the first time, is that the name field of the first node points to the name array in main. When you fetch the name for the second node, the contents of the array in main is updated with the new name, and since the pointer in the first node points to that array it seems like the name is duplicated.

can my code for reversing link list be further enhanced

Here is my program which creates a link list and also reverses it.
#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *list=NULL;
struct node *root=NULL;
static int count=0;
struct node *create_node(int);//function to create node
void travel_list(void);
void create_list(int);
void reverse_list(void);
int main()
{
int i, j, choice;
printf("Enter a number this will be root of tree\n");
scanf("%d", &i);
create_list(i);
printf("Enter 1 to enter more numbers \n 0 to quit\n");
scanf("%d", &choice);
while (choice!=0){
printf("Enter a no for link list\n");
scanf("%d",&i);
// printf("going to create list in while\n");
create_list(i);
travel_list();
printf("Enter 1 to enter more numbers \n 0 to quit\n");
scanf("%d", &choice);
}
printf("reversing list\n");
reverse_list();
travel_list();
}
// end of function main
void create_list (int data)
{
struct node *t1,*t2;
//printf("in function create_list\n");
t1=create_node(data);
t2=list;
if( count!=0)
{
while(t2->next!=NULL)
{
t2=t2->next;
}
t2->next=t1;
count++;
}
else
{
root=t1;
list=t1;
count++;
}
}
struct node *create_node(int data)
{
struct node *temp;
temp = (struct node *)malloc(sizeof(struct node));
temp->data=data;
temp->next=NULL;
// printf("create node temp->data=%d\n",temp->data);
// printf("the adress of node created %p\n",temp);
return temp;
}
void travel_list(void )
{
struct node *t1;
t1=list;
printf("in travel list\n");
while(t1!=NULL)
{
printf("%d-->",t1->data);
t1=t1->next;
}
printf("\n");
}
void reverse_list(void)
{
struct node *t1,*t2,*t3;
t1=list;
t2=list->next;
t3=list->next->next;
int reverse=0;
if(reverse==0)
{
t1->next=NULL;
t2->next=t1;
t1=t2;
t2=t3;
t3=t3->next;
reverse++;
}
while(t3!=NULL)
{
t2->next=t1;
t1=t2;
t2=t3;
list=t1;
travel_list();
t3=t3->next;
}
t2->next=t1;
list=t2;
}
Above is a fully working code.
I want to know if there can be further enhancement to the above code?
Make your indentation and whitespace usage consistent
Use meaningful identifiers rather than t1, t2 and t3
Make the data member a generic type, for example void * rather than int.
Don't use global variables, pass struct node * pointers to your functions.

Resources