PAT 浙大数据结构(Reversing Linked List)

来源:互联网 发布:mac excel怎么加 编辑:程序博客网 时间:2024/05/29 15:23

按要求反转单链表

Given a constant KKK and a singly linked list LLL, you are supposed to reverse the links of every KKK elements on LLL. For example, given LLL being 1→2→3→4→5→6, if K=3K = 3K=3, then you must output 3→2→1→6→5→4; if K=4K = 4K=4, you must output 4→3→2→1→5→6.
Input Specification:

Each input file contains one test case. For each case, the first line contains the address of the first node, a positive NNN (≤105\le 10^5≤10​5​​) which is the total number of nodes, and a positive KKK (≤N\le N≤N) which is the length of the sublist to be reversed. The address of a node is a 5-digit nonnegative integer, and NULL is represented by -1.

Then NNN lines follow, each describes a node in the format:

Address Data Next

where Address is the position of the node, Data is an integer, and Next is the position of the next node.
Output Specification:

For each case, output the resulting ordered linked list. Each node occupies a line, and is printed in the same format as in the input.
Sample Input:

00100 6 4
00000 4 99999
00100 1 12309
68237 6 -1
33218 3 00000
99999 5 68237
12309 2 33218

Sample Output:

00000 4 33218
33218 3 12309
12309 2 00100
00100 1 99999
99999 5 68237
68237 6 -1

反转一次

下面的代码中,first表示的是new即反转结点的前一个结点指针,second表示的是old即正在反转的结点指针,third表示的是tmp即反转结点的下一个结点指针.

下面是具体思路过程.
这里写图片描述步骤1
步骤2

代码实现:

#include <iostream>#include <cstring>#define Max 100000using namespace std;struct Node{    char address[6];   long order;    char address_[6];    Node *next;};Node* InitLink();long Size(Node *head);Node* Transfer(Node *head,long circle,long length){   long n,i,cnt;   n=length/circle;   Node *first,*second,*third,*p,*s;   p=head;   first=head->next;   second=first->next;   for(i=0;i<n;i++)  {       cnt=1;       while(cnt<circle)    {       third=second->next;      // strcpy(third->address_,second->address)       second->next=first;       first=second;       second=third;       cnt++;   }    if(i==n-1) {        p->next->next=second;        p->next=first;        break;    }        s=p->next;        p->next->next=second;        p->next=first;        p=s;        first=second;       second=second->next;  }  return head;}void ChangeAddress_(Node *head){     Node *p=head->next,*q=head->next;     while(q->next)     {    q=q->next;     strcpy(p->address_,q->address);          p=p->next;     }    strcpy(p->address_,"-1");    return ;}void ShowLink(Node *head);int main(){    Node *p[Max];    Node *head,*q;    head=InitLink();  q=head;    long amount,circle,i,length;    char addr[6];    cin>>addr>>amount>>circle;    strcpy(q->address_,addr);    for(i=0;i<amount;i++)        {            p[i]=new Node;            cin>>p[i]->address>>p[i]->order>>p[i]->address_;        }    for(i=0;i<amount;)    {       if(strcmp(q->address_,p[i]->address)==0)        { q->next=p[i];          p[i]->next=NULL;          q=p[i];        i=0;        }        else i++;        if(strcmp(q->address_,"-1")==0) break;    }    //cout<<"显示创建的链表:"<<endl;   // ShowLink(head);    length=Size(head);    head=Transfer(head,circle,length);    ChangeAddress_(head) ;   // cout<<"翻转之后的链表:"<<endl;    ShowLink(head);    return 0;}Node* InitLink(){    Node *head;    head=new Node;    head->next=NULL;    return head;}long Size(Node *head){   long n=0;  Node *p;  p=head->next;  while(p)  {      n++;      p=p->next;  }  return n;}void ShowLink(Node *head){    Node *p;    p=head->next;    while(p)    {       cout<<p->address<<" "<<p->order<<" "<<p->address_<<endl;       p=p->next;    }    return ;}

遗憾的是,由于水平有限,当N取到10^5时,程序超时,等待优化,也恳请各路大神指教.拜谢.

0 0
原创粉丝点击