I have write the code (program) for the Single Linked List using C Language.
Here is my program...
Expand|Select|Wrap|Line Numbers
- //Program of Single Linked List
- #include<stdio.h>
- #include<conio.h>
- #include<alloc.h>
- void create();
- void insert();
- void display();
- void insert_first();
- void insert_last();
- void count();
- void search();
- void insert_after_position();
- void insert_after_search_element();
- void insert_before_search_element();
- void delete_first();
- void delete_last();
- void delete_search();
- void delete_at_position();
- void delete_after_position();
- void delete_before_position();
- void search_update();
- void update_position();
- void reverse();
- void split_list();
- void merge_list();
- void sorted_insert();
- //Structure Difination
- struct node
- {
- int data;
- struct node *next;
- }*head=NULL,*head1=NULL;
- struct node *temp,*q;
- //Void main started
- void main()
- {
- int ch;
- clrscr();
- do
- {
- printf("\n\n\n");
- //Giving the choices for the linked list operation
- printf("--------------------------------------------------------------\n");
- printf("\t[1].Create List\n");
- printf("\t[2].Insert\n");
- printf("\t[3].Display\n");
- printf("\t[4].\n");
- printf("\t[5].\n");
- printf("\t[6].\n");
- printf("\t[7].\n");
- printf("\t[8].\n");
- printf("\t[9].\n");
- printf("\t[10].\n");
- printf("\t[11].\n");
- printf("\t[12].\n");
- printf("\t[13].\n");
- printf("\t[14].\n");
- printf("\t[15].\n");
- printf("\t[16].\n");
- printf("\t[17].\n");
- printf("\t[18].\n");
- printf("\t[19].\n");
- printf("\t[20].\n");
- printf("\t[21].\n");
- printf("\t[22].\n");
- printf("\t[23].\n");
- printf("----------------------------------------------------------------");
- printf("\n Enter Your Choice :- ");
- scanf("%d",&ch);
- switch(ch)
- {
- case 1:
- create();
- break;
- case 2:
- insert();
- break;
- case 3:
- display();
- break;
- case 4:
- insert_first();
- break;
- case 5:
- insert_last();
- break;
- case 6:
- count();
- break;
- case 7:
- search();
- break;
- case 8:
- insert_after_position();
- break;
- case 9:
- insert_after_search_element();
- break;
- case 10:
- insert_before_search_element();
- break;
- case 11:
- delete_first();
- break;
- case 12:
- delete_last();
- break;
- case 13:
- delete_search();
- break;
- case 14:
- delete_at_position();
- break;
- case 15:
- delete_before_position();
- break;
- case 16:
- delete_after_position();
- break;
- case 17:
- search_update();
- break;
- case 18:
- update_position();
- break;
- case 19:
- reverse();
- break;
- case 20:
- split_list();
- break;
- case 21:
- merge_list();
- break;
- case 22:
- sorted_insert();
- break;
- case 23:
- exit();
- default:
- printf("\n Enter Proper Choice!");
- }
- }while(ch<=23);
- getch();
- }
- //Difining the user defined functions.
- void create()
- {
- int d;
- if(head==NULL)
- {
- printf("\n Enter any number :- ");
- scanf("%d",&d);
- temp=(struct node *)malloc(sizeof(struct node));
- temp->data=d;
- temp->next=NULL;
- head=temp;
- main();
- }
- else
- {
- printf("\n List is already created!");
- return;
- }
- display();
- }
- void insert()
- {
- if(head==NULL)
- {
- printf("\n Linked List is not Created!");
- create();
- }
- else
- {
- temp=(struct node*)malloc(sizeof(struct node));
- printf("\n Enter any number :- ");
- scanf("%d",&(temp->data));
- temp->next=NULL;
- q=head;
- while(q->next!=NULL)
- {
- q=q->next;
- }
- q->next=temp;
- }
- display();
- }
- void display()
- {
- if(head==NULL)
- {
- printf("\n Linked List is not Created!");
- create();
- }
- else
- {
- q=head;
- while(q!=NULL)
- {
- printf("\n%d",q->data);
- q=q->next;
- }
- }
- }
- void insert_first()
- {
- temp=(struct node*)malloc(sizeof(struct node));
- printf("\n Enter the data you want to enter at the first position :- ");
- scanf("%d",&temp->data);
- temp->next=head;
- head=temp;
- display();
- }
- void insert_last()
- {
- temp=(struct node*)malloc(sizeof(struct node));
- printf("\n Enter the data :- ");
- scanf("%d",&temp->data);
- temp->next=NULL;
- q=head;
- while(q->next!=NULL)
- {
- q=q->next;
- }
- q->next=temp;
- display();
- }
- void count()
- {
- int c=0;
- q=head;
- while(q!=NULL)
- {
- c++;
- q=q->next;
- }
- printf("\n The total no of nodes in the list is :- %d",c);
- }
- void search()
- {
- int s,f=0,c=0;
- if(head==NULL)
- {
- printf("\n Linked List is not created!");
- return;
- }
- printf("\n Enter any data to be searched :- ");
- scanf("%d",&s);
- q=head;
- while(q!=NULL)
- {
- c++;
- if(q->data==s)
- {
- f++;
- printf("\n Entered value is found at position %d.",c);
- }
- q=q->next;
- }
- if(f==0)
- {
- printf("\n The no is not Found!");
- }
- void insert_after_position()
- {
- int p,l;
- printf("\n After which position, you want to add the data? :- ");
- scanf("%d",&p);
- q=head;
- for(i=0;i<p-1;i++)
- {
- q=q->next;
- if(q==NULL)
- {
- printf("\n Invalid Position");
- return;
- }
- }
- temp=(struct node*)malloc(sizeof(struct node));
- printf("\n Enter the data :- ");
- scanf("%d",&tmp->data);
- temp->next=q->next;
- q->next=temp;
- display();
- }
- void insert_after_search_element()
- {
- int s;
- if(head==NULL)
- {
- printf("\n Linked List is not Created");
- return;
- }
- printf("\n After which element, you want to add the data? :- ");
- scanf("%d",&s);
- q=head;
- while(q!=NULL)
- {
- if(s==q->data)
- {
- temp=(struct node*)malloc(sizeof(struct node));
- printf("\n Enter the data :- ");
- scanf("%d",&temp->data);
- temp->next=q->next;
- q->next=temp;
- }
- q=q->next;
- }
- display();
- }
- void insert_before_search_element()
- {
- int s;
- struct node *p;
- temp=(struct node*)malloc(sizeof(struct node));
- if(head==NULL)
- {
- printf("\n Linked List is not created!");
- return;
- }
- printf("\n Before which element, you want to add the data? :- ");
- scanf("%d",&s);
- q=head;
- while(q!=NULL)
- {
- if(s==q->data)
- {
- temp=(struct node*)malloc(sizeof(struct node));
- printf("\n Enter the data :- ");
- scanf("%d",&temp->data);
- p->next=temp;
- temp->next=q;
- }
- p=q;
- q=q->next;
- }
- display();
- }
- void delete_first()
- {
- if(head==NULL)
- {
- printf("\n Linked List is not created!");
- return;
- }
- temp=head;
- if(head->next==NULL)
- {
- free(temp);
- head=NULL;
- printf("\n Linked List is empty!");
- return;
- }
- head=head->next;
- printf("\n Deleted value is %d.",q->data);
- free(temp);
- display();
- }
- void delete_last()
- {
- struct node *temp,*q;
- q=head;
- while(q->next!=NULL)
- {
- temp=q;
- q=q->next;
- }
- temp->next=NULL;
- printf("\n Deleted value is %d.",q->data);
- free(q);
- display();
- }
- void delete_search()
- {
- int a,f=0;
- printf("\n Which element you want to delete? :- ");
- scanf("%d",&s);
- if(head==NULL)
- {
- printf("\n Linked List is not created!");
- return;
- }
- q=head;
- if(head->data==s)
- {
- head=head->next;
- printf("\n Deleted value is %d.",q->data);
- free(q);
- return;
- }
- while(q!=NULL)
- {
- temp=q;
- q=q->next;
- if(s==q->data)
- {
- f=1;
- temp->next=q->next;
- printf("\n Deleted Value is %d.",q->data);
- free(q);
- return;
- }
- }
- if(f==0)
- {
- printf("\n Value not found.");
- }
- display();
- }
- void delete_at_position()
- {
- int pos,i;
- if(head==NULL)
- {
- printf("\n Linked List is not created!");
- return;
- }
- printf("\n Enter the position :- ");
- scanf("%d",&pos);
- q=head;
- for(i=0;i<pos-1;i++)
- {
- temp=q;
- q=q->next;
- if(q==NULL)
- {
- printf("\n Invalid Position!");
- return;
- }
- }
- if(q==head)
- {
- head=head->next;
- printf("\n Deleted Value is %d.",q->data);
- free(q);
- return;
- }
- temp->next=q->next;
- printf("\n Deleted Value is %d.",q->data);
- free(q);
- display();
- }
- void delete_after_position()
- {
- int i,pos;
- printf("\n Enter the Position :- ");
- scanf("%d",&pos);
- q=head;
- for(i=0;i<=pos-1;i++)
- {
- temp=q;
- q=q->next;
- if(q==NULL)
- {
- printf("\n Invalid Position!");
- return;
- }
- }
- temp->next=q->next;
- printf("\n Deleted Value is %d.",q->data);
- free(q);
- display();
- }
- void delete_before_position()
- {
- int i,pos;
- printf("\n Enter the Position :- ");
- scanf("%d",&pos);
- if(pos==2)
- {
- temp=head;
- head=head->next;
- free(temp);
- return;
- }
- q=head;
- for(i=1;i<=pos-2;i++)
- {
- temp=q;
- q=q->next;
- if(q==NULL)
- {
- printf("\n Invalid Position!");
- return;
- }
- }
- temp->next=q->next;
- printf("\n Deleted Value is %d.",q->data);
- free(q);
- display();
- }
- void search_update()
- {
- int s,f=0;
- if(head==NULL)
- {
- printf("\n Linked List is not created!");
- return;
- }
- printf("\n Enter the element to be search :- ");
- scanf("%d",&s);
- q=head;
- while(q!=NULL)
- {
- if(q->data==s)
- {
- printf("\n Enter the new data :- ");
- scanf("%d",&q->data);
- f=1;
- display();
- return;
- }
- q=q->next;
- }
- if(f==0)
- {
- printf("\n Element not found!");
- }
- display();
- }
- void update_position()
- {
- int pos,i,s;
- if(head==NULL)
- {
- printf("\n Linked List is not created!");
- return;
- }
- printf("\n Enter the Position :- ");
- scanf("%d",&pos);
- q=head;
- for(i=0;i<pos-1;i++)
- {
- q=q->next;
- if(q==NULL)
- {
- printf("\n Invalid Position!");
- return;
- }
- }
- printf("\n Enter the new value :- ");
- scanf("%d",&(q->data));
- display();
- }
- void reverse()
- {
- struct node *p1,*p2,*p3;
- if(head==NULL)
- {
- printf("\n Linked List is not created!");
- }
- else if(head->next==NULL)
- {
- {
- printf("\n Only one element is created!");
- }
- }
- else
- {
- p1=head;
- p2=p1->next;
- p3=p2->next;
- p1->next=NULL;
- p2->next=NULL;
- while(p3!=NULL)
- {
- p1=p2;
- p2=p3;
- p3=p2->next;
- p2->next=p1;
- }
- head=p2;
- }
- printf("\n The Reversed Linked List is Successful!");
- printf("\n Press any key ...");
- getch();
- main();
- }
- void split_list()
- {
- int pos,i;
- if(head==NULL)
- {
- printf("\n Linked List is not Created!");
- }
- else
- {
- printf("\n Enter the Position you want to split :- ");
- scanf("%d",&pos);
- q=head;
- for(i=1;i<pos-1;i++)
- {
- q=q->next;
- }
- head1=q->next;
- q->next=NULL;
- printf("\n The spliting list are this");
- q=head1;
- while(q!=NULL)
- {
- printf("\n %d",q->data);
- q=q->next;
- }
- printf("\n The First Linked List is this");
- q=head;
- while(q!=NULL)
- {
- printf("\n %d",q->data);
- q=q->next;
- }
- printf("\n\t\t\t The Spliting is Successful");
- printf("\n Press any key ...");
- getch();
- main();
- }
- void merge_list()
- {
- int pos,i;
- struct node *d;
- if(head==NULL)
- {
- printf("\n Linked List is not creadted!");
- }
- else
- {
- printf("\n Enter the position you want to split :- ");
- scanf("%d",&pos);
- for(i=0;i<pos-1;i++)
- {
- q=q->next;
- }
- head1=q->next;
- q->next=NULL;
- printf("\n The Spliting List are this");
- q=head1;
- while(q!=NULL)
- {
- printf("\n %d",q->data);
- q=q->next;
- }
- printf("\n The First Linked List is this");
- q=head;
- while(q!=NULL)
- {
- printf("\n %d",q->data);
- q=q->next;
- }
- printf("\n The Merged Linked List is this");
- q=head;
- while(q!=NULL)
- {
- printf("\n %d",a->data);
- q=q->next;
- }
- q->next=head1;
- d=head1;
- while(d!=NULL)
- {
- printf("\n %d",d->data);
- d=d->next;
- }
- printf("\t\t\n Merging is Successful!");
- printf("\t\n Press any key ...");
- getch();
- main();
- }
- void sorted_insert()
- {
- temp=(struct node *)malloc(sizeof(struct node));
- printf("\n Enter your next data :- ");
- scanf("%d",&(temp->data));
- temp->next=NULL;
- q=head;
- if(head==NULL||temp->data<q->data)
- {
- temp->next=head;
- head=temp;
- }
- else
- {
- while(q->next!=NULL && q->next->data<temp->data)
- {
- q=q->next;
- }
- temp->next=q->next;
- q->next=temp;
- }
- display();
- }
Here are the errors:
Error 1:
Error DS\SLL.C 307: Declaration is not allowed here
Expand|Select|Wrap|Line Numbers
- ....
- ...
- void insert_after_position()
- {
- ...
- ...
Error DS\SLL.C 308: Declaration syntax error
Expand|Select|Wrap|Line Numbers
- ...
- ...
- {
- int p,l;
- ...
- ...
Error DS\SLL.C 746: Declaration missing ;
Expand|Select|Wrap|Line Numbers
- ...
- ...
- display();
- }
Error DS\SLL.C 746: Compound statement missing }
Expand|Select|Wrap|Line Numbers
- ...
- ...
- display();
- }
Please help me. Thank You in advance.