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 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
#include<iostream> #define maxnum 1000000 using namespace std; struct node { int tempdata; int tempnext; }a[maxnum];//构建一个结构体数组,用以存放输入的带有“地址”和“指针”的数据 typedef struct node1 *point; struct node1 { int nownum; int nextnum; int data; point next; }; int start, n, k;//全局变量,方便在主函数和rever函数中调用 int ncount;//用以计数最终进入链表的数据量 point rever(point p, int knum) { int count = 1, flag = 1;//count计数,是否达到要求的翻转个数;flag用于帮助确定最终结果的头结点 int times;//用以判断接下来的数据需不需要翻转 point answer, temp, temp1, newpoint, oldpoint, oldnextpoint; temp = p; if (knum == 1) return p;//不需要翻转时,返回原链表 else { times = ncount / k; while (times--) { newpoint = temp; oldpoint = newpoint->next; oldnextpoint = oldpoint->next; while (count < knum) { oldpoint->next = newpoint; newpoint = oldpoint; oldpoint = oldnextpoint; if (oldnextpoint) { oldnextpoint = oldnextpoint->next; } count++; if (count == knum) { if (flag == 2) { temp1->next = newpoint; } if (flag == 1) { answer = newpoint; flag = 2; } temp->next = oldpoint; temp1 = temp;//temp1用以连接翻转后的各个区块 temp = temp->next; } } count = 1; } return answer; } } point product(int startnum) { point answer, temp; answer = (point)malloc(sizeof(struct node1)); answer->next = NULL; temp = answer; int i; i = startnum; ncount = 0; while (i != -1) { point newnode = (point)malloc(sizeof(struct node1)); newnode->data = a[i].tempdata; newnode->nextnum = a[i].tempnext; newnode->nownum = i; newnode->next = NULL; i = a[i].tempnext; temp->next = newnode; temp = temp->next; ncount++; } answer = answer->next; return answer; } int main() { int tempstart; int i; cin >> start >> n >> k; i = n; while (i--) { cin >> tempstart; cin >> a[tempstart].tempdata; cin >> a[tempstart].tempnext; } point p; p = product(start); point mm; mm = rever(p, k); while (mm->next) { printf("%05d %d %05d\n", mm->nownum, mm->data, mm->next->nownum); mm = mm->next; } printf("%05d %d -1\n", mm->nownum, mm->data); return 0; }