Advertisement
shawonrog

Untitled

Oct 17th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.87 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. //Create a basic structure for NODE from which new nodes can be created.
  5. struct node
  6. {
  7.     int data;
  8.     struct node *link;
  9. };
  10.  
  11. //Initialize 3 pointers as globals so that they do not need to be passed in functions.
  12. struct node *header, *ptr, *temp;
  13.  
  14. //Prototypes for various user defined functions.
  15. void insert_front();
  16. void insert_end();
  17. void insert_any();
  18. void display();
  19.  
  20. int main()
  21. {
  22.     int choice;
  23.     int cont = 1;
  24.  
  25.     //Allocate memory for header node.
  26.     header = (struct node *) malloc(sizeof(struct node));
  27.  
  28.  
  29.  
  30.     //Set the content of header node
  31.     header->data = NULL;
  32.     header->link = NULL;
  33.  
  34.     while(cont == 1)
  35.     {
  36.         //Display menu to the user
  37.         printf("\n1. Insert at front\n");
  38.         printf("\n2. Insert at end\n");
  39.         printf("\n3. Insert at any position\n");
  40.         printf("\n4. Display linked list\n");
  41.         printf("\nEnter your choice: ");
  42.         scanf("%d", &choice);
  43.  
  44.         switch(choice)
  45.         {
  46.             case 1:
  47.                 insert_front();
  48.                 break;
  49.             case 2:
  50.                 insert_end();
  51.                 break;
  52.             case 3:
  53.                 insert_any();
  54.                 break;
  55.             case 4:
  56.                 display();
  57.                 break;
  58.         }
  59.  
  60.         printf("\n\nDo you want to continue? (1 / 0): ");
  61.         scanf("%d", &cont);
  62.     }
  63.  
  64.     return 0;
  65. }
  66.  
  67. //Function to insert a node at the front of a single linked list.
  68. void insert_front()
  69. {
  70.     int data_value;
  71.  
  72.     printf("\nEnter data of the node: ");
  73.     scanf("%d", &data_value);
  74.  
  75.     temp = (struct node *) malloc(sizeof(struct node));
  76.  
  77.     temp->data = data_value;
  78.     temp->link = header->link;
  79.     header->link = temp;
  80. }
  81.  
  82. //Function to insert a node at the end of a single linked list.
  83. void insert_end()
  84. {
  85.     int data_value;
  86.  
  87.     printf("\nEnter data of the node: ");
  88.     scanf("%d", &data_value);
  89.  
  90.     temp = (struct node *) malloc(sizeof(struct node));
  91.  
  92.     //Traverse to the end of the linked list.
  93.     ptr = header;
  94.     while(ptr->link != NULL)
  95.     {
  96.         ptr = ptr->link;
  97.     }
  98.  
  99.     temp->data = data_value;
  100.     temp->link = ptr->link;
  101.     ptr->link = temp;
  102. }
  103.  
  104. //Function to insert a node at any position after a particular node.
  105. void insert_any()
  106. {
  107.     int data_value, key;
  108.  
  109.     printf("\nEnter data of the node: ");
  110.     scanf("%d", &data_value);
  111.     printf("\nEnter data of the node after which new node is to be inserted: ");
  112.     scanf("%d", &key);
  113.  
  114.     temp = (struct node *) malloc(sizeof(struct node));
  115.  
  116.     //Traverse till key is found or end of the linked list is reached.
  117.     ptr = header;
  118.     while(ptr->link != NULL && ptr->data != key)
  119.     {
  120.         ptr = ptr->link;
  121.     }
  122.     if(ptr->data == key)
  123.     {
  124.         temp->data = data_value;
  125.         temp->link = ptr->link;
  126.         ptr->link = temp;
  127.     }
  128.     else
  129.     {
  130.         printf("\nValue %d not found\n",key);
  131.     }
  132. }
  133.  
  134. //Function to display the contents of the linked list.
  135. void display()
  136. {
  137.     printf("\nContents of the linked list are: \n");
  138.     //Print the contents of the linked list starting from header
  139.     ptr = header;
  140.     while(ptr->link != NULL)
  141.     {
  142.         ptr = ptr->link;
  143.         printf("%d ", ptr->data);
  144.     }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement