Advertisement
shawonrog

Untitled

Oct 17th, 2017
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.98 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 display();
  18.  
  19.  
  20. void insert_front()
  21. {
  22.     int data_value;
  23.  
  24.     printf("\nEnter data of the node: ");
  25.     scanf("%d", &data_value);
  26.  
  27.     temp = (struct node *) malloc(sizeof(struct node));
  28.  
  29.     temp->data = data_value;
  30.     temp->link = header->link;
  31.     header->link = temp;
  32. }
  33.  
  34.  
  35. void insert_end()
  36. {
  37.     int data_value;
  38.  
  39.     printf("\nEnter data of the node: ");
  40.     scanf("%d", &data_value);
  41.  
  42.     temp = (struct node *) malloc(sizeof(struct node));
  43.  
  44.     //Traverse to the end of the linked list.
  45.     ptr = header;
  46.     while(ptr->link != NULL)
  47.     {
  48.         ptr = ptr->link;
  49.     }
  50.  
  51.     temp->data = data_value;
  52.     temp->link = ptr->link;
  53.     ptr->link = temp;
  54. }
  55. void display()
  56. {
  57.     printf("\nContents of the linked list are: \n");
  58.     //Print the contents of the linked list starting from header
  59.     ptr = header;
  60.     while(ptr->link != NULL)
  61.     {
  62.         ptr = ptr->link;
  63.         printf("%d ", ptr->data);
  64.     }
  65. }
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72. int main()
  73. {
  74.     int choice;
  75.     int cont = 1;
  76.  
  77.     //Allocate memory for header node.
  78.     header = (struct node *) malloc(sizeof(struct node));
  79.  
  80.  
  81.  
  82.     //Set the content of header node
  83.     header->data = NULL;
  84.     header->link = NULL;
  85.  
  86.     while(cont == 1)
  87.     {
  88.         //Display menu to the user
  89.         printf("\n1. Insert at front\n");
  90.         printf("\n2. Insert at end\n");
  91.         printf("\n3. Display linked list\n");
  92.         printf("\nEnter your choice: ");
  93.         scanf("%d", &choice);
  94.  
  95.         switch(choice)
  96.         {
  97.             case 1:
  98.                 insert_front();
  99.                 break;
  100.             case 2:
  101.                 insert_end();
  102.                 break;
  103.  
  104.             case 3:
  105.                 display();
  106.                 break;
  107.         }
  108.  
  109.         printf("\n\nDo you want to continue? (1 / 0): ");
  110.         scanf("%d", &cont);
  111.     }
  112.  
  113.     return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement