首页 > 学院 > 开发设计 > 正文

PAT甲级1074

2019-11-10 21:58:41
字体:
来源:转载
供稿:网友

1074. Reversing Linked List (25)

时间限制400 ms内存限制65536 kB代码长度限制16000 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→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 33218Sample Output:
00000 4 3321833218 3 1230912309 2 0010000100 1 9999999999 5 6823768237 6 -1
#include<stdio.h>#include<algorithm>#include<stack>using namespace std;struct Node{	int data;	int next;}node[100000];//定义静态链表节点struct record{	int address;	//int next;};//用于反转链表int main(){	for (int i = 0; i < 100000; i++)	{		node[i].data = 0;		node[i].next = -1;	}//初始化	int head, N, K;	scanf("%d%d%d", &head, &N, &K);	int address, data, next;	while (N--)	{		scanf("%d%d%d", &address, &data, &next);		node[address].data = data;		node[address].next = next;//链接各节点	}	int count = 0;//计数器	int addressNow = head;	int pre;	stack<record> sta;//用于暂存要被逆转连接的节点	record temprecord;//保存节点地址,本来以为要保存下一个节点的地址,其实根本不需要,也不想改了就这样吧	int oldLast;//上一次逆转后指向最后一个节点的指针	while (addressNow!= -1)	{		temprecord.address = addressNow;		//temprecord.next = node[addressNow].next;		sta.push(temprecord);		pre = addressNow;		addressNow = node[addressNow].next;		count++;		if (count%K == 0)		{					if (count == K)					head = pre;//第一个开始反转的节点成为头了				if (!sta.empty())				{					sta.pop();				}				if (pre != head)				{					node[oldLast].next = pre;				}//如果不是第一个那么需要将上一次反转后的最后一个节点的下个地址做修改				//将样例中的K由4改为2你就知道为什么了			for (int i = 0; i < K-1; i++)			{				node[pre].next = sta.top().address;//逆转链表				pre = sta.top().address;				if (!sta.empty())				{					sta.pop();				}			}			node[pre].next = addressNow;//修改反转节点的最后一个节点的下一个地址,			//若后面不会再反转,那么就是当前扫描的节点的地址,注意我是先计数后又把指针			//往后移一位了			oldLast = pre;//保存反转节点的最后一个节点的地址		}	}	addressNow = head;	while (addressNow!= -1)	{		if(node[addressNow].next!=-1)		printf("%05d %d %05d/n", addressNow, node[addressNow].data, node[addressNow].next);		else			printf("%05d %d %d/n", addressNow, node[addressNow].data, node[addressNow].next);		addressNow = node[addressNow].next;	}	return 0;}/*注意把每次反转后的节点的下一个地址修改正确,比如说1 2 3 4 5 6,反转个数K为2时,应该是2 1 4 3 6 5,这时要记得把1跟4连上,3跟6连上。*/
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表