Advertisement
Guest User

Untitled

a guest
Jan 19th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.82 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<stdbool.h>
  4.  
  5. struct Node{
  6.     int info;
  7.     struct Node *next;
  8. };
  9.  
  10. typedef struct Node NodeType;
  11.  
  12. // Two glboal variables to store address of first and last nodes.
  13. NodeType *first =NULL , *last = NULL;
  14.  
  15.  
  16. NodeType* getNode(){
  17.     NodeType *ptr;
  18.     ptr = (NodeType *) malloc(sizeof(NodeType));
  19.     return ptr;
  20. }
  21.  
  22. void createEmptyList(NodeType *first){
  23.     first =NULL;
  24.     printf("\nList is emptied.\n");
  25. }
  26.  
  27. void insertatBegin(int item){
  28.     NodeType *newnode;
  29.     NodeType *temp;
  30.  
  31.     temp = first; //hold the address of the first node of list(which is already present)
  32.    
  33.     newnode = getNode(); //creates the new node
  34.  
  35.     newnode ->info = item; //info section of new node
  36.  
  37.     //filling next(address) section of new node
  38.     if(first ==NULL){ //if the linked list is empty i.e. first address is null
  39.         newnode ->next =NULL;
  40.         first = last = newnode; //there is only one node so first and last is same node
  41.     }
  42.  
  43.     else{ //if not empty
  44.         newnode ->next = temp;
  45.         first = newnode;
  46.     }
  47.  
  48. }
  49.  
  50.  
  51. void insertatEnd(int item){
  52.     NodeType *newnode;
  53.     newnode =getNode();
  54.  
  55.     newnode -> info =item;
  56.     newnode -> next =NULL;
  57.    
  58.     if (first == NULL) {
  59.         first = newnode;
  60.         last = newnode;
  61.     }
  62.    
  63.     else
  64.     {
  65.         last -> next = newnode;
  66.         last = newnode;
  67.     }
  68.  
  69. }
  70.  
  71.  
  72. void displayElements(){
  73.     NodeType *temp;
  74.  
  75.    
  76.     if (first == NULL) {
  77.         printf("\n Linked list is empty.\n");
  78.         exit(1);
  79.     }
  80.    
  81.     else
  82.     {
  83.         while(temp != NULL){
  84.             printf("%d\t",temp->info);
  85.             temp = temp ->next;
  86.         }
  87.        
  88.     }
  89.    
  90.    
  91. }
  92.  
  93.  
  94. int main(int argc, char const *argv[])
  95. {
  96.     int  item,ch;
  97.    
  98.  
  99.    
  100.     while(1){
  101.         printf("\n---------------------------------\n");
  102.         printf("\nOperations on singly linked list\n");
  103.         printf("\n---------------------------------\n");
  104.         printf("\n0.Make List Empty");
  105.         printf("\n1.Insert node at first");
  106.         printf("\n2.Insert node at last");
  107.         printf("\n3.Insert node at position");
  108.         printf("\n4.Delete first Node");
  109.          printf("\n5.Delete last node");
  110.         printf("\n6.Delete Node from any Position");
  111.         printf("\n7.Sum of all data(info) of nodes");
  112.         printf("\n8.Count no. of nodes");
  113.         printf("\n9.Search Element in the linked list");
  114.         printf("\n10.Display List from Beginning to end");
  115.         printf("\n11.Exit\n");
  116.         printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
  117.  
  118.         printf("Enter your choice : ");
  119.         scanf("%d",&ch);
  120.  
  121.        
  122.         switch (ch)
  123.         {
  124.             case 0:
  125.                 createEmptyList(first);
  126.                 break;
  127.  
  128.  
  129.             case 1:
  130.               printf("Enter the item to be inserted : ");
  131.               scanf("%d",&item);
  132.               insertatBegin(item);
  133.                 break;
  134.  
  135.  
  136.             case 2:
  137.              printf("Enter the item to be inserted : ");
  138.               scanf("%d",&item);
  139.               insertatEnd(item);
  140.                 break;
  141.    
  142.  
  143.  
  144.             case 3:
  145.              
  146.                 break;
  147.  
  148.  
  149.             case 4:
  150.              
  151.                 break;
  152.  
  153.  
  154.             case 5:
  155.              
  156.                 break;
  157.  
  158.  
  159.             case 6:
  160.              
  161.                 break;
  162.  
  163.  
  164.             case 7:
  165.              
  166.                 break;
  167.  
  168.  
  169.             case 8:
  170.              
  171.                 break;
  172.  
  173.  
  174.             case 9:
  175.              
  176.                 break;
  177.  
  178.  
  179.             case 10:
  180.                 displayElements();
  181.  
  182.  
  183.                 break;
  184.  
  185.             case 11:
  186.               exit(1);
  187.  
  188.        
  189.             default:
  190.                 break;
  191.         }
  192.     }
  193.    
  194.  
  195.  
  196.  
  197.  
  198.     return 0;
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement