C ++链表实现分段故障(核心转储)错误

时间:2016-02-07 20:21:56

标签: c++ pointers linked-list

我目前正在尝试自己学习C ++并且经历过一些教科书并试图解决一些问题。在学习指针时,我决定尝试自己实现一个链表。我编写了该程序,但不断收到错误消息:"分段错误(核心转储)"。我在这个网站上搜索过其他类似的问题,虽然在同一个主题上有很多,但没有一个能帮助我解决我的问题。我对编程和指针都很陌生,所以任何帮助都会受到赞赏!

#include <iostream>
#include <cstdio>
#include <cstdlib>

using namespace std;


struct node
{
   int element;
   struct node *next;
}*start;


class pointerlist
{
        public:
                node* CREATE(int num);
                void ADD(int num);
                int FIRST();
                int END();
                int RETRIEVE(int pos);
                int LOCATE(int num);
                int NEXT(int pos);
                int PREVIOUS(int pos);
                void INSERT(int pos, int num);
                void DELETE(int pos);
                void MAKENULL();
                pointerlist()
                {
                        start = NULL;
                }
};

main()
{
        pointerlist pl;
        start = NULL;
        pl.ADD(1);
        cout << "Added 1" << endl;
        for (int j=1; j<=5; j++)
                pl.ADD(j);
        cout << "The pointer implemented list is: " << endl;
        for (int i=1; i<=5; i++)
        {
                cout << pl.END() << "  " ;
        }
        cout << endl << endl;
}  

void pointerlist::ADD(int num)
{
        struct node *temp, *s;
        temp = CREATE(num);
        s = start;
        while (s->next != NULL)
                s = s->next;
        temp->next = NULL;
        s->next = temp;
}

node *pointerlist::CREATE(int num)
{
        struct node *temp, *s;
        temp = new(struct node);
        temp->element = num;
        temp->next = NULL;
        return temp;
}

int pointerlist::FIRST ()
{
        int num;
        struct node *s;
        s = start;
        num = s->element;
        return num;
}

int pointerlist::END()
{
        struct node *s;
        s = start;
        int num;
        while (s != NULL);
        {
                num = s->element;
                s = s->next;
        }
        return num;
}

int pointerlist::RETRIEVE(int pos)
{
        int counter = 0;
        struct node *s;
        s = start;
        while (s != NULL)
        {
                counter++;
                if (counter == pos)
                {
                        return s->element;
                }
                s = s->next;
        }
}

int pointerlist::LOCATE(int num)
{
        int pos = 0;
        bool flag = false;
        struct node *s;
        s = start;
        while (s != NULL)
        {
                pos++;
                if (s->element == num)
                {
                        flag == true;
                        return pos;
                }
                s = s->next;
        }
        if (!flag)
                return -1;
}

int pointerlist::NEXT(int pos)
{
        int next;
        int counter = 0;
        struct node *s;
        s = start;
        while (s != NULL)
        {
                counter++;
                if (counter == pos)
                        break;
                s = s->next;
        }
        s = s->next;
        next = s->element;
        return next;

}

int pointerlist::PREVIOUS(int pos)
{
        int previous;
        int counter = 1;
        struct node *s;
        s = start;
        while (s != NULL)
        {
                previous = s->element;
                counter++;
                if (counter = pos)
                        break;
                s = s->next;
        }
        return previous;
}

void pointerlist::INSERT(int pos, int num)
{
        struct node *temp, *s, *ptr;
        temp = CREATE(num);
        int i;
        int counter = 0;
        s = start;
        while (s != NULL)
        {
                s = s->next;
                counter++;
        }
        if (pos == 1)
        {
                if (start = NULL)
                {
                        start = temp;
                        start->next = NULL;
                }
                else
                {
                        ptr = start;
                        start = temp;
                        start->next = ptr;
                }
        }
        else if(pos>1 && pos <= counter)
        {
                s = start;
                for (i=1; i<pos; i++)
                {
                        ptr = s;
                        s = s->next;
                }
                ptr->next = temp;
                temp->next = s;
        }
}

void pointerlist::DELETE(int pos)
{
        int counter;
        struct node *s, *ptr;
        s = start;
        if (pos == 1)
        {
                start = s->next;
        }
        else
        {
                while (s != NULL)
                {
                        s = s->next;
                        counter++;
                }
                if (pos >0 && pos <= counter)
                {
                        s = start;
                        for(int i=1; i<pos; i++)
                        {
                                ptr = s;
                                s = s->next;
                        }
                        ptr->next = s->next;
                }
                free(s);
        }
}

void pointerlist::MAKENULL()
{
        free(start);
}

问题出现在我的代码的第39行(在我编写pl.ADD(1)的main中)。在这一行中,我试图用vale 1开始列表。我的代码的其余部分也可能存在问题,但是我无法通过这行检查。请帮忙!

2 个答案:

答案 0 :(得分:1)

列表在开头是空的,因此s-> next上的s == start ==NULL会失败:

    s = start;
    while (s->next != NULL)

答案 1 :(得分:0)

感谢您的帮助!我能够通过添加第一个函数来解决问题,该函数将第一个元素添加到列表中。但现在我的END功能出现问题。它在调用时似乎没有进入函数内的循环。我找不到合理的理由。有人能帮我弄清楚为什么我的END功能不起作用吗?