02-线性结构3 Reversing Linked List(25 分)
Given a constant K and a singly linked list L, you are supposed to reverse the links of every Kelements on L. For example, given L being 1→2→3→4→5→6, if K=3, then you must output 3→2→1→6→5→4; if K=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 N (≤105) which is the total number of nodes, and a positive K (≤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 N 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
版本一:用链表的,但因为改变位置要遍历的原因,提交后时间超出;但功能完整
1 #include<iostream> 2 using namespace std; 3 struct link{ 4 int number; 5 string begin; 6 string end; 7 link* next; 8 }; 9 struct format{ 10 string first; 11 int sum; 12 int k; 13 }; 14 using list=link*; 15 format* readf() 16 { 17 string s; int i,j; 18 cin>>s>>i>>j; 19 format* f; 20 f=new format; 21 f->first=s; f->sum=i; f->k=j; 22 return f; 23 } 24 void attach(int n,string b,string e,list* li) 25 { 26 list m=new link; 27 m->number=n; m->begin=b; m->end=e; m->next=NULL; 28 (*li)->next=m; 29 *li=(*li)->next; 30 } 31 list readl(format*f) 32 { 33 int sum=f->sum; 34 string first=f->first; 35 list s=new link; 36 list rear=s; 37 while(sum--){ 38 int n; 39 string b,e; 40 cin>>b>>n>>e; 41 attach(n,b,e,&rear); 42 } 43 list start=new link; 44 list p=start; rear=s->next; 45 while(rear->begin!=first) rear=rear->next; 46 attach(rear->number,rear->begin,rear->end,&p); 47 while(p->end!="-1"){ 48 rear=s->next; 49 while(rear->begin!=p->end){ 50 rear=rear->next; 51 } 52 attach(rear->number,rear->begin,rear->end,&p); 53 } 54 list temp=start; 55 start=start->next; 56 free(temp); 57 return start; 58 } 59 list change(format* f,list li) 60 { 61 int i=f->k;int size=f->sum; 62 if(i==1||i>size) 63 return li; 64 else{ 65 int j=size/i; 66 list s,rear; 67 s=new link; rear=s; 68 s->next=NULL; 69 while(j--){ 70 int p=i;list note=rear; 71 while(p--){ 72 if(p==i-1){ 73 attach(li->number,li->begin,li->end,&rear);//cout<<"# "<<endl; 74 } 75 else{ 76 list m=new link; m->number=li->number;m->begin=li->begin;m->end=li->end; 77 m->next=note->next; 78 note->next=m; 79 } 80 li=li->next; 81 } 82 } 83 while(li){ 84 attach(li->number,li->begin,li->end,&rear); 85 li=li->next; 86 } 87 rear=s->next; 88 while(rear->next!=NULL){ 89 rear->end=(rear->next)->begin; 90 rear=rear->next; 91 } 92 rear->end="-1"; 93 list temp=s; 94 s=s->next; 95 free(temp); 96 return s; 97 } 98 99 } 100 void print(list li) 101 { 102 while(li){ 103 cout<<li->begin<<" "<<li->number<<" "<<li->end<<endl; 104 li=li->next; 105 } 106 } 107 int main() 108 { 109 format* fm=readf(); 110 list li=readl(fm); 111 li=change(fm,li); 112 print(li); 113 return 0; 114 }
版本二:调试了很久,思路是用顺序结构来省去遍历查找的时间,也就是“空间换取时间”;
1 #include<iostream> 2 3 using namespace std; 4 struct Node{ 5 int data; 6 int next; 7 }; 8 Node pointer[100001]; 9 int s; 10 int tag=1; 11 int last,first; 12 int reverse(int now,int k){ 13 14 15 int m,n,temp;temp=now; 16 n=m=pointer[now].next; 17 while(--k){ 18 n=pointer[m].next; 19 pointer[m].next=now; 20 pointer[temp].next=n; 21 now=m; m=n; 22 } 23 if(tag!=1){ 24 first=now; 25 pointer[last].next=first; 26 } 27 if(tag==1){ 28 s=now; 29 --tag;} 30 last=temp; 31 return n; 32 } 33 int main(){ 34 35 36 int start,n,k,addr,data,next; 37 cin>>start>>n>>k; 38 while(n--){ 39 cin>>addr>>data>>next; 40 pointer[addr].data=data; pointer[addr].next=next; 41 } 42 int length=1; int rear=start; 43 while(pointer[rear].next!=-1){ 44 ++length; 45 rear=pointer[rear].next; 46 } 47 int j=length/k; rear=start; 48 if(j==length||j==0){s=start; 49 }else 50 while(j--) 51 rear=reverse(rear,k); 52 while(pointer[s].next!=-1){ 53 printf("%05d %d %05d ",s,pointer[s].data,pointer[s].next); 54 s=pointer[s].next; 55 } 56 printf("%05d %d %d ",s,pointer[s].data,pointer[s].next); 57 return 0; 58 }