Warriorscat

class

May 27th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.43 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. class List_Node
  5. {
  6. public:
  7.  
  8. int info;
  9. List_Node *next, *prev;
  10.  
  11. List_Node(int data)
  12. {
  13. this->info = data;
  14. this->next = this->prev = this;
  15. }
  16. ~List_Node()
  17. {
  18. delete this;
  19. }
  20. };
  21.  
  22. class List
  23. {
  24. List_Node *start, *finish;
  25.  
  26. public:
  27.  
  28. void push_back_element(int data)
  29. {
  30. if(start == NULL)
  31. {
  32. start = finish = new List_Node(data);
  33. return;
  34. }
  35.  
  36. List_Node *tmp = new List_Node(data);
  37. tmp->next = start;
  38. tmp->prev = finish;
  39. start->prev = tmp;
  40. finish->next = tmp;
  41. finish = tmp;
  42. }
  43.  
  44. void insert_element_after(List_Node* &tmp, int data)
  45. {
  46. List_Node *aux = new List_Node(data);
  47. if(tmp == finish)
  48. {
  49. aux->next = tmp->next;
  50. aux->prev = tmp;
  51. tmp->next->prev = aux;
  52. tmp->next = aux;
  53. finish = aux;
  54. }
  55. else
  56. {
  57. aux->next = tmp->next;
  58. aux->prev = tmp;
  59. tmp->next->prev = aux;
  60. tmp->next = aux;
  61. }
  62. }
  63.  
  64. void insert_element_before(List_Node* &tmp, int data)
  65. {
  66. List_Node *aux = new List_Node(data);
  67. if(tmp == start)
  68. {
  69. aux->next = tmp;
  70. aux->prev = tmp->prev;
  71. tmp->prev->next = aux;
  72. tmp->prev = aux;
  73. start = aux;
  74. }
  75. else
  76. {
  77. aux->next = tmp;
  78. aux->prev = tmp->prev;
  79. tmp->prev->next = aux;
  80. tmp->prev = aux;
  81. }
  82. }
  83.  
  84. List_Node* search_element(int data)
  85. {
  86. List_Node *tmp = start;
  87. do
  88. {
  89. if(tmp->info == data)
  90. return tmp;
  91. tmp = tmp->next;
  92. }
  93. while(tmp != start);
  94.  
  95. return NULL;
  96. }
  97.  
  98. List_Node* search_specific_element(int data)
  99. {
  100. List_Node *tmp = start;
  101. do
  102. {
  103. if(tmp->info >= data)
  104. return tmp;
  105. tmp = tmp->next;
  106. }
  107. while(tmp != start);
  108.  
  109. return NULL;
  110. }
  111.  
  112. void delete_element(List_Node* &tmp)
  113. {
  114. if(tmp == start)
  115. {
  116. tmp->next->prev = finish;
  117. finish->next = tmp->next;
  118. start = tmp->next;
  119. }
  120. else if(tmp == finish)
  121. {
  122. tmp->prev->next = start;
  123. start->prev = tmp->prev;
  124. finish = tmp->prev;
  125. }
  126. else
  127. {
  128. tmp->next->prev = tmp->prev;
  129. tmp->prev->next = tmp->next;
  130. }
  131.  
  132. delete tmp;
  133. }
  134.  
  135. void sort_list()
  136. {
  137. List_Node *tmp1 = start;
  138. do
  139. {
  140. List_Node *tmp2 = tmp1->next;
  141. do
  142. {
  143. if(tmp1->info > tmp2->info)
  144. swap(tmp1->info, tmp2->info);
  145. tmp2 = tmp2->next;
  146. }
  147. while(tmp2 != start);
  148. tmp1 = tmp1->next;
  149. }
  150. while(tmp1 != finish);
  151. }
  152.  
  153. void stable_insertion_element(int data)
  154. {
  155. if(start == NULL)
  156. {
  157. start = finish = new List_Node(data);
  158. return;
  159. }
  160.  
  161. //List_Node *tmp = new List_Node(data);
  162. if(data <= start->info)
  163. {
  164. insert_element_before(start, data);
  165. }
  166. else if(data >= finish->info)
  167. {
  168. insert_element_after(finish, data);
  169. }
  170. else
  171. {
  172. List_Node *tmp = search_specific_element(data);
  173. insert_element_before(tmp, data);
  174. }
  175. }
  176.  
  177. void display()
  178. {
  179. List_Node *tmp = start;
  180.  
  181. if(start)
  182. do
  183. {
  184. cout<< tmp->info <<" ";
  185. tmp = tmp->next;
  186. }
  187. while(tmp != start);
  188. }
  189.  
  190. ~List()
  191. {
  192. List_Node *tmp = start;
  193. do
  194. {
  195. List_Node *aux = tmp->next;
  196. delete_element(tmp);
  197. tmp = aux;
  198. }
  199. while(tmp != start);
  200. delete start;
  201. cout<< "\nList was deleted!\n";
  202. }
  203.  
  204. };
  205.  
  206. int n, x;
  207.  
  208. int main()
  209. {
  210. List *s = new List();
  211. cin>> n;
  212. for(int i = 1; i <= n; i++)
  213. cin>> x, s->stable_insertion_element(x);
  214. s->display();
  215.  
  216. return 0;
  217. }
Add Comment
Please, Sign In to add comment