【发布时间】:2013-01-29 06:10:36
【问题描述】:
我正在尝试从头开始制作一个简单的链表,供另一个类使用。 错误似乎是有时 Head 未设置为 NULL。 我有时会说,它不会一直出现故障。它没有段错误的时间,它转到 else 语句。注意我只添加了 1 个字符串。也许你们能发现我没看到的东西,干杯!
LinkedList.h:
#include <string>
#include "Link.h"
using namespace std;
class LinkedList {
Link *head;
public:
LinkedList();
void addFront(string key);
void printList();
//void addBack(string *);
};
LinkedList.cpp:
#include <cstring>
#include <string>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include "LinkedList.h"
using namespace std;
LinkedList::LinkedList() {
head = NULL;
}
void LinkedList::addFront(string key) {
//creates the new list segment
Link *l = new Link(key);
cout << "Made new Link " << key << endl;
// if the list is empty
if (head == NULL){
cout << "Going to set Head " << key << endl;
head = l;
cout << "Set Head to new link " << key << endl;
}
else {
cout << "Else statement " << key << endl;
l->setNext(head);
head = l;
}
}
void LinkedList::printList() {
//Check if list is empty
if(head == NULL)
cout << "NULL" << endl;
else {
Link *l = head;
for(;l != NULL; l=l->getNext())
cout << l->getValue() << endl;
}
}
// void LinkedList::addBack(string *f) {
// Link *l = new Link(f);
// }
链接.h
#include <cstdlib>
#include <cstring>
#include <string>
using namespace std;
class Link {
string key;
Link *next;
public:
Link(string key);
void setValue(char);
void setNext(Link *next);
string getValue();
Link *getNext();
void printList();
};
链接.cpp
#include <cstdlib>
#include <string>
#include <iostream>
#include "Link.h"
using namespace std;
Link::Link(string key) {
this->key = key;
next = NULL;
}
void Link::setNext(Link *l) {
cout << "setting new link "<<endl;
next = l;
cout<< "New link was set" << endl;
}
string Link::getValue() {
return key;
}
Link *Link::getNext() {
return next;
}
哈希.h
#include <iostream>
#include <cstring>
#include <string>
#include "LinkedList.h"
using namespace std;
class Hash{
//100 slot array for hash function
LinkedList *hashFN[100];
//preset prime number
int prime = 101;
int key;
unsigned int location;
public:
//Note: Both the key & values are the same
void insert(string key, string value);
// void deleteItem(int key);
// char* find(int key);
};
哈希.cpp:
#include <iostream>
#include <cstring>
#include <string>
#include "Hash.h"
using namespace std;
void Hash::insert(string k, string v){
//Get Hash for argv[2] aka value
size_t key = std::hash<string>()(k);
unsigned int location;
//check 1
cout << "Hash: " << key << endl;
//Find location
location = key % prime;
//check 2
cout << "Mod 101 Hash: " << location << endl;
hashFN[location]->addFront(k);
cout << "Success!" << endl;
}
// void Hash::deleteItem(int key){
// return;
// }
// char* Hash::find(int key){
// return;
// }
main.cpp
#include <iostream>
#include <functional>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include "Hash.h"
using namespace std;
int main(int argc, char *argv[]) {
Hash HashTable;
string Insert = string("insert");
string Delete = string("delete");
string Find = string("find");
string Argv(argv[2]); //Makes the argv[2] into string type
// check for Request & string parameters
if(argc != 3) {
cout << "Run program with 2 parameters. [Lower Case]" << endl;
cout << "[1] insert, find, or delete" << endl;
cout << "[2] string" << endl;
exit(1);
}
//Check for "insert"
if(strcmp(argv[1], "insert") == 0) {
HashTable.insert(Argv, Argv);
}
return 0;
}
【问题讨论】:
-
LinkedList的头文件和cpp文件是一样的。检查你的帖子。我怀疑您没有在
LinkedList::LinkedList()构造函数中将head链接指针初始化为NULL,但需要确认/拒绝的文件恰好是未发布的文件。 -
请养成使用所有警告和调试信息编译 C++ 代码的习惯(例如 Linux 上的
g++ -Wall -g),并学习使用调试器(Linux 上的gdb)。 -
向我们展示你的 LinkedList.cpp 文件,我希望你在构造函数中初始化为 NULL。
-
抱歉修复了 LinkedList.cpp
-
@user2017327 您的代码中是否有anywhere 复制 LinkedList 对象?这包括将值传递给函数,或者将一个变量从一个变量复制到另一个变量?我问是因为您的
LinkedList课程不练习Rule of Three,除非您正确地拥有适当的析构函数、赋值运算符、和 i> 复制构造函数。
标签: c++ linked-list