Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. typedef struct Node
  4. {
  5. int x;
  6. struct Node *ptr;
  7. }node;
  8.  
  9. node *head = NULL;
  10.  
  11. node *temp = NULL;
  12.  
  13. int main()
  14. {
  15. int i, n, c, t, j, s, m=0, k, l, h;
  16. printf("Input The Number of Nodes:");
  17. scanf("%d", &n);
  18. for(i=1; i<=n; i++)
  19. {
  20. node q = (node)malloc(sizeof (node));
  21. printf("Input Data for %d: ", i);
  22. scanf("%d", &q -> x);
  23. q -> ptr = NULL;
  24. if(head == NULL)
  25. {
  26. head = q;
  27. }
  28. else
  29. {
  30. temp = head;
  31. while(temp -> ptr != NULL)
  32. {
  33. temp = temp -> ptr;
  34. }
  35.  
  36. temp -> ptr = q;
  37. }
  38. }
  39.  
  40. temp = head;
  41. printf("\nThe list is:\n");
  42. while(temp!= NULL)
  43. {
  44. printf("%d ", temp->x);
  45. temp = temp->ptr;
  46. m++;
  47. }
  48. printf("\nSearch test case:");
  49. scanf("%d", &t);
  50. for(j=1; j<=t; j++)
  51. {
  52. c = 1;
  53. printf("\nEnter data you want to search : ");
  54. scanf("%d", &s);
  55. temp = head;
  56. if(head->x == s)
  57. {
  58. printf("%d found at node %d", s, c);
  59. }
  60. else
  61. {
  62. while(temp != NULL && temp->x != s)
  63. {
  64. temp = temp->ptr;
  65. c++;
  66. }
  67. if(temp == NULL)
  68. {
  69. printf("%d does not exist in the list", s);
  70. }
  71. else
  72. {
  73. printf("%d found at node %d", s, c);
  74. }
  75. }
  76. }
  77. printf("\nDelete with position test case:");
  78. scanf("%d", &t);
  79. for(j=1; j<=t; j++)
  80. {
  81. printf("\n\nInput the position of node to delete : ");
  82. scanf("%d", &i);
  83. if(i > n)
  84. {
  85. printf("Node not found\n");
  86. break;
  87. }
  88. else
  89. {
  90. temp = head;
  91. if(i == 1)
  92. {
  93. head = head->ptr;
  94. free(temp);
  95. }
  96. else
  97. {
  98. c = 1;
  99. temp = head;
  100. node *previous = NULL;
  101. while(temp != NULL && c<=i-1)
  102. {
  103. previous = temp;
  104. temp = temp->ptr;
  105. c++;
  106. }
  107. previous->ptr = temp->ptr;
  108. free(temp);
  109. }
  110. printf("\nDeletion complete successfully.\n");
  111. printf("\nThe new list is :\n");
  112. temp = head;
  113. while(temp != NULL)
  114. {
  115. printf("%d ", temp->x);
  116. temp = temp->ptr;
  117. }
  118. printf("\n");
  119. break;
  120. }
  121.  
  122. }
  123. printf("\nDelete with data test case:");
  124. scanf("%d", &t);
  125. for(k=1; k<=t; k++)
  126. {
  127. l = 0;
  128. h = 1;
  129. printf("\nEnter data you want to delete : ");
  130. scanf("%d", &s);
  131. temp = head;
  132. if(head->x == s)
  133. {
  134. head = head->ptr;
  135. printf("%d founded & deleted at node %d\n", s, h);
  136. free(temp);
  137. }
  138. else
  139. {
  140. node *previous = NULL;
  141. while(temp != NULL && temp->x != s)
  142. {
  143. previous = temp;
  144. temp = temp->ptr;
  145. h++;
  146. }
  147. if(temp == NULL)
  148. {
  149. printf("%d does not exist in the list\n", s);
  150. break;
  151. }
  152. else
  153. {
  154. previous->ptr = temp->ptr;
  155. printf("%d founded & deleted at node %d\n", s, h);
  156. free(temp);
  157. }
  158. }
  159. printf("\nThe List after deletion the data :\n");
  160. temp = head;
  161. while(temp != NULL)
  162. {
  163. printf("%d ", temp->x);
  164. temp = temp->ptr;
  165. l++;
  166. }
  167. printf("\n\nTotal Nodes : %d\n", l);
  168. }
  169. printf("\n");
  170. return 0;
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement