国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

PAT甲級1074

2019-11-10 23:04:50
字體:
供稿:網(wǎng)友

1074. Reversing Linked List (25)

時(shí)間限制400 ms內(nèi)存限制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];//定義靜態(tài)鏈表節(jié)點(diǎn)struct record{	int address;	//int next;};//用于反轉(zhuǎn)鏈表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;//鏈接各節(jié)點(diǎn)	}	int count = 0;//計(jì)數(shù)器	int addressNow = head;	int pre;	stack<record> sta;//用于暫存要被逆轉(zhuǎn)連接的節(jié)點(diǎn)	record temprecord;//保存節(jié)點(diǎn)地址,本來以為要保存下一個(gè)節(jié)點(diǎn)的地址,其實(shí)根本不需要,也不想改了就這樣吧	int oldLast;//上一次逆轉(zhuǎn)后指向最后一個(gè)節(jié)點(diǎn)的指針	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;//第一個(gè)開始反轉(zhuǎn)的節(jié)點(diǎn)成為頭了				if (!sta.empty())				{					sta.pop();				}				if (pre != head)				{					node[oldLast].next = pre;				}//如果不是第一個(gè)那么需要將上一次反轉(zhuǎn)后的最后一個(gè)節(jié)點(diǎn)的下個(gè)地址做修改				//將樣例中的K由4改為2你就知道為什么了			for (int i = 0; i < K-1; i++)			{				node[pre].next = sta.top().address;//逆轉(zhuǎn)鏈表				pre = sta.top().address;				if (!sta.empty())				{					sta.pop();				}			}			node[pre].next = addressNow;//修改反轉(zhuǎn)節(jié)點(diǎn)的最后一個(gè)節(jié)點(diǎn)的下一個(gè)地址,			//若后面不會(huì)再反轉(zhuǎn),那么就是當(dāng)前掃描的節(jié)點(diǎn)的地址,注意我是先計(jì)數(shù)后又把指針			//往后移一位了			oldLast = pre;//保存反轉(zhuǎn)節(jié)點(diǎn)的最后一個(gè)節(jié)點(diǎn)的地址		}	}	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;}/*注意把每次反轉(zhuǎn)后的節(jié)點(diǎn)的下一個(gè)地址修改正確,比如說1 2 3 4 5 6,反轉(zhuǎn)個(gè)數(shù)K為2時(shí),應(yīng)該是2 1 4 3 6 5,這時(shí)要記得把1跟4連上,3跟6連上。*/
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 吉安市| 汪清县| 颍上县| 孟连| 怀来县| 彭山县| 新干县| 通海县| 轮台县| 株洲县| 云浮市| 天镇县| 防城港市| 临武县| 湟中县| 乌拉特中旗| 陇南市| 岗巴县| 瑞昌市| 巫溪县| 利辛县| 理塘县| 澄迈县| 沈阳市| 关岭| 五大连池市| 新宾| 临夏市| 金堂县| 习水县| 威海市| 上饶市| 安图县| 日喀则市| 镇原县| 绩溪县| 克东县| 仁怀市| 安岳县| 玛多县| 庄浪县|