【问题标题】:C++ singly linked list using a classes使用类的 C++ 单链表
【发布时间】:2012-03-14 19:47:39
【问题描述】:

我想创建单链表(使用类),在每个列表中都会有:指向文本的指针、int 编号、指向下一个列表的指针。

我需要实现 3 个功能: 插入(将列表插入单链表并根据指针指向的文本使用 strcmp 对元素进行排序) removes(int num) 删除第一个出现数字的列表。 print() 打印整个单链表。

我遇到了删除函数的问题,它在运行时出错,我猜想问题出在哪里if ( tmp->next == NULL && tmp->number==num ) { delete tmp; first = NULL; },但我不知道为什么会这样。

我也不确定我应该如何在插入函数中实现排序,所以如果你有任何想法,如果你能解释一下我的删除函数中的错误是什么,我真的很感激。

代码如下:

    #include <iostream>
#include <cstdlib>
#include <cstring>

using namespace std;

class list
{
private:
    int number;

    char* word;
    list* next;
public:
    void inserts(int num, char* text);
    void removes(int num);
    void print();
};
list* first;

void list::print() {
    cout <<"This is our list:"<<endl;

    // Temp pointer
    list *tmp = first;

    // No nodes
    if ( tmp == NULL ) {
    cout << "EMPTY list" << endl;
    return;
    }

    // One node in the list
    if ( tmp->next == NULL ) {
    cout <<"NUMBER:\t"<< tmp->number;
    cout <<"\tWORD:\t"<< tmp->word << endl;
    cout <<"--------------------------------"<<endl;

    }
    else {
    // Parse and print the list
    while ( tmp != NULL ){
         cout <<"NUMBER:\t"<< tmp->number;
         cout <<"\tWORD:\t"<< tmp->word << endl;
         cout <<"--------------------------------"<<endl;

        tmp = tmp->next;
    }
}
}

void list::inserts(int num, char* word){
    // Create a new list
    list* newlist = new list; 
    newlist->number=num;

    newlist->word=word;
    newlist->next=NULL;

    // Create a temp pointer
    list *tmp = first;

    if ( tmp != NULL ) {
    // Nodes already present in the list
    // Parse to end of list
    while ( tmp->next != NULL ) {
        tmp = tmp->next;
    }

    // Point the last node to the new node
    tmp->next=newlist;
    }
    else {
    // First node in the list
    first = newlist;
    }
}

void list::removes(int num){
int k = 0;
    list* tmp=first;
    if(tmp==NULL)
        return;
       //Last node of the list

   if ( tmp->next == NULL && tmp->number==num ) {
    delete tmp;
    first = NULL;
    }
    else {
    //Parse thru the nodes
    list* prev;
    prev = new list;
    while ( tmp != NULL )
    {
        if ( tmp->number == num && k == 0)
            first = first->next;
if ( tmp->number == num)
break;
        prev = tmp;

        tmp = tmp->next;
k++;
    } 

    //Adjust the pointers
    prev->next=(tmp->next);
    //Delete the current node
delete tmp;
delete prev;

}
}


int main ()
{
    first->print();
    first->inserts(1200,"endian");
    first->print();
   /* first->inserts(10,"endianness");
    first->inserts(1200,"PEEK");
    first->inserts(1200,"POKE");
    first->inserts(1200,".MIL");
    first->print();*/
first->removes(100);
first->print();
getchar();
}

【问题讨论】:

  • "pointer to text" why?? 只有在非常具体的情况下才合理不使用std::string,在你的情况下@ 987654324@ 使事情复杂化,没有任何好处。
  • 显然,在 STL 15 年后,正在用 char* 教授 C++。郁闷。

标签: c++ list linked-list


【解决方案1】:

去掉removes()函数最后一行的delete prev;

我不是专家,但删除 prev 会丢失对列表中第一个节点的引用。

顺便说一句,好的cmets,让它很容易阅读!

【讨论】:

    【解决方案2】:

    removes中,循环while ( tmp != NULL ) { … }之后,tmp可能为NULL。

    但是,在那个循环之后,你有这些行:

    //Adjust the pointers
    prev->next=(tmp->next);
    

    您正在取消引用一个 NULL 指针,这会导致您的程序崩溃。

    将这些行替换为:

    //Adjust the pointers
    if(tmp)
        prev->next=(tmp->next);
    


    另请注意:removes 例程中的内存管理仍然存在错误。您永远不会在prev 的初始值上调用delete,并且您删除了在某些情况下应该保留的列表节点。而不是你在做什么,初始化它:list* prev=0;,并摆脱delete prev

    【讨论】:

    • tmp = tmp-&gt;next 行最终找到了列表的末尾。
    【解决方案3】:

    当这个循环执行时:

    while ( tmp != NULL )
    

    您的 tmp 指针将到达列表的末尾,并且在循环中断时为 NULL。

    所以,当你执行时:

    prev->next=(tmp->next);
    

    您的临时指针没有“下一个”值。也许你应该在循环之外维护另一个指针。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-10
      相关资源
      最近更新 更多