kokokozhina

dynamic_21

Apr 5th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 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 = t;
  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 = h;
  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 = t;
  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_after(list *&h, list *&t, list *&r, int x){//?
  77. list* p = new list;
  78. p->inf = x;
  79. p->prev = r;
  80. p->next = r->next;
  81. if(r == t){
  82. t->next = p;
  83. r = p;//?
  84. }
  85. else{
  86. (r->next)->prev = p;
  87. r->next = p;
  88. }
  89. }
  90.  
  91. void erase(list *&h, list *&t, list *&r){
  92. if(r == h && r == t)
  93. h = t = NULL;
  94. else if(r == h){
  95. h = h->next;
  96. t->next = h;
  97. }
  98. else if(r == t){
  99. t = t->prev;
  100. t->next = h;
  101. }
  102. else{
  103. (r->prev)->next = r->next;
  104. (r->next)->prev = r->prev;
  105. }
  106. delete r;
  107. }
  108. //release the Iosif's task
  109. int main()
  110. {
  111. #ifdef _DEBUG
  112. freopen("input.txt", "r", stdin);
  113. freopen("output.txt", "w", stdout);
  114. #endif
  115. printf("Enter the number of elements and value of K, then array of numbers\n");
  116. int n, k; scanf("%d%d", &n, &k);
  117. list *h, *t;
  118. list *ansh, *anst;
  119. init(ansh, anst);
  120. init(h, t);
  121. for(int i = 0; i < n; i++){
  122. int cur; scanf("%d", &cur);
  123. add_back(h, t, cur);
  124. }
  125. list *p = t;
  126. while(p->next != p->prev){
  127. for(int i = 0; i < k; i++)
  128. p = p->next;
  129. list *aux = p->prev;
  130. printf("%d\n", p->inf);
  131. erase(h, t, p);
  132. p = aux;
  133. }
  134. printf("%d\n", p->inf);
  135. return 0;
  136. }
Add Comment
Please, Sign In to add comment