2012-07-09 79 views
0

我正在編寫一個C++程序來實現鏈接列表。在編譯時它不給任何錯誤,但在輸出窗口就變成空白,並計劃與鏈接列表程序面臨分段錯誤

list1.exe結束已經 遇到問題,需要關閉。

調試器響應:編程接收到的信號SIGSEGV,分段故障。

也許是因爲內存泄漏,但我無法弄清楚確切的錯誤,我們該如何解決這個問題。請問編劇中出了什麼問題,應該修正什麼?

下面是代碼

//Program to implement linked list 

    #include <iostream> 
    #include <cstdlib> 

    using namespace std; 

    class Node 
    { 
     int data; 
     Node * next; 

    public: 
     Node(){} 
     int getdata(){return data ;} 
     void setdata(int a){data=a;} 
     void setnext(Node* c){next=c;} 
     Node* getnext(){return next;} 
    }; 

    class linkedlist 
    { 
     Node* head; 

    public: 
     linkedlist(){head=NULL;} 
     void print(); 
     void push_back(int data); 
    }; 

    void linkedlist::push_back(int data) 
    { 
     Node* newnode= new Node(); 
     if(newnode!=NULL) 
     { 
      newnode->setdata(data); 
      newnode->setnext(NULL); 
     } 
     Node* ptr= head; 

     if(ptr==NULL) 
      {head=newnode;} 
     while ((ptr->getnext())!=NULL) 
     { 
      ptr=ptr->getnext(); 
     } 
     ptr->setnext(newnode); 
    } 

    void linkedlist::print() 
    { 
     Node* ptr=head; 
     if(ptr==NULL) 
      {cout<<"null"; return;} 

     while(ptr!=NULL) 
     { 
      cout<<(ptr->getdata())<<" "; 
      ptr=ptr->getnext(); 
     } 
    } 

    int main() 
    { 
    linkedlist list; 
     list.push_back(30); 
     list.push_back(35); 
     list.print(); 
     return 0; 
    } 
+0

請格式化您的代碼! – Ashe 2012-07-09 12:30:00

+0

在哪條線上折斷?或者至少是哪種方法? – Razvan 2012-07-09 12:30:07

+0

你使用過調試器嗎?沒有?爲什麼不? – 2012-07-09 12:31:56

回答

4

主要的問題是在這裏:

if(ptr==NULL) {head=newnode;} 
while ((ptr->getnext())!=NULL) 
{ 
    ptr=ptr->getnext(); 
} 
ptr->setnext(newnode); 

還有的大概意思是在if (ptr == NULL)部分return;;按照現狀,它設置head = newnode,但隨後繼續嘗試訪問導致段錯誤的ptr->getnext()

一些答案建議設置ptr = head = newnode,但請注意底線是ptr->setnext(newnode)-這將導致head->getnext() == head。無限列表!

爲了您的利益,這裏是你的代碼:

享受!

#include <iostream> 
#include <stdexcept> 

class Node { 
    int data; 
    Node *next; 

public: 
    Node(): next(NULL) {} 

    int getdata() const { 
     return data; 
    } 

    void setdata(int a) { 
     data = a; 
    } 

    Node *getnext() const { 
     return next; 
    } 

    void setnext(Node *c) { 
     next = c; 
    } 
}; 

class linkedlist { 
    Node* head; 

public: 
    linkedlist(): head(NULL) {} 

    void print() const { 
     Node *ptr = head; 

     if (ptr == NULL) { 
      std::cout << "null"; 
      return; 
     } 

     while (ptr != NULL) { 
      std::cout << ptr->getdata() << " "; 
      ptr = ptr->getnext(); 
     } 
    } 

    void push_back(int data) { 
     Node *newnode = new Node(); 

     if (newnode == NULL) { 
      throw std::runtime_error("out of memory!"); 
     } 

     newnode->setdata(data); 

     Node *ptr = head; 

     if (ptr == NULL) { 
      head = newnode; 
      return; 
     } 

     while ((ptr->getnext()) != NULL) { 
      ptr = ptr->getnext(); 
     } 

     ptr->setnext(newnode); 
    } 
}; 

int main() { 
    linkedlist list; 
    list.push_back(30); 
    list.push_back(35); 
    list.print(); 
    return 0; 
} 
+0

+1:因爲你在一個寫得很差的問題的答案中付出了很多努力。 – ereOn 2012-07-09 12:50:29

+0

@len:非常感謝!是的,你是對的!我忘了放置返回聲明..但我沒有改變任何東西..和前衛工作正常..哥特人! – assasinC 2012-07-10 06:43:08

0

在下面一行:while ((ptr->getnext())!=NULL) PTR是NULL

+0

@Len:多一個請幫忙... if我想在未來使用這個編也...就像製作一個頭文件,然後在未來的progs包括該文件..你可以寫,並告訴步驟,我需要做到這一點...將欣賞很多! – assasinC 2012-07-10 06:46:32

0

push_back代碼是不正確的,我見過,可以提高你的代碼的其他一些地方:

#include <iostream> 
#include<cstdlib> 

using namespace std; 

class Node 
{ 
     int data; 
     Node * next; 

    public: 
     Node(int d = 0) : data(d), next(NULL) {} 

     int getdata() { return data; } 
     void setdata(int a) { data = a; } 

     void setnext(Node* c) { next = c; } 
     Node* getnext() { return next; } 
}; 

class linkedlist 
{ 
     Node* head; 

    public: 
     linkedlist() : head(NULL) {} 
     void print(); 
     void push_back(int data); 
}; 

void linkedlist::push_back(int data) 
{ 
    Node* newnode = new Node(data); 

    if(head == NULL) 
    { 
     head = newnode; 
    } 
    else 
    { 
     Node* last = head; 
     while(last->getnext() != NULL) 
     last = last->getnext(); 
     last->setnext(newnode); 
    } 
} 

void linkedlist::print() 
{ 
    Node* ptr = head; 
    if(!ptr) 
    { 
     cout << "null"; 
     return; 
    } 

    while(ptr != NULL) 
    { 
     cout << ptr->getdata() << " "; 
     ptr=ptr->getnext(); 
    } 
} 

int main() 
{ 
    linkedlist list; 
    list.push_back(30); 
    list.push_back(35); 
    list.print(); 
    return 0; 
} 

仍然有一些問題有待改進......

+0

你有試過嗎?就像@ Razvan的回答一樣,當你將head-> next連接到它自己時,你將導致一個無限循環。 – Ashe 2012-07-09 12:39:31

+0

@Len固定,太快了;) – Synxis 2012-07-09 12:48:40

+0

太棒了!:)(刪除downvote) – Ashe 2012-07-09 12:51:41