#include<stdio.h>
#include<string.h>
#include<time.h>
#include<stdlib.h>

typedef struct NodeT{
    int phone;
    char *str;
    int bh;
    struct NodeT *hnext;
    struct NodeT *pnext;
}list;

typedef struct 
{
    int pageCount;//共多少页
    int count;//共多少条
    int item;//每页多少条
    int currentPage;//当前页
}Page;

void file_read();
char *get_str();
void get_on();
void begin(list **,int currentPage,int item,list **,list **);
void get_tail(int,int ,list ***,list ***);
void get_head(int,int ,list ***,list ***);

int main(){
    list *l;
    list *head=NULL;
    list *tail=NULL;
    Page ptr;
    int cnt;
    cnt=rand();
    ptr.count=cnt;
    ptr.item=10;
    ptr.currentPage=1;
    ptr.pageCount=cnt/10;
    if((ptr.pageCount*10)!=cnt)
        ptr.pageCount+=1;
    printf("%d\n",cnt);
    file_read(cnt,&l);    


    begin(&l,ptr.currentPage,ptr.item,&head,&tail);
    get_on(ptr.pageCount,ptr.currentPage,ptr.item,&head,&tail);

    return 0;
}

char *get_str(){
    char *str="hello";
    return str;
}


int get_bh(){
    static int bh=1;
    return bh++;
}


void file_read(int num,list **l){//读入内容
    list *node=NULL;
    list *top=NULL;
    list *end=NULL;
    int i;
    *l=node;
    //srand(time(0));
    for(i=1;i<=num;i++){
        node=(list *)malloc(sizeof(list));
        node->bh=get_bh();
        node->str=get_str();
        node->phone=rand();
        node->pnext=NULL;
        if(i==1){
            node->hnext=NULL;
            top=end=node;
            continue;
        }
        node->hnext=end;
        end->pnext=node;
        end=node;
    }
    *l=top;
}

void begin(list **l,int currentPage,int item,list **head,list **end){
    list *top=(*l);
    printf("第 %d 页\n",currentPage);
    (*head)=(*l);
    while(item){
        item--;
        printf("%d    %d      %s\n",top->bh,top->phone,top->str);
        top=top->pnext;
    }
    (*end)=top;
}


void get_on(int pageCount,int currentPage,int item,list **head,list **tail){
    int flag;
    while(scanf_s("%d",&flag)!=EOF){
    if(flag==1){
        if(pageCount==currentPage){
            printf("没有下一页\n");
            continue;
        }

        currentPage++;

        get_tail(currentPage,item,&head,&tail);
        /*    printf("%d    %d      %s\n",(*head)->bh,(*head)->phone,(*head)->str);
            printf("%d    %d      %s\n",(*tail)->bh,(*tail)->phone,(*tail)->str);*/
    
    }
    else{
        if(currentPage==1){
            printf("没有上一页\n");
            continue;
        }
        currentPage--;
        get_head(currentPage,item,&head,&tail);
        //get_tail( currentPage, item ,&head,&tail);
    }
    }
}

void get_tail(int currentPage,int item ,list *** head,list ***tail){
    list *top=(**tail);
    printf("第 %d 页\n",currentPage);
    (**head)=(**tail);
    while(item&&((top)!=NULL)){
        item--;
        printf("%d    %d      %s\n",top->bh,top->phone,top->str);
        if(top->pnext==NULL)
            break;
        top=top->pnext;
    }
    (**tail)=top;
}
void get_head(int currentPage,int item ,list *** head,list ***tail){
    list *top=(**head);
    list *tmp_head=NULL;
    list *tmp_tail=top;
    int tmp=item;
    (**tail)=(**head);
    while(item){
        item--;
        top=top->hnext;
    }
    (**head)=top;
    tmp_head=top;
    printf("第 %d 页\n",currentPage);
    while(tmp){
        tmp--;
        printf("%d    %d      %s\n",tmp_head->bh,tmp_head->phone,tmp_head->str);
        tmp_head=tmp_head->pnext;
    }
}