Reversing Linked List
来源:互联网 发布:redis hash key mysql 编辑:程序博客网 时间:2024/06/06 07:17
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 400000 4 9999900100 1 1230968237 6 -133218 3 0000099999 5 6823712309 2 33218
Sample Output:
00000 4 3321833218 3 1230912309 2 0010000100 1 9999999999 5 6823768237 6 -1
版本一:用链表的,但因为改变位置要遍历的原因,提交后时间超出;但功能完整
#include<iostream>
using namespace std;
struct link{
int number;
string begin;
string end;
link* next;
};
struct format{
string first;
int sum;
int k;
};
using list=link*;
format* readf()
{
string s; int i,j;
cin>>s>>i>>j;
format* f;
f=new format;
f->first=s; f->sum=i; f->k=j;
return f;
}
void attach(int n,string b,string e,list* li)
{
list m=new link;
m->number=n; m->begin=b; m->end=e; m->next=NULL;
(*li)->next=m;
*li=(*li)->next;
}
list readl(format*f)
{
int sum=f->sum;
string first=f->first;
list s=new link;
list rear=s;
while(sum--){
int n;
string b,e;
cin>>b>>n>>e;
attach(n,b,e,&rear);
}
list start=new link;
list p=start; rear=s->next;
while(rear->begin!=first) rear=rear->next;
attach(rear->number,rear->begin,rear->end,&p);
while(p->end!="-1"){
rear=s->next;
while(rear->begin!=p->end){
rear=rear->next;
}
attach(rear->number,rear->begin,rear->end,&p);
}
list temp=start;
start=start->next;
free(temp);
return start;
}
list change(format* f,list li)
{
int i=f->k;int size=f->sum;
if(i==1||i>size)
return li;
else{
int j=size/i;
list s,rear;
s=new link; rear=s;
s->next=NULL;
while(j--){
int p=i;list note=rear;
while(p--){
if(p==i-1){
attach(li->number,li->begin,li->end,&rear);//cout<<"# "<<endl;
}
else{
list m=new link; m->number=li->number;m->begin=li->begin;m->end=li->end;
m->next=note->next;
note->next=m;
}
li=li->next;
}
}
while(li){
attach(li->number,li->begin,li->end,&rear);
li=li->next;
}
rear=s->next;
while(rear->next!=NULL){
rear->end=(rear->next)->begin;
rear=rear->next;
}
rear->end="-1";
list temp=s;
s=s->next;
free(temp);
return s;
}
}
void print(list li)
{
while(li){
cout<<li->begin<<" "<<li->number<<" "<<li->end<<endl;
li=li->next;
}
}
int main()
{
format* fm=readf();
list li=readl(fm);
li=change(fm,li);
print(li);
return 0;
}
版本二:调试了很久,思路是用顺序结构来省去遍历查找的时间,也就是“空间换取时间”;
#include<iostream>
using namespace std;struct Node{
int data;
int next;
};
Node pointer[100001];
int s;
int tag=1;
int last,first;
int reverse(int now,int k){
int m,n,temp;temp=now;
n=m=pointer[now].next;
while(--k){
n=pointer[m].next;
pointer[m].next=now;
pointer[temp].next=n;
now=m; m=n;
}
if(tag!=1){
first=now;
pointer[last].next=first;
}
if(tag==1){
s=now;
--tag;}
last=temp;
return n;
}
int main(){
int start,n,k,addr,data,next;
cin>>start>>n>>k;
while(n--){
cin>>addr>>data>>next;
pointer[addr].data=data; pointer[addr].next=next;
}
int length=1; int rear=start;
while(pointer[rear].next!=-1){
++length;
rear=pointer[rear].next;
}
int j=length/k; rear=start;
if(j==length||j==0){s=start;
}else
while(j--)
rear=reverse(rear,k);
while(pointer[s].next!=-1){
printf("%05d %d %05d\n",s,pointer[s].data,pointer[s].next);
s=pointer[s].next;
}
printf("%05d %d %d\n",s,pointer[s].data,pointer[s].next);
return 0;
}
- 1074. Reversing Linked List
- Reversing Linked List
- 1074.Reversing Linked List
- Reversing Linked List (25)
- Reversing Linked List
- Reversing Linked List
- Reversing Linked List (25)
- PAT:Reversing Linked List
- 1074. Reversing Linked List
- Reversing Linked List
- PTA Reversing Linked List
- Reversing Linked List
- PTA--Reversing Linked List
- 1074. Reversing Linked List
- 1074. Reversing Linked List
- 1074. Reversing Linked List
- Reversing Linked List
- PAT_1074. Reversing Linked List
- 【最小环 && 离散化】HDU 6005 Pandaland
- HPUACM Freshman Exercises Round 1 [标程]
- 【Android studio】为按钮添加简单事件
- 集合框架————————java视频
- ACM-11月12日周日周末训练心得
- Reversing Linked List
- L
- Mysql基础学习笔记
- 【考研】第十一周总结
- AtomicInteger的认识与使用
- Python---内置方法
- 习题4.3(2)
- Android启动篇 — init原理(1)
- php常用函数复习