Advertisement
Saleh127

data(dispaly,dlt,srch,edit)

Feb 27th, 2020
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4.  
  5. typedef struct Node
  6. {
  7. int data;
  8. struct Node *next;
  9. } node;
  10.  
  11. node *head = NULL;
  12. node *temp = NULL;
  13. node *tail = NULL;
  14.  
  15.  
  16.  
  17. void insertEndOfList()
  18. {
  19. node *newNode = (node*)malloc(sizeof(node));
  20. printf("Enter data for node : ");
  21. scanf("%d", &newNode->data);
  22. newNode->next = NULL;
  23.  
  24. if(head == NULL)
  25. {
  26. head = newNode;
  27. tail = newNode;
  28.  
  29. }
  30.  
  31. else
  32. {
  33. tail->next = newNode;
  34. tail = newNode;
  35. }
  36.  
  37. }
  38.  
  39.  
  40. void insertFrontOfList()
  41. {
  42. node *newNode = (node*)malloc(sizeof(node));
  43. printf("Enter data for node : ");
  44. scanf("%d", &newNode->data);
  45. newNode->next = NULL;
  46.  
  47. if(head == NULL)
  48. {
  49. head = newNode;
  50.  
  51. }
  52.  
  53. else
  54. {
  55. newNode->next = head;
  56. head = newNode;
  57. }
  58.  
  59. }
  60.  
  61.  
  62. void displayList()
  63. {
  64.  
  65. int count = 0;
  66.  
  67. if(head == NULL)
  68. {
  69. printf("\nList is Empty!!\n\n");
  70.  
  71. }
  72.  
  73. else
  74. {
  75.  
  76. printf("\nList is : ");
  77.  
  78. temp = head;
  79.  
  80. while(temp!=NULL)
  81. {
  82. printf("%d ", temp->data);
  83. temp = temp->next;
  84. count++;
  85. }
  86.  
  87. printf("\nTotal Node :%d\n\n", count);
  88.  
  89. }
  90.  
  91.  
  92. }
  93.  
  94.  
  95. void search()
  96. {
  97. printf("\n\nEnter data to search: ");
  98. int data;
  99. scanf("%d", &data);
  100.  
  101. temp = head;
  102. int count = 0;
  103.  
  104. while(temp!=NULL && temp->data != data)
  105. {
  106. temp = temp->next;
  107. count++;
  108. }
  109.  
  110. if(temp == NULL)
  111. {
  112. printf("%d doest not exist;\n\n", data);
  113. }
  114.  
  115. else
  116. {
  117. printf("\n\n\n%d found at Position: %d\n\n", data,count+1);
  118. }
  119.  
  120.  
  121. }
  122.  
  123.  
  124. void deleteNode()
  125. {
  126.  
  127. printf("\n\nEnter data to Delete: ");
  128. int data;
  129. scanf("%d", &data);
  130.  
  131. temp = head;
  132. node *previous = NULL;
  133. int count = 0;
  134.  
  135. while(temp!=NULL && temp->data != data)
  136. {
  137. previous = temp;
  138. temp = temp->next;
  139. count++;
  140. }
  141.  
  142. if(temp == NULL)
  143. {
  144. printf("%d doest not exist;\n\n", data);
  145. }
  146.  
  147. else
  148. {
  149. printf("\n\n\n%d found at Position: %d\n\n", data,count+1);
  150. previous->next = temp->next;
  151. free(temp);
  152. \
  153.  
  154. printf("\n\n\n%d Deleted\n\n", data);
  155. }
  156.  
  157. }
  158.  
  159. int main()
  160. {
  161. int i, n, m ;
  162.  
  163. printf("Enter N: ");
  164. scanf("%d",&n);
  165. for(i = 0; i< n ; i++)
  166. {
  167. insertEndOfList();
  168. }
  169. displayList();
  170.  
  171. printf("\nEnter M: ");
  172. scanf("%d", &m);
  173. for(i = 0; i< m ; i++)
  174. {
  175. insertFrontOfList();
  176. }
  177.  
  178. displayList();
  179.  
  180. search();
  181. deleteNode();
  182.  
  183. printf("\n\nAfter Deletion:");
  184. displayList();
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement