微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

单链表上的分段错误

如何解决单链表上的分段错误

我遇到了无法修复的细分错误问题。我希望对为什么会发生这种情况有所了解。这是一个链接列表,用于获取字符串并将每个字符分配给列表中的节点。分段错误指向我的副本构造函数中的第一行,该行创建了新的Node。代码包括在下面。

sllString.cpp

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

using namespace std;

sllString::sllString(const string &other)
{
    Node *newNode = new Node();
    ^^^        //Segmentation Fault here
    head = newNode;
    newNode->data = other[0];
    newNode->next;
    for (int i = 1; i < other.size(); i++)
    {
        newNode->next = new Node;
        newNode->next->data = other[i];
        newNode = newNode->next;
        counter++;
    }
}

sllString::~sllString()
{
    Node *currNode = head;
    cout << "Delete...";
    while (currNode != NULL)
    {
        head = currNode;
        currNode = currNode->next;
        cout << head->data << " ";
        delete head;
    }
    cout << endl;
}

sllString::sllString(const sllString &other)
{
    Node *newNode = head;
    Node *newNode2 = other.head;
    while (newNode2 != NULL)
    {
        newNode->data = newNode2->data;
        newNode = newNode->next;
        newNode2 = newNode2->next;
    }
}

sllString &sllString::operator=(const sllString &other)
{
    Node *travNode = other.head;
    static sllString result;
    string holder;
    while (travNode != NULL)
    {
        holder += travNode->data;
        travNode = travNode->next;
    }
    result = sllString(holder);
    return result;
}
int sllString::length()
{
    int counter = 0;
    Node *currNode = head;
    if (currNode = NULL)
    {
        return 0;
    }
    while (currNode != NULL)
    {
        counter++;
        currNode->next = currNode;
    }
    return counter;
}
void sllString::erase(char c)
{
    Node *currNode = head;
    if (head = NULL)
    {
        cout << "List is empty.";
    }

    while (currNode != NULL)
    {
        if (currNode->data = c)
        {
            cout << "Removing: " << c << endl;
            currNode->next = currNode->next->next;
            currNode = currNode->next;
        }
    }
}
sllString &sllString::operator+=(const sllString &other)
{
    sllString *result;
    result = this;
    *result += other;
    return *result;
}
char &sllString::operator[](const int n)
{
    Node *current = new Node;
    static char c;
    int compare = n;
    for (current = head; current != NULL; current = current->next)
    {
        if (compare = counter)
        {
            c = current->data;
        }
    }
    return c;
}
ostream &operator<<(ostream &os,const sllString strng)
{
    Node *newNode;
    newNode = strng.head;
    if (newNode != NULL)
    {
        cout << newNode->data;
        newNode = newNode->next;
    }
    while (newNode != NULL)
    {
        cout << "->" << newNode->data;
        newNode = newNode->next;
    }
    return os;
}

sllString.h

#include "node.h"

class sllString
{

public:
    sllString() { head = NULL; }; // fixed by initializing private variable --REMOVE THIS
    sllString(const string &other);
    ~sllString();                      //done
    sllString(const sllString &other); //done
    sllString &operator=(const sllString &other);
    int length();
    sllString &operator+=(const sllString &other);
    char &operator[](const int n);
    int findSubstring(const sllString &substring);
    void erase(char c);
    Node *head = NULL;
    int counter = 0;
    friend ostream &operator<<(ostream &os,const sllString strng);
};

主要

#include <iostream>
#include <string>
#include "sllString.h"

using namespace std;

int main(){
    sllString str("Hello World!");
    sllString newStr;

       newStr = str;
    newStr += sllString(" CS@BC");
    newStr[6] = 'W';
    cout << newStr;
    
    cout << newStr << endl; // Hello World! CS@BC
    cout << newStr.length() << endl; //18
    
    int loc = newStr.findSubstring("World");
    cout << loc << endl; // 6
    
    newStr.erase('l'); //erase the letter l.
    cout << newStr << endl; // Heo Word! CS@BC
    
    newStr.erase('C');
    cout << newStr << endl; // Heo Word! S@B
    
    return 0;
 }

节点类

#include <iostream>
#include <cstddef>


using namespace std;

class Node{
    public:
        char data = 0;
        Node *next = NULL;

};

解决方法

SLLString的副本分配运算符有几个问题。您看到的一个是从result = SLLString(holder);行开始的,该行将递归调用副本分配运算符,最终耗尽堆栈空间。

其他问题源自使用result(您应该修改this对象,而不是新对象)。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?