Advertisement
rootUser

sd lab single link list

May 28th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.54 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. typedef struct Node
  4. {
  5.         int data;
  6.         struct Node *next;
  7. }node;
  8. void insert(node *pointer, int data)
  9. {
  10.         /* Iterate through the list till we encounter the last node.*/
  11.         while(pointer->next!=NULL)
  12.         {
  13.                 pointer = pointer -> next;
  14.         }
  15.         /* Allocate memory for the new node and put data in it.*/
  16.         pointer->next = (node *)malloc(sizeof(node));
  17.         pointer = pointer->next;
  18.         pointer->data = data;
  19.         pointer->next = NULL;
  20.         printf("Successfully inserted.......\n");
  21. }
  22. int find(node *pointer, int key)
  23. {
  24.         pointer =  pointer -> next; //First node is dummy node.
  25.         /* Iterate through the entire linked list and search for the key. */
  26.         while(pointer!=NULL)
  27.         {
  28.                 if(pointer->data == key) //key is found.
  29.                 {
  30.                         return 1;
  31.                 }
  32.                 pointer = pointer -> next;//Search in the next node.
  33.         }
  34.         /*Key is not found */
  35.         return 0;
  36. }
  37.  
  38. int update(node *pointer, int key, int new_key)
  39. {
  40.         pointer =  pointer -> next; //First node is dummy node.
  41.         /* Iterate through the entire linked list and search for the key. */
  42.         while(pointer!=NULL)
  43.         {
  44.                 if(pointer->data == key) //key is found.
  45.                 {
  46.                         pointer->data = new_key;
  47.                         return 1;
  48.                 }
  49.                 pointer = pointer -> next;//Search in the next node.
  50.         }
  51.         /*Key is not found */
  52.         return 0;
  53. }
  54.  
  55. void delete(node *pointer, int data)
  56. {
  57.         /* Go to the node for which the node next to it has to be deleted */
  58.         while(pointer->next!=NULL && (pointer->next)->data != data)
  59.         {
  60.                 pointer = pointer -> next;
  61.         }
  62.         if(pointer->next==NULL)
  63.         {
  64.                 printf("Element %d is not present in the list\n",data);
  65.                 return;
  66.         }
  67.         /* Now pointer points to a node and the node next to it has to be removed */
  68.         node *temp;
  69.         temp = pointer -> next;
  70.         /*temp points to the node which has to be removed*/
  71.         pointer->next = temp->next;
  72.         /*We removed the node which is next to the pointer (which is also temp) */
  73.         free(temp);
  74.         /* Beacuse we deleted the node, we no longer require the memory used for it .
  75.            free() will deallocate the memory.
  76.          */
  77.          printf("Successfully deleted.......\n");
  78.         return;
  79. }
  80. void print(node *pointer)
  81. {
  82.         if(pointer==NULL)
  83.         {
  84.                 return;
  85.         }
  86.         printf("%d ",pointer->data);
  87.         print(pointer->next);
  88. }
  89. int main()
  90. {
  91.         /* start always points to the first node of the linked list.
  92.            temp is used to point to the last node of the linked list.*/
  93.         node *start,*temp;
  94.         start = (node *)malloc(sizeof(node));
  95.         temp = start;
  96.         temp -> next = NULL;
  97.         /* Here in this code, we take the first node as a dummy node.
  98.            The first node does not contain data, but it used because to avoid handling special cases
  99.            in insert and delete functions.
  100.          */
  101.  
  102.         printf("1. Insert\n");
  103.         printf("2. Delete\n");
  104.         printf("3. Print\n");
  105.         printf("4. Find\n");
  106.         printf("5. Update\n");
  107.         while(1)
  108.         {
  109.                 int query;
  110.                 printf("Enter your choice :");
  111.                 scanf("%d",&query);
  112.                 if(query==1)
  113.                 {
  114.                         int data;
  115.                         printf("Enter the data you want to insert: ");
  116.                         scanf("%d",&data);
  117.                         insert(start,data);
  118.  
  119.                 }
  120.                 else if(query==2)
  121.                 {
  122.                         int data;
  123.                         printf("Enter the data you want to delete: ");
  124.                         scanf("%d",&data);
  125.                         delete(start,data);
  126.  
  127.                 }
  128.                 else if(query==3)
  129.                 {
  130.                         printf("The list is ");
  131.                         print(start->next);
  132.                         printf("\n");
  133.                 }
  134.                 else if(query==4)
  135.                 {
  136.                         int data;
  137.                         printf("Enter the data you want to find: ");
  138.                         scanf("%d",&data);
  139.                         int status = find(start,data);
  140.                         if(status)
  141.                         {
  142.                                 printf("Element Found\n");
  143.                         }
  144.                         else
  145.                         {
  146.                                 printf("Element Not Found\n");
  147.  
  148.                         }
  149.                 }
  150.                 else if(query==5)
  151.                 {
  152.                         int data,new_data;
  153.                         printf("Enter the data you want to update: ");
  154.                         scanf("%d",&data);
  155.                         printf("Enter the new data: ");
  156.                         scanf("%d",&new_data);
  157.                         int status = update(start,data,new_data);
  158.                         if(status)
  159.                         {
  160.                                 printf("Element successfully updated\n");
  161.                         }
  162.                         else
  163.                         {
  164.                                 printf("Element Not updated\n");
  165.  
  166.                         }
  167.                 }
  168.         }
  169.  
  170.  
  171.  
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement