【问题标题】:Segmentation fault from filling Linked List填充链表的分段错误
【发布时间】:2017-03-18 06:25:00
【问题描述】:

我已经在这工作了 4.5 个小时,试图弄清楚为什么这不起作用。仍然没有运气。我不断收到分段错误,或者尽管构建成功,但列表永远不会显示。

SimpleVector.h

// SimpleVector class template
#ifndef SIMPLEVECTOR_H
#define SIMPLEVECTOR_H
#include <iostream>
#include <iomanip>
using namespace std;

class SimpleVector
{
private:
    struct Link{
        int data;
        Link *next;
    };
   Link *head;
public:
   // Default constructor
   SimpleVector()
      {head = NULL;}

   // Destructor declaration
   ~SimpleVector();

   void linkList(int);

   //void insertLink(T);

   void displayList();
};

//Destructor for SimpleVector

SimpleVector::~SimpleVector(){
    Link *linkPtr;
    Link *nextPtr;

    nextPtr = head;

    while(linkPtr !=  NULL){
        nextPtr = linkPtr->next;
    }

    delete linkPtr;

    linkPtr = nextPtr;
}

//Creation of List

void SimpleVector::linkList(int size){    
    Link *newLink = new Link; //create first link
    head = newLink; //

    head->data = size--;         //Fill the front with data
    head->next = NULL;     //Point the front to no where

    do{
        Link *end = new Link;   //Create a new link
        end->data = size--;     //Fill with data
        end->next = NULL;       //Point to no where
        head->next = end;       //Previous link will point to the end
       // head = end;             //Move to the end
    }while(size > 0);          //Repeat until filled
}

//Creation of Link and insertion
/*
template <class T>
void SimpleVector<T>::insertLink(T){

}
*/

//Function to print the entire list

void SimpleVector::displayList(){
    Link *linkPtr;

    linkPtr = head;

    while(linkPtr != NULL){
        cout<<setprecision(3)<<linkPtr->data;
        linkPtr = linkPtr->next;
    }
}

#endif

ma​​in.cpp

// This program demonstrates the SimpleVector template.
#include <iostream>
#include "SimpleVector.h"
using namespace std;


int main(){
int SIZE = 10; // Number of elements

// Create a SimpleVector of ints.
SimpleVector intTable;

intTable.linkList(SIZE);

intTable.displayList();

return 0;
}

【问题讨论】:

  • 尽管构建成功 -- 成功构建仅意味着您的程序中没有语法错误。它与逻辑是否正确或您的程序是否会产生正确的结果无关。
  • 哪一行产生分段错误?
  • 你的析构函数是错误的,因为它使用了未初始化的局部变量。你的编译器没有警告你这个?
  • @PaulMcKenzie 不,它没有
  • @EggplantMachina -- 你声明了linkPtr,从不将其设置为任何值,然后在while 循环中使用它。因此,您使用的是未初始化的指针。

标签: c++ linked-list segmentation-fault


【解决方案1】:

你在linkList() 函数中做错了。 此行head-&gt;next = end;

假设第一个节点包含 10,然后结束包含 9(新节点)
现在head-&gt;next = end

表示10 -&gt; 9

现在新节点 end 变为 8

再次head-&gt;next = end 表示10 -&gt; 8 前 9 个丢失。 . . .

最后会变成10 -&gt; 1

试试这个。

void SimpleVector::linkList(int size){    
    Link *newLink = new Link; //create first link
    head = newLink; //

    head->data = size--;         //Fill the front with data
    head->next = NULL;     //Point the front to no where
    Link *temp = head;
    do{
        Link *end = new Link;   //Create a new link
        end->data = size--;     //Fill with data
        end->next = NULL;       //Point to no where
        temp->next=end;//Previous link will point to the end
        temp=end;  //Now this has become previous link     
       // head = end;             //Move to the end
    }while(size > 0);          //Repeat until filled
}

它使用temp变量指向前一个节点,然后在previous和end(新创建的节点)之间创建链接,然后temp变为end。

编辑:

Link *linkPtr=head;
    Link *nextPtr;// = linkPtr->next;
    do
    {
        nextPtr = linkPtr->next;
        cout<<linkPtr->data<<"\t";
        delete linkPtr;
        linkPtr = nextPtr;
    }while(linkPtr!=NULL);

试试这个。它会删除整个列表

【讨论】:

  • 我之前尝试过这样的事情,但仍然没有运气。我认为这可能与 displayresult 有关,所以我将其更改为 10 次迭代 for 循环,但仍然没有显示任何内容
  • @user64322 我猜你没有阅读我在主线程中关于析构函数问题的评论。
  • 你想删除析构函数中的整个列表吗?
  • @user64322 -- 你没有在析构函数中看到明显的错误吗? linkPtr 设置为什么?
  • 是的,我明白了,但我不知道他想在析构函数中做什么?他正在使用 linkptr->next 也未初始化 while(linkptr)。
猜你喜欢
  • 2016-03-30
  • 1970-01-01
  • 2018-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多