2016-02-07 97 views
-2

我目前正在学习C++对我自己的,并已经历了一些教科书和试图做的一些问题。在学习指针时,我决定尝试自己实现一个链表。我写的程序,但要得到一个错误,指出:“分割错误(核心转储)”。我已经通过本网站上的其他类似的问题搜索,虽然也有关于同一主题很多,还没有帮我解决我的问题。我很新的编程和指针,所以任何帮助将不胜感激!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); 
} 

问题发生在我的代码(主要是我写pl.ADD(1))的第39行。在此行中我试图开始列表与淡水河谷1有可能与我的代码的其余太问题,但我一直没能闯过这条线进行检查。请帮忙!

+1

Pointerlist为空。您需要创建pointerlist类的新实例执行ADD操作 –

+0

之前,您应该获得良好的C++的书,这说明指针是什么。现在您将代码发送给其他人进行调试。 – Tapani

回答

1

列表为开头空,因此它会在s-> nexts == start ==NULL失败:

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

感谢您的帮助!我可以通过添加第一个将第一个元素添加到列表中的函数来解决问题。但是现在我遇到了END函数的问题。它在被调用时似乎没有进入函数内的循环。我找不到这个原因。任何人都可以帮助我找出为什么我的END功能不起作用?