我正在尝试找出以下问题的实现:
从未排序的链接列表中删除重复项。
我的方法是尝试遍历链表。在每个节点上,检查哈希表以查看它是否存在。如果是,请删除该节点。否则,插入列表。
我的链表实施:
struct Node {
int data;
Node *next;
Node(int d){data = d;}
};
class LinkedList {
private:
Node *head;
public:
LinkedList() {head = NULL;}
~LinkedList() {
while (head != NULL) {
Node *tmp = head;
head = head->next;
delete tmp;
}
}
void add(int d) {
Node *newNode = new Node(d);
newNode->next = head;
head = newNode;
}
void print() {
Node *cur = head;
while (cur != NULL) {
std::cout << cur->data << "->";
cur = cur->next;
}
}
Node *getHead() {
return head;
}
};
哈希表:
class HashNode {
protected:
int key;
int value;
HashNode *next;
public:
HashNode(int key, int value) {
this->key = key;
this->value = value;
this->next = NULL;
}
friend class HashMap;
};
class HashMap {
private:
HashNode **htable;
public:
HashMap() {
htable = new HashNode*[tableSize];
for (int i = 0; i < tableSize; i++) {
htable[i] = NULL;
}
}
~HashMap() {
for (int i = 0; i < tableSize; i++) {
HashNode *entry = htable[i];
while (entry != NULL) {
HashNode *prev = entry;
entry = entry->next;
delete prev;
}
delete[] htable;
}
}
int hashFunction(int key) {
return key % tableSize;
}
void insert(int key, int value) {
int hashVal = hashFunction(key);
HashNode *prev = NULL;
HashNode *entry = htable[hashVal];
while (entry != NULL) {
prev = entry;
entry = entry->next;
}
entry = new HashNode(key, value);
if (prev == NULL) {
htable[hashVal] = entry;
} else {
prev->next = entry;
}
}
bool search(int key) {
bool flag = false;
int hashVal = hashFunction(key);
HashNode *entry = htable[hashVal];
while (entry != NULL) {
if (entry->key == key)
flag = true;
entry = entry->next;
}
return flag;
}
};
最后我的功能是删除重复项:
void removeDuplicates(LinkedList *listPtr, HashMap *hashPtr) {
Node *cur = listPtr->getHead();
Node *prev = NULL;
while (cur != NULL) {
Node *tmp = cur;
if (hashPtr->search(cur->data)) {
prev->next = cur->next;
cur = cur->next;
delete tmp;
} else {
hashPtr->insert(cur->data, 1);
prev = cur;
cur = cur->next;
}
}
}
当我在main中运行以下内容时:
#include <iostream>
const int tablesize = 128;
int main() {
LinkedList myList;
HashMap intMap;
myList.add(1);
myList.add(3);
myList.add(5);
myList.add(3);
myList.add(3);
myList.add(5);
myList.add(20);
myList.add(57);
std::cout << "before function call: \n";
myList.print();
removeDuplicates(&myList, &intMap);
std::cout << "\nafter:\n";
myList.print();
return 0;
}
这是我得到的输出:
before function call:
57->20->5->3->3->5->3->1->
after:
go(14453,0x7fff762c1300) malloc: *** error for object 0x7f9250801200: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
57->20->5->3->1->Abort trap: 6
最终列表正确打印出来,但我仍然遇到与之相关的奇怪错误。 可以有人帮助我,给新手一些提示我做错了什么,提前谢谢。
编辑 - 我在gdb下运行它,这是输出:
main () at 2.1RemoveDuplicatesFromLinkedList.cpp:149
149 std::cout << "\nafter:\n";
(gdb) step
57->20->5->3->3->5->3->1->
after:
150 myList.print();
(gdb) step
152 return 0;
(gdb) step
broken(14784,0x7fff762c1300) malloc: *** error for object 0x100801200: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
57->20->5->3->1->
Program received signal SIGABRT, Aborted.
0x00007fff8484e286 in __pthread_kill () from /usr/lib/system/libsystem_kernel.dylib
(gdb)
奇怪的是,我很好奇所以我去了cpp.sh在线编译和执行。输出完全符合预期,程序终止没有任何错误。在我的本地机器上有什么可能导致这种情况的想法? (OS X)