PTA 02-线性结构3 Reversing Linked List

PTA 02-线性结构3 Reversing Linked List 周期性逆转链表 在编译器上没问题,但PTA不过审核,有没有大哥帮忙看看 #include <stdio.h> #define N 100001 struct LNode { int Data; int Next; }; struct LNode a[N]; void read(int Datacount); int reverse(int FirstAddress); void Pr(int RFirstAddress); int main() { int FirstAddre... 阅读详情

这题的意思是 第一行输入首地址 、N、K 下面接着N行,每行包括首地址、数字、尾地址,首地址对应前一个元素的尾地址, 注意这里不一定是按顺序来的 所以需要你自己排好,然后把从第一个开始,每K个为一组,按顺序将每一组倒序输出,********注意输出后的首地址和尾地址也要前后对应*********


这题我写了整整一天 脑子都要炸了   。。。

代码修修补补终于还是ac了。

但是改了这么多次之后可读性变的非常差,我自己都不想看- - 我是用链表写的  听说用顺序表写会简单很多,下次有时间再用顺序表写一遍吧!溜了溜了,头都要炸了。。。


我的方法是读入数据之后排序排好,然后根据K将链表中的前K个放入堆栈,放完K个之后输出,再放入,循环,直到放完了


其实有很多细节也要注意


轻喷 0.0

惨不忍睹的代码:


#include<stdio.h>
#include<stdlib.h>
#include<string.h> 


typedef struct Node *List;
List Head;
char a[10];
int N,K;

struct Node{
	char head[10];
	int num;
	char end[10];
	List next;
};


List read(){									//读入数据 返回链表的首地址 
	List head,p;
	head=(List)malloc(sizeof(struct Node));
	head->next=NULL;
	p=head;

	scanf("%s %d %d",a,&N,&K);
	while(N--){
		char l[10],r[10];
		int num;
		scanf("%s %d %s",l,&num,r);
		List tmp=(List)malloc(sizeof(struct Node));
		tmp->next=NULL;
		strcpy(tmp->head,l);
		tmp->num=num;
		strcpy(tmp->end,r);
		p->next=tmp;
		p=p->next;
	}
	
	return head;
	
	
}


void Print(List L){														//输出链表 
	L=L->next;
	while(L){
		printf("%s %d %s\n",L->head,L->num,L->end);
		L=L->next;
	}
}


void sort(List L){						//给链表按顺序排好 
	List left,last,the;
	left=L;
	the=L->next;
	last=L;
	while(the){								//先排第一个 
		if(strcmp(the->head,a)==0){
			if(last==L)								//已经是第一位  不用换位置 
				break;
			else{									//排到第一位
				last->next=the->next;					 
				the->next=left->next;
				left->next=the;
				break;
			}
		}else{
			last=last->next;
			the=the->next;
		}
	}
	

	
	left=left->next;
	
	while(strcmp(left->end,"-1")!=0){			//排之后的 
		last=left;
		the=left->next;
		while(the){
			if(strcmp(the->head,left->end)==0){
				if(last==left){
					left=left->next;
					break;
				}else{
					last->next=the->next;
					the->next=left->next;
					left->next=the;
					left=left->next;
					break;
				}
			}else{
				last=last->next;
				the=the->next;				
			}
		}
	}
	free(left->next);					//这里要注意把排不进去的结点删掉 不然之后会把这些无关的节点也输出 
	left->next=NULL;
	return;
	
}


void push(List L,List ele){							//将排好的链表放入堆栈中 
	List tmp;
	tmp=(List)malloc(sizeof(struct Node));
	*tmp=*ele;								//注意结构体赋值 
	tmp->next=L->next;
	L->next=tmp;
	return;
}

void pop(List L){								//从堆栈中输出 
	if(L->next==NULL)
		return;
	List r=L->next;
	printf("%s %d %s\n",r->head,r->num,r->end);
	L->next=r->next;
	
	return;
	
}


int main(){
	int i=0,j;

	Head=read();
	
	sort(Head);

	List zhan;
	zhan=(List)malloc(sizeof(struct Node));
	zhan->next=NULL;
	
	Head=Head->next;
	List done=Head;
	while(Head){
//		for(i=0;i<K;i++){
//			push(zhan,Head);
//			Head=Head->next;
//		}
		push(zhan,Head);
		Head=Head->next;
		i++;
		if(i==K){
			List S;
			S=zhan->next;
			while(S->next!=NULL){				

				
					List SS=S->next; 
					strcpy(S->end,SS->head);
					S=S->next;					
				

			}
			if(S->next==NULL&&Head!=NULL){
				List cycle=Head;
				for(j=1;j<K;j++){
					if(cycle!=NULL){
						cycle=cycle->next;
					}else
						break;
				}
				if(cycle==NULL)
					strcpy(S->end,Head->head);
				else
					strcpy(S->end,cycle->head);
			}
				
			if(Head==NULL)
				strcpy(S->end,"-1");
			done=Head;
			while(i--)
				pop(zhan);	
			i=0;		
		}

			
	}
	
	while(done){
		printf("%s %d %s\n",done->head,done->num,done->end);
		done=done->next;		
	}


	return 0;
} 


数据结构——(PTA02-线性结构3 Reversing Linked List 02-线性结构3 Reversing Linked List——链表分段逆转 阅读详情

相关推荐

[PTA刷题]Reversing Linked List

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 being 1→2→3→4→5→6, if K = 3K=3, then you must outpu

黑客与画家 1357

pta Reversing Linked List 题解

7-1 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...

qq_40409416的博客 560

PTAReversing Linked List

#include <stdio.h> #include <stdlib.h> struct Node { int Address; int Next_Address; Node* Next; int Data; }; typedef Node* List; List Insert(List L,int address,int data,int next_address){ List tmp=new Node; tmp-&...

fftx_00的博客 417

PTA 02-线性结构3 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,...

zhuiyisinian的博客 1941

PTA 02-线性结构3 Reversing Linked List

02-线性结构3 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 out

viVi_beea的博客 345

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

02-线性结构3 Reversing Linked List(25 point(s))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...

ckxkobe的博客 1024

PTA-02-线性结构3 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,...

sharemon的博客 272

PTA || 02-线性结构3 Reversing Linked List

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...

xuuyann 857

逆转链表-pta02-线性结构3 Reversing Linked List

题目链接 题目 难点分析 1.链表结点如何逆转 用三个指针new_head、old_head,tmp。之所以要用三个,是因为除了逆转的两个结点外,还需要保存下一个待逆转的结点,不保存的话逆转之后就找不到了。 另外因为不是N个结点全部逆转,而是要一次逆转K个,所以需要保存上一次逆转之后的尾结点LastRear和这一次逆转之后的尾结点rear。需要把lastRear的指针指向当前逆转好的序列的头

bobo1356的博客 574

PTA数据结构-02-线性结构3 Reversing Linked List

一、题目 Given a constantKand a singly linked listL, you are supposed to reverse the links of everyKelements onL. For example, givenLbeing 1→2→3→4→5→6, ifK=3, then you must output 3→2→1→6→5→4; i...

hdu_ch的博客 302

链表翻转——PTA——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, the...

qq_43457051的博客 217

PTA-数据结构 编程题02-线性结构3 Reversing Linked List (25 分)

PTA-数据结构 编程题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,...

qq_43683133的博客 578

PTA 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,...

abc15766228491的博客 1743

[pta]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 LLL being 1→2→3→4→5→6, if K=3,

Gold_Experience的博客 1514

数据结构 PTA 02-线性结构3 Reversing Linked List (25分) C语言实现

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,...

卡卡 582

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

题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/664 5-2Reversing Linked List(25分) Given a constantKKand a singly linked listLL, you are supposed to reverse the links of everyK...

weixin_30872867的博客 117
上一篇: ZOJ Problem Set - 3782 Ternary Calculation
下一篇: PTA 02-线性结构4 Pop Sequence
SiCheng_Z
博客等级 码龄9年 0粉丝 8原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值