Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdio.h>
- using namespace std;
- struct list{
- int inf;
- list* next;
- list* prev;
- };
- void init(list *&h, list *&t){
- h = t = NULL;
- }
- void print(list *&h, list *&t){
- list* p = h;
- while(p){
- cout << p->inf << " ";
- p = p->next;
- }
- }
- void add_head(list *&h, list *&t, int x){
- list* p = new list;
- p->prev = t;
- p->inf = x;
- if(!t){
- p->next = NULL;
- h = t = p;
- }
- else{
- h->prev = p;
- p->prev = t;
- t = p;
- }
- }
- void add_back(list *&h, list *&t, int x){
- list* p = new list;
- p->next = h;
- p->inf = x;
- if(!h){
- p->prev = NULL;
- h = t = p;
- }
- else{
- t->next = p;
- p->prev = t;
- t = p;
- }
- }
- void del_all(list *&h, list *&t){
- while(h){
- list* p = h;
- if(p == t)
- h = t = NULL;
- else{
- h = h->next;
- h->prev = t;
- }
- delete p;
- }
- }
- list* find(list *&h, list *&t, int x){
- list* p = h;
- while(p){
- if(p->inf == x) break;
- p = p->next;
- }
- return p;
- }
- void insert_after(list *&h, list *&t, list *&r, int x){//?
- list* p = new list;
- p->inf = x;
- p->prev = r;
- p->next = r->next;
- if(r == t){
- t->next = p;
- r = p;//?
- }
- else{
- (r->next)->prev = p;
- r->next = p;
- }
- }
- void erase(list *&h, list *&t, list *&r){
- if(r == h && r == t)
- h = t = NULL;
- else if(r == h){
- h = h->next;
- t->next = h;
- }
- else if(r == t){
- t = t->prev;
- t->next = h;
- }
- else{
- (r->prev)->next = r->next;
- (r->next)->prev = r->prev;
- }
- delete r;
- }
- //release the Iosif's task
- int main()
- {
- #ifdef _DEBUG
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- #endif
- printf("Enter the number of elements and value of K, then array of numbers\n");
- int n, k; scanf("%d%d", &n, &k);
- list *h, *t;
- list *ansh, *anst;
- init(ansh, anst);
- init(h, t);
- for(int i = 0; i < n; i++){
- int cur; scanf("%d", &cur);
- add_back(h, t, cur);
- }
- list *p = t;
- while(p->next != p->prev){
- for(int i = 0; i < k; i++)
- p = p->next;
- list *aux = p->prev;
- printf("%d\n", p->inf);
- erase(h, t, p);
- p = aux;
- }
- printf("%d\n", p->inf);
- return 0;
- }
Add Comment
Please, Sign In to add comment