代码之家  ›  专栏  ›  技术社区  ›  Mazhar Khan

单链表无限循环

  •  0
  • Mazhar Khan  · 技术社区  · 7 年前

    自从我开始学习链表已经一周了,我只学会了单链表。所以今天我实现了我在c++中学习的链表,当我试图运行它时,代码进入了一些随机数的无限循环。我试着调试代码,但我找不到什么,所以代码总是有问题。代码如下。感谢您的帮助。谢谢

    #include <iostream>
    using namespace std;
    
    struct node{
    int data;
    node * next;
    };
    
    class singly{
    private:
    node * head,*tail;
    public:
    singly(){
        head=NULL;
        tail=NULL;
    }
    
    void createNode(int value){
        node * temp = new node;
        temp->data=value;
        temp->next=NULL;
        if(head==NULL){
            head=temp;
            tail=temp;
            temp=NULL;
        }
    
        else{
            tail->next=temp;
            tail=temp;
        }
    }
    
    void display(){
        node * temp = new node;
        head=temp;
    
        while(temp!=NULL){
            cout << temp->data << "\t" << endl;
            temp->next=temp;
        }
    }
    
    void insert_end(int value){
        node*newnode = new node;
        node*temp = new node;
        newnode->data=value;
        newnode->next=NULL;
        temp=head;
    
        while(temp->next!=NULL){
            temp = temp->next;
        }
        temp->next=newnode;
    }
    
    void delete_node(){
        node*current = new node;
        node*previous = new node;
        current = head;
        while(current->next!=NULL){
            previous=current;
            current=current->next;
        }
    
        tail=previous;
        previous->next=NULL;
        delete current;
    }
    };
    
    int main(){
    
    singly lists;
    lists.createNode(32);
    lists.createNode(654);
    lists.createNode(34);
    lists.createNode(234);
    
    cout<<"\n--------------------------------------------------\n";
    cout<<"---------------Displaying All nodes---------------";
    cout<<"\n--------------------------------------------------\n";
    lists.display();
    cout<<"\n--------------------------------------------------\n";
    cout<<"-----------------Inserting At End-----------------";
    cout<<"\n--------------------------------------------------\n";
    lists.createNode(55);
    lists.display();
    cout<<"\n--------------------------------------------------\n";
    cout<<"-----------------Deleing At End-------------------";
    cout<<"\n--------------------------------------------------\n";
    lists.delete_node();
    lists.display();
    }
    
    2 回复  |  直到 5 年前
        1
  •  1
  •   Vlad from Moscow    7 年前

    成员函数 display 没有意义。 它使数据成员过度膨胀 head 新创建了未初始化的 temp .

        node * temp = new node;
        head=temp;
    

    因此,该函数调用未定义的行为。

    void display()
    {
        for ( node * temp = head; temp != nullptr; temp = temp->next )
        {
            cout << temp->data << "\t";
        }
    }
    

    或者最好用以下方式定义它

    std::ostream & display( std::ostream &os = std::cout )
    {
        for ( node * temp = head; temp != nullptr; temp = temp->next )
        {
            os << temp->data << "\t";
        }
    
        return os;
    }
    

    数据成员 insert_end 也是错误的。它没有考虑到这一点 tail 可以相等 nullptr 并且不会改变它们。

    该函数可以按以下方式定义

    void insert_end(int value)
    {
        node *newnode = new node { value, nullptr };
    
        if ( tail == nullptr )
        {
            head = tail = newnode;
        }
        else
        {
            tail = tail->next = newnode;
        }
    }
    

    成员函数 delete_node 首先,对于单链接列表没有意义,再次是错误的,并调用未定义的行为。该函数应从列表中删除第一个节点。

    void delete_node()
    {
        if ( head != nullptr )
        {
            tail = nullptr;
            node *current = head;
    
            while ( current->next )
            {
                tail = current;
                current = current->next;
            }
    
            if ( tail == nullptr )
            {
                head = tail;
            }
            else
            {
                tail->next = nullptr;
            }
    
            delete current;
        }
    }
    
        2
  •  0
  •   eshirima    7 年前
    1. 首先, display() 是错误的。您希望更新 temp = temp->next; 它也可以初始化为 node * temp = head 因此不需要第二行。

    2. 你的 delete_node() 可以重新写入:

      if (head->next == NULL) // handles the case that it consists of 1 element
      {
          delete head;
          head = NULL;
      }
      else
      {
          node *nextToEnd = head;
          node *end = head->next;
          while (end->next != NULL)
          {
              nextToEnd = end;
              end = end->next;
          }
          delete end;
          nextToEnd->next = NULL;
      }
      
    3. new keyword