PAT 1097 Deduplication on a Linked List

来源:互联网 发布:身份证借人开淘宝店 编辑:程序博客网 时间:2024/06/06 10:39

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 599999 -7 8765423854 -15 0000087654 15 -100000 -15 9999900100 21 23854
Sample Output:
00100 21 2385423854 -15 9999999999 -7 -100000 -15 8765487654 15 -1
题目:
输入一个链表,要求整个链表中的key唯一,若有重复,保留第一个元素,后面的重复元素都删除并把这些被删元素插入另一新链表,最后依次输出原链表和删除元素的链表

解法&&思路:
1.如何处理输入元素并建立链表?
使用stl容器unordered_map,key是address(唯一),value是这个节点,全部读入保存之后,拿着首元素的地址找next_address,并建立链接
2.如何找出冗余元素?
建立另一个unordered_map,key是节点的值,value是address,边遍历链表边向map中添加元素,用节点的值当key找adress,当unordered_map中已存在key的元素时(说明该节点是冗余节点),删除该节点,插入删除元素链表
最后,依次输出链表元素

遇到的语言层面的细节问题:
new && malloc:
用malloc给结构体分配空间的时候,这个结构体内的成员变量不会自动调用构造函数!!!new会,具体就是stuct里面如果有std::string的变量的时候,得用new,malloc会有问题
还有就是,删除节点并插入删除元素链表的时候,记得把这次插入节点的next指针置null

Code:

#include <iostream>
#include <vector>
#include <unordered_map>
#include <string>
#include <algorithm>
#include <stdio.h>
#define MAXN 100000


struct LinkNode {
std::string address;
std::string next_address;
int vaule;
LinkNode* next;
bool is_redundant;
};


void PrintLinkedList(LinkNode* h){
printf("Print LinkedList:");
while (h) {
printf("%d ", h->vaule);
    }
printf("\n");
}


int main() {
std::unordered_map<std::string, LinkNode*> node_map;
std::unordered_map<int, std::string> vaule_map;
std::string head_address;
int n;
std::cin >> head_address;
std::cin >> n;
LinkNode* nodes[MAXN];
LinkNode* head_node;
for (int i = 0; i < n; i++) {
std::string address;
int vaule;
std::string next_address;
std::cin >> address;
std::cin >> vaule;
std::cin >> next_address;
nodes[i] = new LinkNode;
nodes[i]->vaule = vaule;
nodes[i]->address = address;
nodes[i]->next_address = next_address;
nodes[i]->is_redundant = false;
nodes[i]->next = NULL;
if (address == head_address) {
head_node = nodes[i];
}
node_map[address] = nodes[i];
}
LinkNode* p = head_node;
while (p) {
p->next = node_map[p->next_address];
p = p->next;
}
LinkNode* temp = head_node;
while (temp) {
if (vaule_map[abs(temp->vaule)] == "") {  // new
vaule_map[abs(temp->vaule)] = temp->address;
} else {  // existed
temp->is_redundant = true;
}
temp = temp->next;
}
temp = head_node;
LinkNode* prior;
LinkNode* removed_head = (LinkNode*)malloc(sizeof(LinkNode));
removed_head->next = NULL;
LinkNode* temp_removed = removed_head;
LinkNode* mid;
while (temp) {
mid = temp->next;
if (temp->is_redundant) {
prior->next = mid;
temp_removed->next = temp;
temp_removed = temp;
temp_removed->next = NULL;
} else {
prior = temp;
}
temp = mid;
}
while (head_node) {
printf("%s %d %s\n", head_node->address.c_str(), head_node->vaule, head_node->next==NULL?"-1":head_node->next->address.c_str());
head_node = head_node->next;
}
temp = removed_head->next;
while (temp) {
printf("%s %d %s\n", temp->address.c_str(), temp->vaule, temp->next == NULL ? "-1" : temp->next->address.c_str());
temp = temp->next;
}
for (int i = 0; i < n; i++) {
delete nodes[i];
}
system("pause");
}
0 0
原创粉丝点击