Friday, 23 August 2013

Singly Linked List Program in C

Hi, This program is about implementation of Singly linked list program in c.

If you have any doubt regarding this program or any concept of C then please send me at khimanichirag@gmail.com or post a comment.


suggestion always appreciable
      #include<stdio.h>  
      #include<conio.h>  
      struct node  
      {  
             int data;  
             struct node *link;  
      };  
      struct node *start=NULL,*top=NULL,*temp=NULL;  
      int i=0;  
      void print();  
      void create();  
      void search();  
      void delet();  
      void show();  
      void lin(int n)  
      {  
             int i;  
             printf("n");  
             for(i=0;i<n;i++)  
             {  
               printf("*");  
             }  
             printf("n");  
      }  
      void main()  
      {  
             clrscr();  
             print();  
      }  
      void print()  
      {  
             clrscr();  
             lin(50);  
             printf("1.create a node");  
             printf("n2.delete a node");  
             printf("n3.search a node at specified index");  
             printf("n4.show the Link List");  
             lin(50);  
             printf("nChoose your option:");  
             switch(getch())  
             {  
               case '1':  
                    create();  
                    break;  
               case '2':  
                    delet();  
                    break;  
               case '3':  
                    search();  
                    break;  
               case '4':  
                    show();  
                    break;  
               default:  
                    printf("nInvalid choice!!!!n");  
             }  
             printf("nYou want to do more operation.(Y/N):n");  
             if(getch()=='y')  
             print();  
      }  
      void create()  
      {  
             do  
             {  
               temp=(struct node*)malloc(sizeof(struct node));  
               if(start==NULL)  
               {  
                      start=temp;  
                      top=temp;  
                      top->link=NULL;  
               }  
               else  
               {  
                      top->link=temp;  
                      top=temp;  
                      top->link=NULL;  
               }  
               lin(35);  
               printf("nEnter the %d element: ",i+1);  
                     i++;  
               scanf("%d",&(top->data));  
               lin(40);  
               printf("nyou want to enter more elements(Y/N):n");  
             }while(getch()=='y');  
      }  
      void delet()  
      {  
             int num;  
             struct node *temp1=NULL;  
             printf("nEnter the data in which you want to delete the node");  
             scanf("%d",&num);  
             temp=start;  
             while(temp!=NULL)  
             {  
               if(temp->data==num)  
               {  
                      i--;  
                      if(temp->link==NULL)  
                      {  
                        temp1->link=temp->link;  
                        top=temp1;  
                        break;  
                      }  
                      else if(temp==start)  
                      {  
                        start=NULL;  
                        break;  
                      }  
                      else  
                      {  
                        temp1->link=temp->link;  
                        break;  
                      }  
               }  
               else  
               {  
                      temp1=temp;  
                      temp=temp->link;  
               }  
             }  
      }  
      void search()  
      {  
             int i=0,cnt=1;  
             printf("nnEnter the index in which you want to search a node in list:");  
             scanf("%d",&i);  
             temp=start;  
             while(cnt!=i)  
             {  
               temp=temp->link;  
               cnt++;  
             }  
             printf("nYour node at index %d is:= %d",i,temp->data);  
      }  
      void show()  
      {  
             if(start==NULL)  
             {  
               printf("your link list is emptyn");  
             }  
             printf("nnYour link list is: ");  
             temp=start;  
             while(temp!=NULL)  
             {  
               printf("%d ",temp->data);  
               temp=temp->link;  
             }  
             printf("n");  
      }  

No comments:

Post a Comment