我正在尝试从头开始制作一个由另一个类使用的简单链表。错误似乎是有时 Head 未设置为 NULL。我有时会说,它不会一直出现故障。它没有段错误的时间,它转到 else 语句。注意我只添加了 1 个字符串。也许你们可以发现我没有看到的东西,干杯!
链表.h:
#include <string>
#include "Link.h"
using namespace std;
class LinkedList {
Link *head;
public:
LinkedList();
void addFront(string key);
void printList();
//void addBack(string *);
};
链表.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;
// }
主文件
#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;
}