码迷,mamicode.com
首页 > 其他好文 > 详细

1097. Deduplication on a Linked List (25)

时间:2015-12-06 12:59:52      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:


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. #pragma warning(disable:4996)
  2. #include <stdio.h>
  3. #include <algorithm>
  4. #include <vector>
  5. #include <iostream>
  6. using namespace std;
  7. struct Node
  8. {
  9. int address;
  10. int val;
  11. int next;
  12. };
  13. int val_list[100010], next_list[100010];
  14. int val_check[10010] = { 0 };
  15. vector<Node> result, removeal;
  16. int main(void) {
  17. int start_point, n;
  18. cin >> start_point >> n;
  19. for (int i = 0; i < n; i++) {
  20. Node temp;
  21. scanf("%d %d %d", &temp.address, &temp.val, &temp.next);
  22. val_list[temp.address] = temp.val;
  23. next_list[temp.address] = temp.next;
  24. }
  25. int flag = 0;
  26. while (flag != 1) {
  27. Node node;
  28. if (val_check[abs(val_list[start_point])] == 0) {
  29. val_check[abs(val_list[start_point])]++;
  30. node.address = start_point;
  31. node.val = val_list[start_point];
  32. node.next = next_list[start_point];
  33. result.push_back(node);
  34. }
  35. else {
  36. val_check[abs(val_list[start_point])]++;
  37. node.address = start_point;
  38. node.val = val_list[start_point];
  39. node.next = next_list[start_point];
  40. removeal.push_back(node);
  41. }
  42. start_point = next_list[start_point];
  43. if (start_point == -1)
  44. flag = 1;
  45. }
  46. for (int i = 0; i < result.size(); i++) {
  47. if (i != result.size() - 1)
  48. result[i].next = result[i + 1].address;
  49. else
  50. result[i].next = -1;
  51. if(result[i].next!=-1)
  52. printf("%05d %d %05d\n", result[i].address, result[i].val, result[i].next);
  53. else
  54. printf("%05d %d %d\n", result[i].address, result[i].val, result[i].next);
  55. }
  56. for (int i = 0; i < removeal.size(); i++) {
  57. if (i != removeal.size() - 1)
  58. removeal[i].next = removeal[i + 1].address;
  59. else
  60. removeal[i].next = -1;
  61. if (removeal[i].next != -1)
  62. printf("%05d %d %05d\n", removeal[i].address, removeal[i].val, removeal[i].next);
  63. else
  64. printf("%05d %d %d\n", removeal[i].address, removeal[i].val, removeal[i].next);
  65. }
  66. /*while (true)
  67. {
  68. }*/
  69. return 0;
  70. }





1097. Deduplication on a Linked List (25)

标签:

原文地址:http://www.cnblogs.com/zzandliz/p/5023346.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!