c++ - 链表段错误

标签 c++ linked-list singly-linked-list

为什么这会导致段错误?我试图用 gdb 运行回溯,但它没有给我任何帮助。 任何帮助将不胜感激,我已经为此苦恼了几个小时。

我的节点.h

#ifndef NODE_H
#define NODE_H

#include <string>
using namespace std;

class Node
{
  public:

    Node(const string, const int) ;
   ~Node() { }
    void setNext(Node *);//setter for the next variable
    Node * getNext();// getter for the next variable
    string getKey();// getter for the key variable
    int getDistance();   // getter for the dist variable

  private:
    Node *next;
    int dist;
    string key;
};

#endif

我的 Node.cpp

#include "node.h"
#include <string>

Node::Node(string k, int d){
    key = k;
    dist = d;
}

void Node::setNext(Node * n){
    next = n;
}

Node * Node::getNext(){
    return next;
}

string Node::getKey(){
return key;
}

int Node::getDistance(){
    return dist;
}

我的列表.h

#ifndef LIST_H
#define LIST_H

#include "node.h"

class SLL
{
    public:
        SLL();
        ~SLL() { }
               void Insert (string searchKey, int distance);
               bool Delete (string searchKey);
               void Print();
               int Search(string searchKey);

    private:
        int count;
        Node *head;
    Node *iterator;
    Node *temp;
};

#endif

我的列表.cpp

#include "list.h"
#include <iostream>

SLL::SLL():head(0){}

void SLL::Insert(string searchKey, int distance){
Node * temp = new Node(searchKey, distance);

if(head == 0){
    head = temp;
}
else{
    temp->setNext(head);
    head = temp;
}
}

bool SLL::Delete(string searchKey){
 if(head == 0){
cout << "An attempt was made to delete a node from an empty list" << endl;
 }
 else{
Node* iterator = head;
Node* last = 0;

while(iterator != 0){
   if (iterator->getKey() == searchKey){
    break;
   }
   else{
    last = iterator;
    iterator = iterator->getNext();
   }
}
if (iterator == 0){
    return false;
}
else{
    if(head == iterator){
        head = head->getNext();

    }
    else {
        last->setNext(iterator->getNext());
    }
    delete iterator;



    }

    }
 }

void SLL:: Print(){
iterator = head;
while(iterator != 0){   
    cout << iterator->getKey()  << "-" << iterator->getDistance() << endl;
    iterator = iterator->getNext();
 }

}

int SLL::Search(string searchKey){

 }

我的 main.cpp

#include "list.h"
#include "node.h"
#include <iostream>

using namespace std;

int main(int argc, char* argv[]) {
    SLL * sll; 

    sll->Insert("test", 1);
    sll->Insert("test2", 2);
    sll->Delete("test");
    sll->Print();
}

最佳答案

提示:段错误发生在这里:

int main(int argc, char* argv[]) {
    SLL * sll; 

    sll->Insert("test", 1); // BIG segfault here.
    ...

(没有完整答案,因为这看起来像是作业。)

关于c++ - 链表段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14903940/

相关文章:

java - SWIG C++ 结构到 java

c++ - 我可以在 Visual Studio 2017 调试器中查看未处理的异常吗?

c - 使用链表的 C 中的哈希表

java - 反向链表问题

C++链表遍历和修改

python - 无法合并两个已排序的单链表,因为 "type object ' _Node' 没有属性 '_element' "

c# - 将 C/C++ 函数导入 C#

c++ - 如何仅构建 boost 所需的模块?

c - 单链表似乎不起作用

c - 如何在另一个链表中获取链表的奇数 inode ?我不想使用双指针