Advertisement
kokokozhina

Untitled

Apr 5th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 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. //list *cur = h;
  19. do{
  20. cout << p->inf << " ";
  21. p = p->next;
  22. }
  23. while(p!= h);
  24. }
  25.  
  26. void add_head(list *&h, list *&t, int x){
  27. list* p = new list;
  28. p->prev = t;
  29. p->inf = x;
  30. if(!t){
  31. p->next = NULL;
  32. h = t = p;
  33. }
  34. else{
  35. h->prev = p;
  36. p->prev = t;
  37. t = p;
  38. }
  39. }
  40.  
  41. void add_back(list *&h, list *&t, int x){
  42. list* p = new list;
  43. p->next = NULL;
  44. p->inf = x;
  45. if(!h){
  46. p->prev = NULL;
  47. h = t = p;
  48. }
  49. else{
  50. t->next = p;
  51. p->prev = t;
  52. t = p;
  53. t->next = h;
  54. h->prev = t;
  55. }
  56. }
  57.  
  58. void del_all(list *&h, list *&t){
  59. while(h){
  60. list* p = h;
  61. if(p == t)
  62. h = t = NULL;
  63. else{
  64. h = h->next;
  65. h->prev = t;
  66. }
  67. delete p;
  68. }
  69. }
  70.  
  71. list* find(list *&h, list *&t, int x){
  72. list* p = h;
  73. while(p){
  74. if(p->inf == x) break;
  75. p = p->next;
  76. }
  77. return p;
  78. }
  79.  
  80. void insert_after(list *&h, list *&t, list *&r, int x){//?
  81. list* p = new list;
  82. p->inf = x;
  83. p->prev = r;
  84. p->next = r->next;
  85. if(r == t){
  86. t->next = p;
  87. r = p;//?
  88. }
  89. else{
  90. (r->next)->prev = p;
  91. r->next = p;
  92. }
  93. }
  94.  
  95. void erase(list *&h, list *&t, list *&r){
  96. if(r == h && r == t)
  97. h = t = NULL;
  98. else if(r == h){
  99. h = h->next;
  100. t->next = h;
  101. }
  102. else if(r == t){
  103. t = t->prev;
  104. t->next = h;
  105. }
  106. else{
  107. (r->prev)->next = r->next;
  108. (r->next)->prev = r->prev;
  109. }
  110. delete r;
  111. }
  112. //release the Iosif's task
  113. int main()
  114. {
  115. #ifdef _DEBUG
  116. freopen("input.txt", "r", stdin);
  117. freopen("output.txt", "w", stdout);
  118. #endif
  119. printf("Enter the number of elements and value of K, then array of numbers\n");
  120. int n, k; scanf("%d%d", &n, &k);
  121. list *h, *t;
  122. init(h, t);
  123. for(int i = 0; i < n; i++){
  124. int cur; scanf("%d", &cur);
  125. add_back(h, t, cur);
  126. }
  127. list *p = t;
  128. while(p->next != p->prev){
  129. for(int i = 0; i < k; i++)
  130. p = p->next;
  131. list *aux = p;
  132. p = p->next;
  133. cout << p->inf << endl;
  134. print(h, t);
  135. erase(h, t, aux);
  136. print(h, t);
  137. }
  138. printf("%d\n", p->inf);
  139. return 0;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement