pswaldia1

linkedList pointers

Aug 18th, 2018
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.15 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct node{
  5.     int info;
  6.     struct node* next;
  7.    
  8. };
  9.  typedef struct node node;
  10.  
  11. //initalisation of the start to NULL
  12. node* start=NULL;
  13.  
  14. // function that will return the available node.
  15.  node* getnode()
  16. {
  17.     node* temp=(node*)malloc(sizeof(node));
  18.     return temp;
  19. }
  20.  
  21. //function that will free the memory allocated to node passed as an argument
  22. void freenode(node* p)
  23. {
  24.  
  25.     free(p);
  26. }
  27.  
  28. //function to insert the node at the beginning
  29. void insert_at_the_beginning(int data)
  30. {
  31.     node* temp1=getnode();
  32.     temp1->info=data;
  33.     temp1->next=start;
  34.     start=temp1;
  35.    
  36. }
  37.  
  38. //function to display the node's info
  39. void display()
  40. {
  41.     node* temp1=start;
  42.     while(temp1!=NULL)
  43.     {
  44.         printf("%d ",temp1->info);
  45.         temp1=temp1->next;
  46.     }
  47. }
  48.  
  49. //function to search a data in a linked list.
  50. int Search_for_element(int data)
  51. {   int count=0;
  52.     node* temp=start;
  53.     while(temp!=NULL)
  54.     {
  55.         if(temp->info==data)
  56.         {
  57.             return count+1;
  58.         }
  59.         count=count+1;
  60.         temp=temp->next;
  61.     }
  62.     return -1;
  63.    
  64.    
  65. }
  66.  
  67. //finding a node previous to the node whose data is passed as an argument
  68. node* findNode_forInsertion(int data)   //valid for sorted list
  69. {   node* loc;
  70.     node* save;
  71.     node* ptr;
  72.     save=start;
  73.     ptr=start->next;
  74.    
  75.    
  76.     if(start==NULL)   //empty list
  77.     {
  78.         loc=NULL;
  79.         return loc;
  80.     }
  81.     if(data<start->info)
  82.     {
  83.         loc=NULL;
  84.         return loc;
  85.     }
  86.     while(ptr!=NULL)
  87.     {
  88.         if(data<ptr->info)
  89.         {
  90.             loc=save;
  91.             return loc;
  92.         }
  93.         save=ptr;
  94.         ptr=ptr->next;
  95.        
  96.     }
  97.     loc=save;
  98.     return loc;
  99. }
  100.  
  101. //function to insert the node anywhere in a linked list
  102. void insert_anywhere()
  103. {   int dp;
  104.     printf("enter the element that should be contained in a node\n");
  105.     scanf("%d",&dp);
  106.     struct node* loc=findNode_forInsertion(dp);
  107.     if(loc==NULL)
  108.     {
  109.         insert_at_the_beginning(dp);
  110.         return;
  111.     }
  112.  
  113.     struct node* temp=(node*)malloc(sizeof(node));
  114.     temp->info=dp;
  115.     temp->next=loc->next;
  116.     loc->next=temp;
  117. }
  118.  
  119. //function to find a node previous to the node whose data is given and the node itself.
  120. void findNodeInDeleteCase(int data,node** loc,node** locp)
  121. {
  122.     node* save;
  123.     node* ptr;
  124.     save=start;
  125.     ptr=start->next;
  126.     if(start==NULL)
  127.     {
  128.        
  129.         *locp=NULL;
  130.         *loc=NULL;
  131.         return;
  132.     }
  133.     if(data==start->info)
  134.     {
  135.        
  136.         *loc=start;
  137.         *locp=NULL;
  138.         return;
  139.     }
  140.     while(ptr!=NULL)
  141.     {
  142.        
  143.         if(data==ptr->info)
  144.         {
  145.             *loc=ptr;
  146.             *locp=save;
  147.             return;
  148.         }
  149.         save=ptr;
  150.         ptr=ptr->next;
  151.     }
  152.     *loc=ptr;
  153.     *locp=save;
  154.    
  155.     return;
  156.    
  157.    
  158. }
  159.  
  160. //function to delete the node whose address and the previous node's address is given.
  161. void deleteNode(node* locp,node* loc)
  162. {
  163.     if(loc==NULL)
  164.         printf("No node available to delete\n");
  165.     else if(locp==NULL)
  166.     {
  167.         start=start->next;   //deleting a first node
  168.         freenode(loc);   //call to fucnction
  169.        
  170.     }
  171.     else
  172.     {
  173.         locp->next=loc->next;
  174.         freenode(loc);  //deleting a node
  175.     }
  176.        
  177. }
  178. int main()
  179. {   int choice;
  180.     for(;;)
  181.     {   printf("\n");
  182.         printf("1. Insert at the beginning\n");
  183.         printf("2. Display the current list\n");
  184.         printf("3. Search for a element by data in the linked list\n");
  185.         printf("4.Insert Anywhere\n");
  186.         printf("5. Delete node\n");
  187.         printf("Enter your choice:\n");
  188.         scanf("%d",&choice);
  189.         switch(choice)
  190.         {
  191.             case 1:
  192.                 int data;
  193.                 printf("Enter the data\n");
  194.                 scanf("%d",&data);
  195.                 insert_at_the_beginning(data);
  196.                 break;
  197.             case 2:
  198.                 printf("The contents till now\n");
  199.                 display();
  200.                 break;
  201.             case 3:{
  202.                 int d;
  203.                 printf("Enter the element that you want to search\n");
  204.                 scanf("%d",&d);
  205.                 int x;
  206.                 x=Search_for_element(d);
  207.                 if(x==-1)
  208.                     printf("Element not found in the linked list\n");
  209.                 else
  210.                     printf("Element found at: %d position\n",x+1);
  211.                 break;
  212.             }
  213.             case 4:{
  214.                 insert_anywhere();
  215.                 break;
  216.         }
  217.             case 5:{
  218.                 node* locp=NULL;
  219.                 node* loc=NULL;
  220.                 int p;
  221.                 printf("enter the node's data that need to be deleted\n");
  222.                 scanf("%d",&p);
  223.                 findNodeInDeleteCase(p,&loc,&locp);
  224.                 deleteNode(locp, loc);
  225.                 break;
  226.             }
  227.                
  228.             case 6:
  229.                 exit(0);
  230.             default:
  231.                 printf("entered choice is not correct.\n");
  232.                 break;
  233.             }
  234.        
  235.     }
  236.    
  237. }
Add Comment
Please, Sign In to add comment