pat 1074 Reversing Linked List

 1 #include<iostream>
 2 #include<vector>
 3 #pragma warning(disable:4996)
 4 
 5 using namespace std;
 6 
 7 struct Node
 8 {
 9     int data;
10     int next;
11 };
12 
13 int main()
14 {
15     int first, address, n, k, cnt = 1, L = 100000;
16     Node List[100005];  //使用静态链表
17     cin >> first >> n >> k;
18     while (n)  //读取数据
19     {
20         --n;
21         cin >> address;
22         cin >> List[address].data >> List[address].next;
23     }
24     List[L].next = -1;
25     int b = first, e = first, r = L;
26     while (e != -1)
27     {
28         if (cnt % k != 0)
29         {
30             e = List[e].next;
31             ++cnt;
32         }
33         else
34         {
35             int t = List[e].next;
36             int p;
37             for (p = b; p != e;)
38             {
39                 int q = List[p].next;
40                 List[p].next = List[r].next;
41                 List[r].next = p;
42                 p = q;
43             }
44             List[p].next = List[r].next;
45             List[r].next = p;
46             r = b;
47             e = t;
48             b = e;
49             ++cnt;
50         }
51     }
52     List[r].next = b;
53     int p;
54     for (p = List[L].next; List[p].next != -1; p = List[p].next)
55         printf("%05d %d %05d\n", p, List[p].data, List[p].next);
56     printf("%05d %d %d", p, List[p].data, List[p].next);
57  
58     system("pause");
59     return 0;
60 }

使用静态链表

猜你喜欢

转载自www.cnblogs.com/Huayra/p/12500218.html