PAT1024 强行用链表写了一发。

主要的思想还是 上课的那个PPT上面的 链表反转的思想。
然后加一点七七八八的 递推。
一层一层往下翻转就好啦。
1A 真开心。

代码:http://paste.ubuntu.net/16506799/

#include <cstdio>
#include <iostream>
using namespace std;
struct node
{
    int address;
    int data;
    struct node* next;
};
struct pnode
{
    int address;
    int data;
    int next;
}p[1000005];
node *head=new node();
int fist,n,k,l=0;
void insert(node *p1,node *p2)
{
    p2->next=p1->next;
    p1->next=p2;
    return ;
}
node * del(node *p)
{
    node *t=p->next;
    p->next=t->next;
    return t;
}
void change (node *temp,int s)
{
    int cnt=1;
    if(s==0)return ;
    node *p=temp,*t;
    p=p->next;
    while(cnt<k)
    {
        cnt++;
        t=del(p);
        insert(temp,t);
    }
    change(p,s-1);

}
void creat()
{
    //head=NULL;
    head->next=NULL;
    node *p1=new node();
    p1=head;
    while(fist!=-1)
    {
        l++;
        node *p2=new node();
        p2->address=p[fist].address;
        p2->data=p[fist].data;
        insert(p1,p2);
        p1=p2;
        fist=p[fist].next;
    }
    change(head,l/k);
    return ;
}
void print()
{
    head=head->next;
    while(head->next!=NULL)
    {
        printf("%05d %d %05d
",head->address,head->data,head->next->address );
        head=head->next;
    }
    printf("%05d %d -1",head->address,head->data );
}
int main()
{
    
    cin>>fist>>n>>k;
    int i;
    for(i=0;i<n;i++)
    {
        int ad,da,ne;
        scanf("%d%d%d",&ad,&da,&ne);
        p[ad].address=ad;
        p[ad].data=da;
        p[ad].next=ne;
    }
    creat();
    print();
   return 0;
}

原文地址:https://www.cnblogs.com/chs97/p/5510346.html