2011-05-29 95 views
0

我正在學習C++。我正在創建一個鏈表數據結構。顯示結構中節點值的函數之一不起作用。出於某種原因,遍歷節點的while循環在顯示函數中不起作用,因此我看不到這些節點中的值。有沒有人看到問題是什麼?我一直在盯着代碼一段時間,不知道這裏有什麼問題。 感謝您的幫助提前。 頭文件:基本C++編程問題

// linklist.h 
// class definitions 


#ifndef LINKLIST_H 
#define LINKLIST_H 

class linklist 
{ 
private: 
    // structure containing a data part and link part 
    struct node 
    { 
     int data; 
     node *link; 
    }*p; 

public: 

    linklist(); 
    void append(int num); 
    void addatbeg(int num); 
    void addafter(int loc, int num); 
    void display(); 
    int count(); 
    void del(int num); 
    ~linklist(); 
}; 

#endif 

.cpp file 

// LinkedListLecture.cpp 
// Class LinkedList implementation 

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

using namespace std; 


    // initializes data member 
    linklist::linklist() 
    { 
     p =NULL; 
    } 

    // adds a node at the end of a linked list 
    void linklist::append(int num) 
    { 
      node *temp, *r; 
     // if the list is empty, create first node 
     if(p==NULL) 
     { 
       temp = new node; 
      temp->data = num; 
      temp->link = NULL;  
     } 
     else 
     { 
      // go to last node 
      temp = p; 
      while(temp->link!=NULL) 
       temp = temp->link; 
      // add node at the end 
      r = new node; 
      r->data=num; 
      r->link=NULL; 
      temp->link=r; 
     } 
    } 



// displays the contents of the linked list 
void linklist::display() 
{ 
    node *temp = p; 
    cout<< endl; 
    // traverse the entire linked list 
    while(temp!=NULL) // DEBUG: the loop doesn't work 
    { 
     cout<<temp->data<<" "; 
     temp = temp->link; 
    } 

    void main() 
{ 
    linklist l; 

    l.append(14); 
    l.append(30); 
    l.append(25); 
    l.append(42); 
    l.append(17); 
    cout<<"Elements in the linked list:"; 
    l.display(); // this function doesn't work 
    system("PAUSE"); 
} 
+1

它以什麼方式「不起作用」?期望的輸出是什麼,你取而代之的是什麼? (還要記住,當你轉移到真正的生產代碼時,最好使用標準庫附帶的鏈表,'std :: list') – 2011-05-29 02:52:34

+0

[GWW's answer](http:// stackoverflow。 com/questions/6165674/basic-c-programming-question/6165685#6165685)強調它不是'display()',而是'append()'引起你的悲傷。一旦你解決了這個問題,你可能想要爲每個'new'添加一個'delete',去除[內存泄漏](http://en.wikipedia.org/wiki/Memory_leak)。 – Johnsyweb 2011-05-29 03:06:21

回答

4

您從不將p設置爲非NULL值。

if(p==NULL) 
    { 
     p = new node; 
     p->data = num; 
     p->link = NULL;  
    } 
+2

+1:這當然有幫助! – Johnsyweb 2011-05-29 03:03:47

+1

或者更好的是,將節點創建的東西移動到頂端,並讓'if'和'else'只是擔心連接新節點。 – 2011-05-29 04:04:46

+0

@ Ben Voigt:同意 – GWW 2011-05-29 04:06:39

1

我認爲GWW強調了這個問題,但是學習編程它的一部分來學習如何識別錯誤。

如果你做的東西,並沒有得到預期的結果你可以:

  • 使用Visual C++調試單步,看看你的變量的值。
  • 放入日誌行來報告您認爲重要的信息
  • 檢查代碼 - 如果您認爲有些事情是正確的,但它不起作用,那麼請轉到先前的步驟並檢查它是否正確。
  • 添加單元測試,或遵循合同添加前/後條件和類不變量的設計。

學習如何通過編寫鏈表來編程C++,就像通過加1 + 1來學習數學一樣。它是老式的思維,很慢而且沒有任何上下文就很無聊。 數學不計算,就像C++編程不是指針操作一樣。在某個階段你可能需要了解它,但你最好學習其他重要的東西,比如stl和boost。

如果據瞭解append() ment創建了一些東西,找到列表的末尾,添加它。你可以看到在你追加函數中你創建了一些混合的uyp並移動到列表的末尾,但是你永遠不會添加它。

+1

C++程序員*應該*用指針做一些簡單的項目,以瞭解它們的工作原理。使用'std :: unique_ptr','std :: shared_ptr'和'std :: vector'對任何指針的理解都容易得多。這並不意味着你需要能夠複製具有所有錯綜複雜的'unique_ptr',但是不知道某些底層分配的東西,你在調試器中看到的將是毫無意義的。 – 2011-05-29 04:03:52

+1

單元測試將是一個巨大的好處。一個簡單的測試,比如'linklist l; l.append(14); assert(l.p!= NULL); assert(l.p-> data == 14);'會很快識別錯誤。 – Johnsyweb 2011-05-29 04:05:33

+0

@本Voigt:不要不同意你。 – 2011-05-29 13:48:18