2013-04-09 101 views
1

我的添加函数显然有一个问题,因为它是首先解除引用,第一个指向没有,因为这一点。我只是不知道如何解决它,以便它不是空指针。访问冲突读取位置0xC0000005 C++

struct Node 
    { 
     int data; 
     Node *next; 
    }; 

    class LinkedList 
    { 
     Node *first; 
     Node *last; 
     int count; 
     public: 

     LinkedList() 
     { 
      first = NULL; 
      last = NULL; 
      count = 0; 
     } 


     void Add(int item) 
     { 
      if (first == NULL) 
      { 
       first->data = item; 
       last->data = item; 
       last->next = NULL; 
       first->next = last; 
       count = 1; 
      } 
      else 
      { 
       Node *newNode = new Node; 
       newNode->data = last->data; 
       newNode->next = last; 
       last->data = item; 
       last->next = NULL; 
       count ++; 
      } 
     } 
+0

如果你不希望'first'为空,那么将它指向某个东西。你已经知道如何创建一个'Node'(如'else'块所示),所以:'first = new Node;'。 – jamesdlin 2013-04-09 02:53:22

+5

你的条件说'如果第一个是空的,那么先用'。这是不正确的。如果它为null,则不能使用“first”。 – 2013-04-09 02:54:06

+0

你可以通过为它分配内存来使它不为NULL,就像你在else情况下一样('Node * newNode = new Node;')。 – MatthewD 2013-04-09 02:57:00

回答

3
if (first == NULL) 
{ 
    /* if first is NULL dereference it. Hooray! */ 
    first->data = item; 
    ... 
+0

我认为OP可能已经知道(“它是首先取消引用并且第一个没有指向任何东西”),但不知道如何修复它。 – jamesdlin 2013-04-09 02:58:02

+0

只是简单地说'first = new Node;'? – kccqzy 2013-04-09 03:00:24

+0

@BdkFivehunna无论是否为NULL,你都需要一个'new Node'。 – 2013-04-09 03:00:37

6

您在ifelse之间有很多共同点的代码。

 if (first == NULL) 
     { 
      first->data = item; 
      last->data = item; 
      last->next = NULL; 
      first->next = last; 
      count = 1; 
     } 
     else 
     { 
      Node *newNode = new Node; 
      newNode->data = last->data; 
      newNode->next = last; 
      last->data = item; 
      last->next = NULL; 
      count ++; 
     } 

if,你增加count01。在else,你增加它。

count总是递增。所以你不需要输入两遍。

 if (first == NULL) 
     { 
      first->data = item; 
      last->data = item; 
      last->next = NULL; 
      first->next = last; 
     } 
     else 
     { 
      Node *newNode = new Node; 
      newNode->data = last->data; 
      newNode->next = last; 
      last->data = item; 
      last->next = NULL; 
     } 
     count ++; 

你也在他们都设置last->dataitem

而你正在设置last->nextNULL在他们两个。

 if (first == NULL) 
     { 
      first->data = item; 
      first->next = last; 
     } 
     else 
     { 
      Node *newNode = new Node; 
      newNode->data = last->data; 
      newNode->next = last; 
     } 
     last->data = item; 
     last->next = NULL; 
     count ++; 

当它是第一个新节点时,您也忘了创建一个new Node

 if (first == NULL) 
     { 
      Node *newNode = new Node; // Added 
      first = newNode;   // Added 
      last = newNode;    // Added 
      first->data = item; 
      first->next = last; 
     } 
     else 
     { 
      Node *newNode = new Node; 
      newNode->data = last->data; 
      newNode->next = last; 
     } 
     last->data = item; 
     last->next = NULL; 
     count ++; 

first->data = itemif是多余的。 firstlast相同,并且last->data = item已经发生。

 if (first == NULL) 
     { 
      Node *newNode = new Node; 
      first = newNode; 
      last = newNode; 
      // Removed 
      first->next = last; 
     } 
     else 
     { 
      Node *newNode = new Node; 
      newNode->data = last->data; 
      newNode->next = last; 
     } 
     last->data = item; 
     last->next = NULL; 
     count ++; 

而且,由于firstnewNode相同的值if,我们可以交替使用的变量名。现在

 if (first == NULL) 
     { 
      Node *newNode = new Node; 
      first = newNode;   // These two pointers are equal! 
      last = newNode; 
      newNode->next = last;  // (same pointer) 
     } 
     else 
     { 
      Node *newNode = new Node; 
      newNode->data = last->data; 
      newNode->next = last; 
     } 
     last->data = item; 
     last->next = NULL; 
     count ++; 

几乎所有else也是你if。它可以全部被搬出。

 Node *newNode = new Node; 
     if (first == NULL) 
     { 
      first = newNode; 
      last = newNode; 
     } 
     else 
     { 
      newNode->data = last->data; 
     } 
     newNode->next = last; 
     last->data = item; 
     last->next = NULL; 
     count ++; 

这段代码现在也应该可以理解了。教训:Don't Repeat Yourself。 :)

+1

优秀的文章,大量的细节和良好的解释,所以OP可以沿着。 +1 – 2013-04-09 04:58:32

0

看一看linked list

有一些细节,开始first == NULL,当你需要创建第一,把它插入到链表,把它挂起来,请联系文章的一些算法用。

我会说最简单的是带有一个头节点(而不是first *)的单链表,它可以指向自己,但是有很多方法来实现链表,并且取决于你选择如何连接元素。

这取决于你在做什么后,但如果你只是需要一些工作,那么你可以从boost intrusive circular slist algorithms拿起,你只需定义你自己的结构与数据和下一个指针,告诉它如何访问下一个并使用提供的算法来做所有的工作(链接和取消链接节点)。