Advertisement
kokokozhina

dynamic_15

Apr 5th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3.  
  4. using namespace std;
  5.  
  6. struct list{
  7. int inf;
  8. list* next;
  9. list* prev;
  10. };
  11.  
  12. void init(list *&h, list *&t){
  13. h = t = NULL;
  14. }
  15.  
  16. void print(list *&h, list *&t){
  17. list* p = h;
  18. while(p){
  19. cout << p->inf << " ";
  20. p = p->next;
  21. }
  22. }
  23.  
  24. void add_head(list *&h, list *&t, int x){
  25. list* p = new list;
  26. p->prev = NULL;
  27. p->inf = x;
  28. if(!t){
  29. p->next = NULL;
  30. h = t = p;
  31. }
  32. else{
  33. h->prev = p;
  34. p->prev = t;
  35. t = p;
  36. }
  37. }
  38.  
  39. void add_back(list *&h, list *&t, int x){
  40. list* p = new list;
  41. p->next = NULL;
  42. p->inf = x;
  43. if(!h){
  44. p->prev = NULL;
  45. h = t = p;
  46. }
  47. else{
  48. t->next = p;
  49. p->prev = t;
  50. t = p;
  51. }
  52. }
  53.  
  54. void del_all(list *&h, list *&t){
  55. while(h){
  56. list* p = h;
  57. if(p == t)
  58. h = t = NULL;
  59. else{
  60. h = h->next;
  61. h->prev = NULL;
  62. }
  63. delete p;
  64. }
  65. }
  66.  
  67. list* find(list *&h, list *&t, int x){
  68. list* p = h;
  69. while(p){
  70. if(p->inf == x) break;
  71. p = p->next;
  72. }
  73. return p;
  74. }
  75.  
  76. void insert_before(list *&h, list *&t, list *&r, int x){
  77. list* p = new list;
  78. p->inf = x;
  79. p->next = r;
  80. p->prev = r->prev;
  81. if(r == h){
  82. h->prev = p;
  83. h = p;//?
  84. }
  85. else{
  86. (r->prev)->next = p;
  87. r->prev = p;
  88. }
  89. }
  90.  
  91. int pop_front(list *&h, list *&t){
  92. int i = h->inf;
  93. list *r = h;
  94. h = h->next;
  95. if(h) h->prev = NULL;
  96. if(!h) t = NULL;
  97. delete r;
  98. return i;
  99. }
  100.  
  101. int pop_back(list *&h, list *&t){
  102. int i = t->inf;
  103. list *r = t;
  104. t = t->prev;
  105. if(t) t->next = NULL;
  106. if(!t) h = NULL;
  107. delete r;
  108. return i;
  109. }
  110.  
  111. void erase(list *&h, list *&t, list *&r){
  112. if(r == h && r == t)
  113. h = t = NULL;
  114. else if(r == h){
  115. h = h->next;
  116. t->next = NULL;
  117. }
  118. else if(r == t){
  119. t = t->prev;
  120. t->next = NULL;
  121. }
  122. else{
  123. (r->prev)->next = r->next;
  124. (r->next)->prev = r->prev;
  125. }
  126. delete r;
  127. }
  128. //insert new element before every minimum
  129. int main()
  130. {
  131. #ifdef _DEBUG
  132. freopen("input.txt", "r", stdin);
  133. freopen("output.txt", "w", stdout);
  134. #endif
  135. printf("Enter the number of elements and new member, then array of numbers\n");
  136. int n, k; scanf("%d%d", &n, &k);
  137. int mn = 2E9;
  138. list *h, *t;
  139. init(h, t);
  140. for(int i = 0; i < n; i++){
  141. int cur; scanf("%d", &cur);
  142. add_back(h, t, cur);
  143. if(cur < mn)
  144. mn = cur;
  145. }
  146. list *cur = h;
  147. for(int i = 0; i < n; i++){
  148. if(cur->inf == mn)
  149. insert_before(h, t, cur, k);
  150. cur = cur->next;
  151. }
  152. print(h, t);
  153. return 0;
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement