02-线性结构3 Reversing Linked List

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 (≤10?5??) 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\n",s,pointer[s].data,pointer[s].next);
54 s=pointer[s].next;
55 }
56 printf("%05d %d %d\n",s,pointer[s].data,pointer[s].next);
57 return 0;
58 }

时间: 2024-08-25 14:11:38

02-线性结构3 Reversing Linked List的相关文章

[PAT] 02-线性结构2 Reversing Linked List(单向链表的逆转) - C语言实现

今天突然想起自己的cnblog有差不多一年没更了??放一道很久前做的也写好了很久但是一直忘记发布的题.如果有不同的算法欢迎分享~ [PAT]02-线性结构2 Reversing Linked List   (25分) Given a constant KK and a singly linked list LL, you are supposed to reverse the links of every KK elements on LL. For example, given LL bein

pat02-线性结构1. Reversing Linked List (25)

02-线性结构1. Reversing Linked List (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L. For example, given L being 1→2→3→4→

02-线性结构3 Reversing Linked List (25 分)

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 K elements 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 ou

02-线性结构2 Reversing Linked List

由于最近学的是线性结构,且因数组需开辟的空间太大.因此这里用的是纯链表实现的这个链表翻转. Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements 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,

数据结构练习 02-线性结构2. Reversing Linked List (25)

Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements 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 Specificat

PAT 02-线性结构1. Reversing Linked List

Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements 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 Specificat

Reversing Linked List

题目来源:PTA02-线性结构2 Reversing Linked List   (25分) Question:Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements 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; i

PAT线性结构_一元多项式求导、按给定步长反转链表、出栈序列存在性判断

02-线性结构1. 一元多项式求导 (25) 设计函数求一元多项式的导数.(注:xn(n为整数)的一阶导数为n*xn-1.) 输入格式:以指数递降方式输入多项式非零项系数和指数(绝对值均为不超过1000的整数).数字间以空格分隔. 输出格式:以与输入相同的格式输出导数多项式非零项的系数和指数.数字间以空格分隔,但结尾不能有多余空格.注意“零多项式”的指数和系数都是0,但是表示为“0 0”. 输入样例: 3 4 -5 2 6 1 -2 0 输出样例: 12 3 -10 1 6 0 最简单的方式是用

线性结构和非线性结构

线性结构和非线性结构同属于数据结构中的逻辑结构类型 线性结构是指该结构中的节点之间存在一对一的关系.其特点是开始节点和终端节点都是唯一的,除了开始节点和终端节点外,其余节点都有且仅有一个直接前驱,有且仅有一个直接后继.此类型的存储结构有:顺序表(数组).链表.堆栈结构.队列结构等 非线性结构又包括集合.树形结构.图形结构或网状结构,特点是数据元素之间存在一个对多个或多个对多个的关系,其中集合是一种关系极为松散的结构.