2011-05-29 80 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