Advertisement
palmerstone

Linked List insertion sort

Sep 18th, 2011
480
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6.  
  7. struct node
  8. {
  9. node *prev;
  10. int x;
  11. node *next;
  12. };
  13.  
  14. node *ptr = 0, *ptr2 = 0, *ptr3 = 0, *ptr4 = 0, *ptr5 = 0, *ptr6 = 0, *ptr7 = 0, *ptr8 = 0;
  15.  
  16. int main()
  17. {
  18. int t, i, j, k, l, a, b, c, d, y, z, n;
  19. char ch;
  20.  
  21. ptr4 = (node *)malloc(sizeof(node));
  22. ptr4->x = -1919191;
  23. ptr4->prev = 0;
  24. ptr4->next = 0;
  25.  
  26.  
  27. scanf("%d", &t);
  28. getchar();
  29. for (i = 1; i <= t; i++)
  30. {
  31. ptr2 = ptr4;
  32. ch = getchar();
  33. getchar();
  34. scanf("%d", &n);
  35. getchar();
  36.  
  37. if (ch == 105)
  38. {
  39. printf("List: ");
  40. ptr->next = (node *)malloc(sizeof(node));
  41. ptr = ptr->next;
  42. ptr->x = n;
  43. ptr->next = 0;
  44. ptr->prev = ptr3;
  45. ptr3 = ptr;
  46.  
  47. ptr2 = ptr;
  48. for (; ;)
  49. {
  50. b = ptr2->x;
  51. ptr7 = ptr2->prev;
  52. if (ptr7->x == -1919191) break;
  53. a = ptr7->x;
  54. if (a > b)
  55. {
  56. ptr7->x = b;
  57. ptr2->x = a;
  58. }
  59. ptr2 = ptr2->prev;
  60. }
  61.  
  62. ptr2 = ptr4;
  63. while(true)
  64. {
  65. ptr2 = ptr2->next;
  66. printf("%d ", ptr2->x);
  67. if (ptr2->next == 0) break;
  68. }
  69. putchar(10);
  70. }
  71.  
  72. if (ch == 105)
  73. {
  74. printf("List: ");
  75. ptr2 = ptr4;
  76. for (; ;)
  77. {
  78.  
  79.  
  80. if (ptr2->x > n)
  81. {
  82. ptr8 = ptr2->prev;
  83. ptr = ptr8;
  84. ptr8->next = (node *)malloc(sizeof(node));
  85. ptr8 = ptr8->next;
  86. ptr8->x = n;
  87. ptr8->prev = ptr;
  88. ptr8->next = ptr2;
  89. break;
  90. }
  91.  
  92. if (ptr2->next == 0)
  93. {
  94. ptr8 = ptr2;
  95. ptr2->next = (node *)malloc(sizeof(node));
  96. ptr2 = ptr2->next;
  97. ptr2->x = n;
  98. ptr2->next = 0;
  99. ptr2->prev = ptr8;
  100. break;
  101. }
  102.  
  103. ptr2 = ptr2->next;
  104. }
  105.  
  106.  
  107. ptr2 = ptr4;
  108. while(true)
  109. {
  110. ptr2 = ptr2->next;
  111. printf("%d ", ptr2->x);
  112. if (ptr2->next == 0) break;
  113. }
  114. putchar(10);
  115. }
  116.  
  117. if (ch == 100)
  118. {
  119. d = 0;
  120. for (; ;)
  121. {
  122. ptr2 = ptr2->next;
  123. if (ptr2->x == n)
  124. {
  125. printf("%d Deleted\n", n);
  126. ptr5 = ptr2->prev;
  127. ptr6 = ptr5;
  128. ptr5->next = ptr2->next;
  129. ptr5 = ptr5->next;
  130. ptr5->prev = ptr6;
  131. d = 1;
  132. break;
  133. }
  134. if (ptr2->next == 0) break;
  135. }
  136. if (d == 0) printf("%d Not Found\n", n);
  137. }
  138.  
  139. if (ch == 115)
  140. {
  141. ptr2 = ptr4, d = 0;
  142. for (; ;)
  143. {
  144. ptr2 = ptr2->next;
  145. if (ptr2->x == n)
  146. {
  147. printf("%d Found\n", n);
  148. d = 1;
  149. break;
  150. }
  151. if (ptr2->next == 0) break;
  152. }
  153. if (d == 0) printf("%d Not Found\n", n);
  154. }
  155. }
  156. return 0x0;
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement