分享

7.3 链表处理

 印度阿三17 2019-09-14

2019年9月PAT - 练习笔记——7.3

以下页码标注的是阅读器中实际页码,而不是书本身自印的页码。

第7章 提高篇(1)——数据结构专题(1)

7.3 链表处理

注意

  1. 注意结点地址输出格式要求
  2. 注意对无效结点的处理

目录

  1. B1025 / A1074 反转链表
  2. A1032 Sharing
  3. A1052 Linked List Sorting
  4. A1097 Deduplication on a Linked List

  1. B1025 / A1074 反转链表

    给定一个常数 K 以及一个单链表 L,请编写程序将 L 中每 K 个结点反转。例如:给定 L 为 1→2→3→4→5→6,K 为 3,则输出应该为 3→2→1→6→5→4;如果 K 为 4,则输出应该为 4→3→2→1→5→6,即最后不到 K 个元素不反转。

    输入格式:

    每个输入包含 1 个测试用例。每个测试用例第 1 行给出第 1 个结点的地址、结点总个数正整数 N (≤105)、以及正整数 K (≤N),即要求反转的子链结点的个数。结点的地址是 5 位非负整数,NULL 地址用 −1 表示。

    接下来有 N 行,每行格式为:

    Address Data Next
    

    其中 Address 是结点地址,Data 是该结点保存的整数数据,Next 是下一结点的地址。

    输出格式:

    对每个测试用例,顺序输出反转后的链表,其上每个结点占一行,格式与输入相同。

    输入样例:

    00100 6 4
    00000 4 99999
    00100 1 12309
    68237 6 -1
    33218 3 00000
    99999 5 68237
    12309 2 33218
    

    输出样例:

    00000 4 33218
    33218 3 12309
    12309 2 00100
    00100 1 99999
    99999 5 68237
    68237 6 -1
    
    1. 我的

      #include <iostream>
      #include <vector>
      #include <map>
      
      using namespace std;
      
      typedef struct {
      	int address;
      	int data;
      	int next;
      }Node;
      
      int main(void)
      {
      	int start = 0, n = 0, k = 0;
      	scanf("%d %d %d", &start, &n, &k);
      	
      	vector<Node> nodes(n);
      	map<int, int> normal, reverse;
      	for (int i = 0;i < n;  i) {
      		int address = 0, data = 0, next = 0;
      		scanf("%d %d %d", &address, &data, &next);
      		nodes[i] = {address, data, next};
      		normal[address] = i;
      		reverse[next] = i;
      	}
      	
      	int i = 0, j = 0, now = start, first = start;
      	for (;now != -1;  i,   j) {
      		now = nodes[normal[now]].next;
      		if (k - 1 == j % k) {
      			if (first != start) printf("d\n", nodes[reverse[now]].address);
      			for (int count = 0, now1 = now;count < k;  count) {
      				printf("d %d ", nodes[reverse[now1]].address, nodes[reverse[now1]].data);
      				now1 = nodes[reverse[now1]].address;
      				if (count   1 < k) printf("d\n", nodes[reverse[now1]].address);
      			}
      			first = now;
      		}
      	}
      	if (i % k) {
      		printf("d\n", first);
      		for (;first != -1;) {
      			printf("d %d ", first, nodes[normal[first]].data);
      			first = nodes[normal[first]].next;
      			if (first != -1) printf("d\n", first);
      		}
      	}
      	printf("-1");
      	
      	return 0;
      }
      
    2. 《算法笔记》P272

      要考虑可能存在无效结点的情况,即不是由题目给出的头结点引出的单链表上的结点,这些结点是要去掉的,最终不予输出。”(测试点6)

      输入:
      00000 6 3
      00000 1 11111
      11111 2 22222
      22222 3 -1
      33333 4 44444
      44444 5 55555
      55555 6 -1
      输出:
      22222 3 11111
      11111 2 00000
      00000 1 -1
      

  2. A1032 Sharing

    To store English words, one method is to use linked lists and store a word letter by letter. To save some space, we may let the words share the same sublist if they share the same suffix. For example, loading and being are stored as showed in Figure 1.

    fig.jpg

    Figure 1

    You are supposed to find the starting position of the common suffix (e.g. the position of i in Figure 1).

    Input Specification:

    Each input file contains one test case. For each case, the first line contains two addresses of nodes and a positive N (≤105), where the two addresses are the addresses of the first nodes of the two words, and N is the total number of nodes. The address of a node is a 5-digit positive integer, and NULL is represented by −1.

    Then N lines follow, each describes a node in the format:

    Address Data Next
    

    whereAddress is the position of the node, Data is the letter contained by this node which is an English letter chosen from { a-z, A-Z }, and Next is the position of the next node.

    Output Specification:

    For each case, simply output the 5-digit starting position of the common suffix. If the two words have no common suffix, output -1 instead.

    Sample Input 1:

    11111 22222 9
    67890 i 00002
    00010 a 12345
    00003 g -1
    12345 D 67890
    00002 n 00003
    22222 B 23456
    11111 L 00001
    23456 e 67890
    00001 o 00010
    

    Sample Output 1:

    67890
    

    Sample Input 2:

    00001 00002 4
    00001 a 10001
    10001 s -1
    00002 a 10002
    10002 t -1
    

    Sample Output 2:

    -1
    
    1. 我的

      #include <iostream>
      #include <map>
      
      using namespace std;
      
      int main(void)
      {
      	int start1 = 0, start2 = 0, n = 0;
      	scanf("%d %d %d", &start1, &start2, &n);
      	
      	map<int, int> normal;
      	for (int i = 0;i < n;  i) {
      		int address = 0, c = 0, next = 0;
      		scanf("%d %c %d", &address, &c, &next);
      		normal[address] = next;
      	}
      	
      	map<int, bool> link1;
      	int now = start1;
      	for (;now != -1;now = normal[now]) link1[now] = true;
      	now = start2;
      	for (;now != -1;now = normal[now]) if (link1[now]) break;
      	if (now != -1) printf("d", now);
      	else printf("-1");
      	
      	return 0;
      }
      

      注意无效结点的处理

      注意两个链表头结点为同一个的情况:测试点4

    2. 《算法笔记》P277


  3. A1052 Linked List Sorting

    A linked list consists of a series of structures, which are not necessarily adjacent in memory. We assume that each structure contains an integer key and a Next pointer to the next structure. Now given a linked list, you are supposed to sort the structures according to their key values in increasing order.

    Input Specification:

    Each input file contains one test case. For each case, the first line contains a positive N (<105) and an address of the head node, where N is the total number of nodes in memory and the address of a node is a 5-digit positive integer. NULL is represented by −1.

    Then N lines follow, each describes a node in the format:

    Address Key Next
    

    where Address is the address of the node in memory, Key is an integer in [−105,105], and Next is the address of the next node. It is guaranteed that all the keys are distinct and there is no cycle in the linked list starting from the head node.

    Output Specification:

    For each test case, the output format is the same as that of the input, where N is the total number of nodes in the list and all the nodes must be sorted order.

    Sample Input:

    5 00001
    11111 100 -1
    00001 0 22222
    33333 100000 11111
    12345 -1 33333
    22222 1000 12345
    

    Sample Output:

    5 12345
    12345 -1 00001
    00001 0 11111
    11111 100 22222
    22222 1000 33333
    33333 100000 -1
    
    1. 我的

      #include <iostream>
      #include <map>
      
      using namespace std;
      
      int main(void)
      {
      	int n = 0, start = 0;
      	cin >> n >> start;
      	
      	map<int, int> normal, addresses, keys;
      	for (int i = 0;i < n;  i) {
      		int address = 0, key = 0, next = 0;
      		cin >> address >> key >> next;
      		normal[address] = next;
      		addresses[address] = key;
      	}
      	
      	int now = start;
      	for (;now != -1;now = normal[now]) keys[addresses[now]] = now;
      	
      	printf("%d ", keys.size());
      	map<int, int>::iterator iter = keys.begin();
      	for (;iter != keys.end();  iter) printf("d\nd %d ", iter->second, iter->second, iter->first);
      	printf("-1");
      	
      	return 0;
      }
      

      注意无效结点的处理:测试点1、4

    2. 《算法笔记》P279


  4. A1097 Deduplication on a Linked List

    Given a singly linked list L with integer keys, you are supposed to remove the nodes with duplicated absolute values of the keys. That is, for each value K, only the first node of which the value or absolute value of its key equals K will be kept. At the mean time, all the removed nodes must be kept in a separate list. For example, given L being 21→-15→-15→-7→15, you must output 21→-15→-7, and the removed list -15→15.

    Input Specification:

    Each input file contains one test case. For each case, the first line contains the address of the first node, and a positive N (≤105) which is the total number of nodes. 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 Key Next
    

    where Address is the position of the node, Key is an integer of which absolute value is no more than 104, and Next is the position of the next node.

    Output Specification:

    For each case, output the resulting linked list first, then the removed list. Each node occupies a line, and is printed in the same format as in the input.

    Sample Input:

    00100 5
    99999 -7 87654
    23854 -15 00000
    87654 15 -1
    00000 -15 99999
    00100 21 23854
    

    Sample Output:

    00100 21 23854
    23854 -15 99999
    99999 -7 -1
    00000 -15 87654
    87654 15 -1
    
    1. 我的

      #include <iostream>
      #include <map>
      #include <queue>
      
      using namespace std;
      
      map<int, int> normal, addresses;
      
      void Output(queue<int>&q)
      {
      	if (!q.empty()) {
      		printf("d %d ", q.front(), addresses[q.front()]);
      		q.pop();
      		for (;!q.empty();q.pop()) printf("d\nd %d ", q.front(), q.front(), addresses[q.front()]);
      		printf("-1\n");
      	}
      }
      
      int main(void)
      {
      	int start = 0, n = 0;
      	scanf("%d %d", &start, &n);
      	
      	queue<int> result, remove;
      	for (int i = 0;i < n;  i) {
      		int address = 0, key = 0, next = 0;
      		scanf("%d %d %d", &address, &key, &next);
      		normal[address] = next;
      		addresses[address] = key;
      	}
      	
      	map<int, bool> nums;
      	for (int now = start;now != -1;now = normal[now]) {
      		int key = addresses[now];
      		if (nums[abs(key)]) remove.push(now);
      		else {
      			result.push(now);
      			nums[abs(key)] = true;
      		}
      	}
      	Output(result);
      	Output(remove);
      	
      	return 0;
      }
      

      注意链表为空的情况:测试点2

    2. 《算法笔记》P282


来源:https://www./content-4-451151.html

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多