Write a C program to implement the operations on a singly linked list. The program should include the following operations.

i. Creation of linked list

ii. Traversal of linked list

iii.Insertion of element---

                ( a). At the beginning of list

    ( b).At the end of list

         ( c).Before a given node

       ( d). After a given node

       ( e). At a given position

                                                       iv. Deletion of a node---

  (a). First node

(b)Last node

                                 (c). Node with a particular value

                          (d). Node at a given position

     CODING in C:-

  1. #include<stdio.h>
  2. #include<malloc.h>
  3.  
  4. //Definition of the data structure
  5. struct node
  6. {
  7.                //parts of node
  8.                int data;
  9.                struct node *next;
  10. };
  11.  
  12. //Rename of the data structure
  13. typedef struct node node;
  14.  
  15. //Function prototype declarations
  16. node *createlist (node *head);
  17. void traverse (node *head);
  18. node *insert (node *head);
  19. node *insertbegin(node *head, int d);
  20. node *insertend(node *head, int d);
  21. node *insertbefore(node *head, int d);
  22. node *insertafter(node *head, int d);
  23. node *insertatparticularposition(node *head, int d);
  24. node *delet(node *head);
  25. node *deletbegin(node *head);
  26. node *deletend(node *head);
  27. node *deletvalue(node *head);
  28. node *deletatparticularposition(node *head);
  29.  
  30. int main()
  31. {
  32.                //Declaration of  variable
  33.                char cont;
  34.     node *head;
  35.     //Allocation memory dynamically for the variable
  36.     head = (node *)malloc(sizeof(node));
  37.    
  38.    
  39.     do
  40.                {
  41.                    //Displaying and calling the functions
  42.                    printf("\nCreation of a Linked List");
  43.         head = createlist(head);
  44.                    printf("\nContents of Linked List : \n");
  45.                    traverse(head);            
  46.                    printf("\nInsertion of a new node : \n");
  47.                    head = insert(head);
  48.                    printf("\nDeletion of Node : \n");
  49.                    head = delet(head);
  50.                    printf("\n Do you want to continue ? (Y or N) : ");
  51.                    scanf("%s",&cont);
  52.                }while(cont == 'Y' || cont =='y');
  53. }
  54.  
  55. //Function to create a linked list
  56. node *createlist (node *head)
  57. {
  58.                //Declaration of variables
  59.                int n, i;
  60.                node *list;
  61.               
  62.                //Inputt total number
  63.                printf("\nEnter total number of elements in the list : ");
  64.                scanf("%d", &n);
  65.               
  66.                //Initioalizing the list by head pointer
  67.                list = head;
  68.  
  69.                for ( i = 0; i<n; i++)
  70.                {
  71.                               printf("Enter data :");
  72.                               scanf("%d", &list->data);
  73.                               if ( i == n-1)
  74.                                 list->next = NULL;
  75.                               else
  76.                               {                                
  77.                                 list->next=(node *)malloc(sizeof(node));
  78.                                 list = list->next;                             
  79.                    }
  80.                }
  81.                return head;
  82. }
  83.  
  84. //Function to traverse a linked list
  85. void traverse (node *head)
  86. {
  87.                //Declaration of variables
  88.                node *list;
  89.               
  90.                //Initializing the list by pointer head
  91.                list = head;
  92.  
  93.                while (list->next != NULL)
  94.     {
  95.                printf("%d -> ",list->data);
  96.                list = list -> next;
  97.                }
  98.                printf("%d\n", list->data);
  99. }
  100.  
  101. //Function to insert nodes in a linked list
  102. node *insert (node *head)
  103. {
  104.                //declaring variables
  105.                int element, option;
  106.               
  107.                //Inputt the data to insert
  108.                printf("Enter data to insert :");
  109.                scanf("%d", &element);
  110.               
  111.                //Displaying insertion options
  112.                printf("Insertion options:");
  113.                printf("\n1. At the beginning");
  114.                printf("\n2. At the end");
  115.                printf("\n3. Before a key node");
  116.                printf("\n4. After a key node");
  117.                printf("\n5. At a particular position");
  118.                printf("\nEnter option : ");
  119.               
  120.                //Inputt the insertion option
  121.                scanf("%d", &option);
  122.               
  123.                switch(option)
  124.                {
  125.                               //Calling of functions according to the oprtions
  126.                               case 1:
  127.                                              head = insertbegin(head, element);
  128.                                              break;
  129.                               case 2:
  130.                                              head = insertend(head, element);
  131.                                              break;
  132.                               case 3:
  133.                                              head = insertbefore(head, element);
  134.                                              break;
  135.                               case 4:
  136.                                              head = insertafter(head, element);
  137.                                              break;
  138.                               case 5:
  139.                                              head = insertatparticularposition(head, element);
  140.                                              break;
  141.                               //Printing the default case
  142.                               default:
  143.                                               printf("\nInvalid choice. Insertion not done.");                                                                   
  144.                }
  145.               
  146.                printf("\nContents of Linked List after Insertion: \n");
  147.                traverse(head); 
  148.                return head;
  149. }
  150.  
  151. //Function to insert node at the begining
  152. node *insertbegin(node *head, int d)
  153. {
  154.                //Declaration of variable
  155.                node * tempnode;
  156.               
  157.                //Allocating memory dynamically for the new node
  158.                tempnode = (node *)malloc(sizeof(node));
  159.               
  160.                //Insert the new node at the begining
  161.                tempnode->data = d;
  162.                tempnode->next = head;
  163.               
  164.                head = tempnode;           
  165.                return head;
  166. }
  167.  
  168. //Function to insert node at the end
  169. node *insertend(node *head, int d)
  170. {
  171.                //Declaration of variables
  172.                node *tempnode, *list;
  173.               
  174.                //Allocating memory dynamically for the new node
  175.                tempnode = (node *)malloc(sizeof(node));
  176.               
  177.                //Inputt the data into the new node
  178.                tempnode->data = d;
  179.                tempnode->next = NULL;
  180.  
  181.                list = head;
  182.  
  183.                while (list -> next != NULL)
  184.                   list = list -> next;
  185.               
  186.         list -> next = tempnode;
  187.                return head;  
  188. }
  189.  
  190. //Function to insert node before a particular node
  191. node *insertbefore(node *head, int d)
  192. {
  193.                //Declaration and initialization of variables
  194.                int key;
  195.                int found = 0;
  196.                node *tempnode, *list;
  197.               
  198.                //Allocating memory dynamically for the new node
  199.                tempnode = (node *)malloc(sizeof(node));
  200.               
  201.                //Inputt the data into the new node
  202.                tempnode->data = d;
  203.               
  204.                printf("Enter value of key :")          ;
  205.                scanf("%d",&key);
  206.               
  207.                if ( head -> data == key)
  208.                {
  209.                               //Insertion of the new node at the beginging
  210.                               tempnode -> next = head;
  211.                               head = tempnode;
  212.                               return head;
  213.                }
  214.               
  215.                list = head;
  216.                while (found == 0 && list -> next != NULL)
  217.                {
  218.                               if (list -> next -> data == key)
  219.                               {
  220.                                              tempnode -> next = list -> next;
  221.                                              list -> next = tempnode;
  222.                                              found = 1;
  223.                               }
  224.                               else
  225.                                  list = list -> next;
  226.                }
  227.               
  228.                if ( found == 0)
  229.                   printf ("Key Node not found!");
  230.               
  231.                return head;
  232. }
  233.  
  234. //Function to insert node after a particular node
  235. node *insertafter (node *head, int d)
  236. {
  237.                //Initialization and declaration of variables
  238.                int key;
  239.                int found = 0;
  240.                node *list, *tempnode;
  241.               
  242.                //Allocating memory dynamically for the new node
  243.                tempnode = (node *)malloc(sizeof(node));
  244.               
  245.                tempnode->data = d;
  246.                printf("Enter value of key :")          ;
  247.                scanf("%d",&key);
  248.         list = head;
  249.    
  250.         while (found == 0 && list != NULL)     
  251.         {
  252.                     if (list -> data == key)
  253.                     {
  254.                               tempnode -> next = list -> next;
  255.                               list -> next = tempnode;
  256.                               found = 1;
  257.                     }
  258.                               else
  259.                                   list = list -> next;
  260.                 }
  261.                 
  262.                if ( found == 0)
  263.                   printf ("Key Node not found!");
  264.               
  265.                return head;
  266. }
  267.  
  268. //Function to insert a node at a particular position
  269. node *insertatparticularposition(node *head, int d)
  270. {
  271.                //Declaration and initialization of variables
  272.                int keyposition;
  273.                int currentposition=1;
  274.                int validposition=0;
  275.                node *list, *tempnode;
  276.               
  277.                //Dynamically allocation of memory for the new node
  278.                tempnode = (node *)malloc(sizeof(node));
  279.                tempnode->data = d;
  280.               
  281.                //Input value of keynode
  282.                printf("Enter the position :")          ;
  283.                scanf("%d",&keyposition);
  284.               
  285.                list=head;
  286.                //If the position is in first location
  287.                if(keyposition==0)
  288.                {
  289.                               //Insert the new node at the begining
  290.                               tempnode -> next = head;
  291.                               head=tempnode;
  292.                               return head;
  293.                }
  294.                //Inserting the new node at the particular location
  295.                while(validposition==0 && list !=NULL)
  296.                {
  297.                               if(currentposition==keyposition)
  298.                               {
  299.                                              tempnode -> next = list -> next;
  300.                                              list -> next = tempnode;
  301.                                              validposition=1;
  302.                               }
  303.                               else
  304.                               {
  305.                                              currentposition=currentposition+1;
  306.                                              list=list -> next;
  307.                               }
  308.                }
  309.                if(validposition==0)
  310.                {
  311.                               printf("Invalid Position.");
  312.                }
  313.               
  314.                return head;
  315. }
  316.  
  317. //Function to delete the nodes of a linked list
  318. node *delet(node *head)
  319. {
  320.                //Initializing the option variable
  321.                int option;
  322.               
  323.                //Displaying the deletion options
  324.                printf("Deletion options:");
  325.                printf("\n1. Deletion of First Node");
  326.                printf("\n2. Deletion of Last Node");
  327.                printf("\n3. Deletion of node with a particular value");
  328.                printf("\n4. Deletion of a node at a given position");
  329.                printf("\nEnter option : ");
  330.                scanf("%d", &option);
  331.                switch(option)
  332.                {
  333.                              
  334.                               case 1:
  335.                                              head = deletbegin(head);
  336.                                              break;
  337.                case 2:
  338.                                              head = deletend(head);
  339.                                              break;
  340.                               case 3:
  341.                                               head = deletvalue(head);
  342.                                               break;
  343.                               case 4:
  344.                                              head = deletatparticularposition(head);
  345.                                              break;
  346.                               //Diplaying the default option
  347.                               default:
  348.                                              printf("\nInvalid choice. Deletion not done.");                                                                    
  349.                }
  350.                 printf("\nContents of Linked List after Deletion: \n");
  351.                traverse(head); 
  352.                return head;       
  353. }
  354.  
  355. //Function to delete the first node of the list
  356. node *deletbegin(node *head)
  357. {
  358.                //Declaring the variables
  359.                node *delnode, *list;
  360.               
  361.                //If list does not exist
  362.                if (head == NULL)
  363.                {
  364.                               printf("Empty List");
  365.                               return NULL;
  366.                }
  367.                //initializing the variable delnode with head
  368.                delnode = head;
  369.  
  370.                //Deleting the first node
  371.                head = head -> next;
  372.  
  373.                //Deallocating the deleted node
  374.                free(delnode);
  375.                return head;
  376. }
  377.  
  378. //Function to delete the last node if a list
  379. node *deletend(node *head)
  380. {
  381.                //Declaring the variables
  382.                node *delnode, *list;
  383.               
  384.                //If the list does not exist
  385.                if (head == NULL)
  386.                {
  387.                               printf("Empty List");
  388.                               return NULL;
  389.                }
  390.               
  391.                //If the node to be deleted is the first node
  392.                if (head -> next == NULL)
  393.                {
  394.                               //Deleting the node
  395.                               delnode=head;
  396.                               head = NULL;
  397.                               free(delnode);
  398.                               return(head);
  399.                }
  400.               
  401.                list = head;
  402.                //Deleting the node
  403.                while( list -> next -> next != NULL )
  404.                   list = list -> next;
  405.         delnode = list -> next;
  406.                list -> next = NULL;
  407.               
  408.                //Deallocating the deleted node
  409.                free(delnode);
  410.                return(head);
  411. }
  412.  
  413. //Function to delete a node with a particular value
  414. node *deletvalue(node *head)
  415. {
  416.                //Declaration and initialization of variables
  417.                int key, found = 0;
  418.                node *delnode, *list;
  419.               
  420.                //Inputt the value
  421.                printf("Enter value of node that you want to delete : ");
  422.                scanf("%d", &key);
  423.                //If the list is empty
  424.                if (head == NULL)
  425.                {
  426.                               printf("Empty List");
  427.                               return NULL;
  428.                }
  429.               
  430.                //If the first node is the node with the value
  431.                if (head -> data == key)
  432.                {
  433.                               //Deleting the node
  434.                               delnode=head;
  435.                               head = head-> next;
  436.                               free(delnode);
  437.                               return(head);
  438.                }
  439.                 
  440.                list = head;
  441.                //Deletion of that particular valued node
  442.                while(list -> next != NULL && found == 0)
  443.                {
  444.                               if(list -> next -> data == key)
  445.                               {
  446.                                              found = 1;
  447.                                              delnode=list->next;
  448.                                              list->next = list -> next -> next;
  449.                                             
  450.                                              //Deallocating the node
  451.                                              free(delnode);
  452.                                              return(head);
  453.                               }
  454.                               list=list->next;
  455.                }
  456.                if(found==0)
  457.                               printf("Node not found");
  458.                return(head);                    
  459. }
  460.  
  461. //Deleting a node at a particular position
  462. node *deletatparticularposition(node *head)
  463. {
  464.                //Declaration and initialization of variables
  465.                int pos,cnt=1;
  466.               
  467.                //Input the value
  468.                printf("Enter position of node that you want to delete : ");
  469.                scanf("%d", &pos);
  470.               
  471.                node *p=head,*tmp;
  472.                //To check valid position
  473.                if(pos<1 || pos>countNodes(head))
  474.                               printf("\nWrong position value, try again:");
  475.                else
  476.                {
  477.                              
  478.                               if(pos==1)//if position is 1
  479.                               {
  480.                                              p=head;
  481.                                              head=head->next;
  482.                               }
  483.                               else//if position is more than 1
  484.                               {
  485.                                             
  486.                                              while(cnt<pos)
  487.                                              {
  488.                                                             cnt++;
  489.                                                             tmp=p;
  490.                                                             p=p->next;
  491.                                              }
  492.                                             
  493.                                              tmp->next=p->next;//insert node
  494.                               }
  495.                              
  496.                               free(p);
  497.                }
  498.                return head;
  499. }
  500.  
  501. int countNodes(node *head)
  502. {
  503.                //Declaration and initialization of variables
  504.     int count=0;
  505.     node *p;
  506.     p=head;
  507.     //Count the number of nodes
  508.     while(p!=NULL)
  509.     {
  510.         count++;
  511.         p=p->next;
  512.     }
  513.     return(count);
  514. } 

OUTPUTS :-

OUTPUT 1 :-

Creation of a Linked List

Enter total number of elements in the list : 3

Enter data :23

Enter data :34

Enter data :45

 

Contents of Linked List :

23 -> 34 -> 45

 

Insertion of a new node :

Enter data to insert :12

Insertion options:

1. At the beginning

2. At the end

3. Before a key node

4. After a key node

5. At a particular position

Enter option : 1

 

Contents of Linked List after Insertion:

12 -> 23 -> 34 -> 45

 

Deletion of Node :

Deletion options:

1. Deletion of First Node

2. Deletion of Last Node

3. Deletion of node with a particular value

4. Deletion of a node at a given position

Enter option : 1

 

Contents of Linked List after Deletion:

23 -> 34 -> 45

 

 

OUTPUT 2 :-

 

 

Creation of a Linked List

Enter total number of elements in the list : 3

Enter data :12

Enter data :23

Enter data :34

 

Contents of Linked List :

12 -> 23 -> 34

 

Insertion of a new node :

Enter data to insert :45

Insertion options:

1. At the beginning

2. At the end

3. Before a key node

4. After a key node

5. At a particular position

Enter option : 2

 

Contents of Linked List after Insertion:

12 -> 23 -> 34 -> 45

 

Deletion of Node :

Deletion options:

1. Deletion of First Node

2. Deletion of Last Node

3. Deletion of node with a particular value

4. Deletion of a node at a given position

Enter option : 2

 

Contents of Linked List after Deletion:

12 -> 23 -> 34

 

OUTPUT 3:-

 

Creation of a Linked List

Enter total number of elements in the list : 3

Enter data :12

Enter data :23

Enter data :45

 

Contents of Linked List :

12 -> 23 -> 45

 

Insertion of a new node :

Enter data to insert :34

Insertion options:

1. At the beginning

2. At the end

3. Before a key node

4. After a key node

5. At a particular position

Enter option : 3

Enter value of key :45

 

Contents of Linked List after Insertion:

12 -> 23 -> 34 -> 45

 

Deletion of Node :

Deletion options:

1. Deletion of First Node

2. Deletion of Last Node

3. Deletion of node with a particular value

4. Deletion of a node at a given position

Enter option : 3

 

Enter value of node that you want to delete : 45

Contents of Linked List after Deletion:

12 -> 23 -> 34

 

OUTPUT 4:-

 

Creation of a Linked List

Enter total number of elements in the list : 3

Enter data :12

Enter data :23

Enter data :34

 

Contents of Linked List :

12 -> 23 -> 34

Insertion of a new node :

Enter data to insert :31

Insertion options:

1. At the beginning

2. At the end

3. Before a key node

4. After a key node

5. At a particular position

Enter option : 4

Enter value of key :23

 

Contents of Linked List after Insertion:

12 -> 23 -> 31 -> 34

 

Deletion of Node :

Deletion options:

1. Deletion of First Node

2. Deletion of Last Node

3. Deletion of node with a particular value

4. Deletion of a node at a given position

Enter option : 4

Enter position of node that you want to delete : 4

 

Contents of Linked List after Deletion:

12 -> 23 -> 31 


0 comments:

Post a Comment