#include<stdio.h>
#include<conio.h>

struct llist
{
   int info;
   struct llist *next;
};

typedef struct llist linked_list;
linked_list *start=NULL,*new1;

void main()
{
   int menu;

   void ll_insert()
   {
	char ch;
	do{
	   new1=create_node();
	   if(start==NULL)
		start=new1;
	   else
		addnode();
	   fflush(stdin);
	   printf("Do you want to add one more row(y/n):");
	   scanf("%c",&ch);
	}while(ch=='y' || ch=='Y');

   }

   linked_list *create_node()
   {
	linked_list *new1;
	new1=(linked_list*)malloc(sizeof(linked_list));
	printf("\nEnter the info:");
	scanf("%d",&new1->info);
	new1->next=NULL;
	return new1;
   }   

   void addnode()
   {
	linked_list *prev,*ptr;
	if(new->info < start->info)
	{
	   new->next=start;
	   start=new;
	   return;
	}
	for(prev=start,ptr=start->next;ptr;prev=ptr,ptr=ptr->next)
	{
	   if(new->info < ptr->info)
	   {
		prev->next=new;
		new->next=ptr;
		return;
	   }
	}
	if(ptr==NULL)
	{
	   prev->next=new;
	}
   }

   void ll_delete()
   {
	linked_list *prev,*ptr,*temp;
	int info;
	printf("\nEnter info to be deleted:");
	scanf("%d",&info);
	if(start->info==info)
	{
	   temp=start;
	   start=start->next;
	   free(temp);
	   return;
	}
	for(prev=start,ptr=start->next;ptr;prev=ptr,ptr=ptr->next)
	{
	   if(ptr->info==info)
	   {
		prev->next=ptr->next;
		free(ptr);
		break;
	   }  
	}
	if(ptr==NULL)
	{
	   printf("\nElement not found!!!!");
	   getch();
	}
   }

   void ll_traverse()
   {
	linked_list *ptr;
	if(start==NULL)
	{
	   printf("\nLinked List is empty!!!!");
	   return;
	}
	for(ptr=start;ptr;ptr=ptr->next)
	{
	   printf(" %d "ptr->info);
	}
	getch();
   }

   do{
	clrscr();
	printf("\n1.Insert\n2.Delete\n3.Traverse\n4.Exit\nEnter your:choice:");      
	scanf("%d",&menu);

	switch(menu)
	{
	   case 1:
		ll_insert();
		break;
	   case 2:
		ll_delete();
		break;
	   case 3:
		ll_traverse();
		break;
	   case 4:
		return;
	}
   }while(menu!=4);
}

I jst did a very simple linked list program...But gettin declaration syntax error in line no:21:(....I chkd out all braces n al bt itz nt working:'(....Plz help me.....
Thnx,
Ann

Edited 7 Years Ago by Nick Evan: Added code-tags

This line is wrong: new1=create_node(); The function create_node() is not yet defined at this point in your code. The functions linked_list *create_node() and ll_insert() need to switch positions in your code.

ps.
Here's the rulebook. Read it.

Edited 7 Years Ago by Nick Evan: n/a

please use tag code and explain what you are trying to accomplish

#include<stdio.h>
#include<conio.h>

struct llist
{
int info;
struct llist *next;
};

typedef struct llist linked_list;
linked_list *start=NULL,*new1;

void main()
{
int menu;

void ll_insert()
{
char ch;
do{
new1=create_node();
if(start==NULL)
start=new1;
else
addnode();
fflush(stdin);
printf("Do you want to add one more row(y/n):");
scanf("%c",&ch);
}while(ch=='y' || ch=='Y');

}

linked_list *create_node()
{
linked_list *new1;
new1=(linked_list*)malloc(sizeof(linked_list));
printf("\nEnter the info:");
scanf("%d",&new1->info);
new1->next=NULL;
return new1;
}

void addnode()
{
linked_list *prev,*ptr;
if(new->info < start->info)
{
new->next=start;
start=new;
return;
}
for(prev=start,ptr=start->next;ptr;prev=ptr,ptr=ptr->next)
{
if(new->info < ptr->info)
{
prev->next=new;
new->next=ptr;
return;
}
}
if(ptr==NULL)
{
prev->next=new;
}
}

void ll_delete()
{
linked_list *prev,*ptr,*temp;
int info;
printf("\nEnter info to be deleted:");
scanf("%d",&info);
if(start->info==info)
{
temp=start;
start=start->next;
free(temp);
return;
}
for(prev=start,ptr=start->next;ptr;prev=ptr,ptr=ptr->next)
{
if(ptr->info==info)
{
prev->next=ptr->next;
free(ptr);
break;
}
}
if(ptr==NULL)
{
printf("\nElement not found!!!!");
getch();
}
}

void ll_traverse()
{
linked_list *ptr;
if(start==NULL)
{
printf("\nLinked List is empty!!!!");
return;
}
for(ptr=start;ptr;ptr=ptr->next)
{
printf(" %d "ptr->info);
}
getch();
}

do{
clrscr();
printf("\n1.Insert\n2.Delete\n3.Traverse\n4.Exit\nEnter your:choice:");
scanf("%d",&menu);

switch(menu)
{
case 1:
ll_insert();
break;
case 2:
ll_delete();
break;
case 3:
ll_traverse();
break;
case 4:
return;
}
}while(menu!=4);
}
Comments
don't do this

please use tag code and explain what you are trying to accomplish

Next time, please just click the 'flag bad post' link if you see no code-tags, and someone will fix it. Now we have the same code posted twice, only yours has no indention.

also dont use void main
accoring to salem void Main(ers) are doomed TO ka-BOOOM ;)
use soemthing like this

int main(){
//soem codes here
//..................
return 0;
}

Next time, please just click the 'flag bad post' link if you see no code-tags, and someone will fix it. Now we have the same code posted twice, only yours has no indention.

I thought it was for spammmers scheduled for delete

This is quite messy; you might have start looking at some text on good programming practice. The few things which I could pick up from a quick glance are as follow

1. Main should return an int not void – MUST
2. Do not declare function within main. Would still work not a good programming practice. You just limiting the scope of those function visibility to main function. In long run of programming C you would seem what I’m saying.
3. new1 is a variable of type linked_list but you haven’t declared it anywhere
4. And you use new1 variable at few places, but really it cant be accessed anywhere apart from create_node function. You see why?

-ssharish

This article has been dead for over six months. Start a new discussion instead.