Advertisement
kokokozhina

dnmc_21_new

Apr 8th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.69 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_back(list *&h, list *&t, int x){
  25.     list* p = new list;
  26.     p->next = h;
  27.     p->inf = x;
  28.     if(!h){
  29.         p->prev = p;
  30.         p->next = p;
  31.         h = t = p;
  32.     }
  33.     else{
  34.         t->next = p;
  35.         p->prev = t;
  36.         t = p;
  37.         h->prev = t;
  38.     }
  39. }
  40.  
  41. void erase(list *&h, list *&t, list *&r){
  42.     if(r == h && r == t)
  43.         h = t = NULL;
  44.      else if(r == h){
  45.             h = h->next;
  46.             t->next = h;
  47.             h->prev = t;
  48.         }
  49.         else if(r == t){
  50.             t = t->prev;
  51.             t->next = h;
  52.             h->prev = t;
  53.         }
  54.         else{
  55.             (r->prev)->next = r->next;
  56.             (r->next)->prev = r->prev;
  57.         }
  58.     delete r;
  59. }
  60. //release the Iosif's task
  61. int main()
  62. {
  63. #ifdef _DEBUG
  64.     freopen("input.txt", "r", stdin);
  65.     freopen("output.txt", "w", stdout);
  66. #endif
  67.     printf("Enter the number of elements and value of K, then array of numbers\n");
  68.     int n, k; scanf("%d%d", &n, &k);
  69.     list *h, *t;
  70.     init(h, t);
  71.     for(int i = 0; i < n; i++){
  72.         int cur; scanf("%d", &cur);
  73.         add_back(h, t, cur);
  74.     }
  75.     list *p = t;
  76.    
  77.     for(int i = 0; i < n-1; i++){
  78.         for(int i = 0; i < k; i++)
  79.           p = p->next;
  80.         list* aux = p;
  81.         p = p->prev;
  82.         erase(h, t, aux);
  83.     }
  84.  
  85.     printf("Here is answer: %d\n", p->inf);
  86.    
  87.     return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement