Advertisement
shawonrog

p5

Apr 11th, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.13 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. Display linked list\n");
  40.         printf("\nEnter your choice: ");
  41.  
  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.                 display();
  54.                 break;
  55.         }
  56.  
  57.         printf("\n\nDo you want to continue? (1 / 0): ");
  58.         scanf("%d", &cont);
  59.     }
  60.  
  61.     return 0;
  62. }
  63.  
  64. //Function to insert a node at the front of a single linked list.
  65. void insert_front()
  66. {
  67.     int data_value;
  68.  
  69.     printf("\nEnter data of the node: ");
  70.     scanf("%d", &data_value);
  71.  
  72.     temp = (struct node *) malloc(sizeof(struct node));
  73.  
  74.     temp->data = data_value;
  75.     temp->link = header->link;
  76.     header->link = temp;
  77. }
  78.  
  79. //Function to insert a node at the end of a single linked list.
  80. void insert_end()
  81. {
  82.     int data_value;
  83.  
  84.     printf("\nEnter data of the node: ");
  85.     scanf("%d", &data_value);
  86.  
  87.     temp = (struct node *) malloc(sizeof(struct node));
  88.  
  89.     //Traverse to the end of the linked list.
  90.     ptr = header;
  91.     while(ptr->link != NULL)
  92.     {
  93.         ptr = ptr->link;
  94.     }
  95.  
  96.     temp->data = data_value;
  97.     temp->link = ptr->link;
  98.     ptr->link = temp;
  99. }
  100.  
  101.  
  102. //Function to display the contents of the linked list.
  103. void display()
  104. {
  105.     printf("\nContents of the linked list are: \n");
  106.     //Print the contents of the linked list starting from header
  107.     ptr = header;
  108.     while(ptr->link != NULL)
  109.     {
  110.         ptr = ptr->link;
  111.         printf("%d ", ptr->data);
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement