鱼C论坛

 找回密码
 立即注册
查看: 5941|回复: 4

这个链表问题在哪里呀?怎么运行不正常?

[复制链接]
发表于 2014-1-4 19:40:34 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
我用Dev-C++编写链表程序,运行以后有问题。
  1. #include <iostream>
  2. #include <string>
  3. /************************************************
  4. * linked_list -- Class to handle a linked list *
  5. *              containing a list of strings.   *
  6. *                                              *
  7. * Member functions:                            *
  8. *      add -- Add an item to the list          *
  9. *      is_in -- Check to see if a string is    *
  10. *                      in the list.            *
  11. ************************************************/
  12. class linked_list {
  13.     private:
  14.         /*
  15.          * Node in the list
  16.          */
  17.         struct node {
  18.             // String in this node
  19.             std::string data;
  20.             // Pointer to next node
  21.             struct node *next;
  22.         };
  23.         //First item in the list
  24.         struct node *first;
  25.     public:
  26.         // Constructor
  27.         linked_list(void): first(NULL) {};
  28.         // Destructor
  29.         ~linked_list();
  30.     private:
  31.         // No copy constructor
  32.         linked_list(const linked_list &);
  33.         // No assignment operator
  34.         linked_list& operator = (const linked_list &);
  35.     public:
  36.         // Add an item to the list
  37.         void add(
  38.             // Item to add
  39.             const std::string &what
  40.         ) {
  41.             // Create a node to add
  42.             struct node *new_ptr = new node;
  43.             // Add the node
  44.             new_ptr->next = first;
  45.             new_ptr->data = what;
  46.             first = new_ptr;
  47.         }
  48.         bool is_in(const std::string &what);
  49. };
  50. /************************************************
  51. * is_in -- see if a string is in a             *
  52. *      linked list.                            *
  53. *                                              *
  54. * Returns true if string's on the list,        *
  55. *              otherwise false.                *
  56. ************************************************/
  57. bool linked_list::is_in(
  58.     // String to check for
  59.     const std::string &what
  60. ) {
  61.     /* current structure we are looking at */
  62.     struct node *current_ptr;
  63.     current_ptr = first;
  64.     while (current_ptr != NULL) {
  65.         if (current_ptr->data == what)
  66.             return (true);
  67.         current_ptr = current_ptr->next;
  68.     }
  69.     return (false);
  70. }
  71. /************************************************
  72. * linked_list::~linked_list -- Delete the      *
  73. *      data in the linked list.                *
  74. ************************************************/
  75. linked_list::~linked_list(void) {
  76.     while (first != NULL) {
  77.         delete first;
  78.         first = first->next;
  79.     }
  80. }
  81. int main() {
  82.     linked_list list;   // A list to play with
  83.     list.add("Sam");
  84.     list.add("Joe");
  85.     list.add("Mac");
  86.     if (list.is_in("Harry"))
  87.         std::cout << "Harry is on the list\n";
  88.     else
  89.         std::cout << "Could not find Harry\n";
  90.     return (0);
  91. }
复制代码
这段是代码。调试过程中到最后一步,跳出一个窗口

CPU

CPU

如果直接执行返回值为3221226356
请问出什么问题了?怎么解决?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-1-4 20:41:31 | 显示全部楼层
大哥,既然DELETE,就不要再用他了好吗? 你看你的析构函数,已经delete了first了 还要去找他的next 这样不太好吧,你要不先找个中间指针变量把他的next装好了再去delete他,然后把first赋值。没调试过你的程序,只有先给出这样一个答案。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-1-5 08:33:02 | 显示全部楼层

谢谢哦,我把first = first->next;和delete first;颠倒了次序就好了。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-2-9 19:42:48 | 显示全部楼层
路过看看= =
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-6-9 11:56:45 | 显示全部楼层
纠正一下这个问题的答案。我上面说的只颠倒两个语句次序的做法不能说是正确的。也许当时测试的编译器忽略了这个问题。那就是当最后一个first = first->next;的时候,first就已经是NULL了,于是就要delete一个已经是NULL的节点。这不科学。应该像下面这样做:
  1. linked_list::~linked_list() {
  2.     while (first != NULL) {
  3.         node * prev = first;
  4.         delete first; //NOTICE: the link should be reset before first is deleted.
  5.         first = prev->next;
  6.     }
  7. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-10 02:08

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表