【问题标题】:Runtime Error for linkedlists in C++C++ 中链表的运行时错误
【发布时间】:2017-05-31 01:21:36
【问题描述】:

每当我运行此代码以实现链接列表时,都会遇到运行时错误。我认为错误出现在默认构造函数中,因为我试图运行只创建对象的程序,没有做任何其他事情。

linkedlist.h 文件是

#ifndef LinkedList_h
#define LinkedList_h

#include<iostream>
#include<string>

using namespace std;

struct node{
    string song;
    node *next;
};

class LinkedList{
    private:
        node *head;
        int listLength;
    public:
        LinkedList();
        bool insertNode(node *newnode, int position);
        bool removenode(int position);
        void printList();
        ~LinkedList();
};

#endif

linkedlist.cpp文件是

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

LinkedList::LinkedList(){
    head = new node;
    head->song ="head";
    head->next=NULL;
    listLength = 0;
}

bool LinkedList::insertNode(node *newnode, int position){
    if(position<=0 || position >listLength+1)
        {
            cout<<"Error: position is out of range";
            return false;
        }
    if(head->next == NULL){
        head->next = newnode;
        listLength++;
        return true;
    }
    int count = 0;
    node *p=head;
    node *q=head;
    while(q){
        if(position == count){
            p->next=newnode;
            newnode->next = q;
            listLength++;
            return true;
        }
    p=q;
    q=p->next;
    count++;
    }
    cout<<"Unable to insert the element due to technical issues";
    return false;
}

bool LinkedList::removenode(int position){
    if(position<=0 || position > listLength+1){
        cout<<"Invallid position\n";
        return false;
    }
    if(head->next ==NULL){
        cout<<"The list is already empty\n";
        return false;
    }
    int count =0;
    node *q = head;
    node *p = head;
    while(q){
        if(count==position){
        p->next = q->next;
        delete q;
        listLength--;
        return true;
        }
    p=q;
    q=p->next;
    count++;
    }
    cout<<"Error removing elements";
    return false;
}

void LinkedList::printList(){
    int count=0;
    node *p = head;
    node *q = head;

    if(head->next==NULL){
        cout<<"The list is empty\n";
    }
    while(count<listLength){
        cout<<"\n"<<p->song<<endl;
        q=p;
        p=q->next;
        count++;
    }
}

LinkedList::~LinkedList() 
{
    node * p = head;
    node * q = head;
    while (q)
    {
        p = q;
        q = p -> next;
        if (q) delete p;
    }
    delete head;
}

main.cpp是

#include "linkedlist.h"
#include "linkedlist.cpp"
#include<iostream>

using namespace std;

int main(){
    node *A = new node;
    A->song = "Swatch";

    node *B = new node;
    B->song = "one plus 2";

    node *C = new node;
    C->song = "Woodland";

    LinkedList l;
    l.insertNode(A,1);
    l.insertNode(B,2);
    return 0;
}

【问题讨论】:

  • 请edit您的问题提供minimal reproducible example。
  • “我收到运行时错误”。然后调试它。这就是人们遇到错误时所做的事情。帮助您解决此问题的最佳工具是调试器。

标签: c++ data-structures linked-list


【解决方案1】:

看起来你从未在构造函数中初始化 head。这将导致运行时错误。您需要添加一个声明,如

head = new node;

然后记得在不再需要时删除它。

【讨论】:

  • 现在问题出在你的析构函数上。请注意,在您的 while 循环的第一次运行中,您将删除头节点。然后在完成 while 循环后,再次删除 head。删除已释放的内存会导致运行时错误。
  • 看到完整代码后的另一条评论。您可能希望构造函数初始化一个空列表,而不是拥有一个无用的头节点。你可以在构造函数中设置head=NULL,在insert和print方法中需要检查head是否为null。
猜你喜欢
  • 1970-01-01
  • 2020-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-04
  • 1970-01-01
  • 2012-07-28
相关资源
最近更新 更多