Advertisement
Guest User

test 2

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