Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.4 kB
1
Indexable
Never
#include <stdio.h>
#include <stdlib.h>
//#include "function.h"


int main(){

    int n, k;

    while(scanf("%d%d", &n, &k)!=EOF){
        head = createList(n);
        solve(n, k);
    }

    return 0;
}

#ifndef FUNC_H_INCLUDED
#define FUNC_H_INCLUDED

typedef struct _Node{
    int number;
    struct _Node* prev;
    struct _Node* next;
}Node;

Node* head;
int a;
Node* createList(int n){
    if(a==0){
        head=(Node*)malloc(sizeof(Node)*n);
        a=1;
        for(int i=0;i<n;i++){
            head[i].number=i+1;
            if(i!=0) head[i]->prev=head[i];
            else head[i].prev=NULL;
            if(i!=n-1) head[i]->next=head[i+2];
            else head[i].next=NULL;
        }
        return head;
    }
    else{
        a=2;
    }

}
void solve(int n, int m){
    Node* go=head,*yoyo=head;
    if(a!=1){
        if(n==1){
            head=head->next;
            head->prev=NULL;
        }
        while(m!=0){
            if(yoyo->next==NULL) break;
            yoyo=yoyo->next;
        }
        go->prev=yoyo;
        go->next=yoyo->next;
        if(yoyo->next!=NULL) yoyo->next->prev=go;
        yoyo->next=go;
        go=head;
    }
    for(int i=0;i<n-1;i++){
        printf("%d ",go->number);
        go=go->next;
    }
    printf("%d\n",go->number); 
}

#endif