简体   繁体   English

如何显示链表(双向链表)的遍历?

[英]How to display the traverse of a linked list (doubly linked list)?

My assignment is to create a list in our main function and then display those numbers as a list. 我的任务是在我们的主要功能中创建一个列表,然后将这些数字显示为列表。 Then, we are supposed to show that it also traverses by displaying for example a reverse of that list. 然后,我们应该通过显示例如该列表的反向来显示它也遍历。 My problem is that I cannot seem to get the traversing part to display.Basically, anything that does not have a comment line after, the professor had already provided. 我的问题是,我似乎无法显示遍历部分。基本上,任何已经没有注释行的内容,教授都已经提供了。 Anything that has a comment line, I added. 我添加了任何带有注释行的内容。 Thanks 谢谢

    #include <iostream>

     using namespace std;
     struct Node {
        int data;
        Node* next;
        Node* prev; //
        Node(int d=0, Node* p=0, Node* n=0) : data(d), next(p), prev(n) {} //
        Node* insert_after(int value);
        Node* delete_();
        void display_list_after() ;
        void display_list_prev();
        };

    Node* Node::insert_after(int value) {
    Node *p = new Node(value);
    Node *n = p; //
    p->next = next;
    p->prev = this; //
    next = p;
    return p;
   }
    Node* Node::delete_(){ //
    Node *p = this;
    Node *n = 0;
       if (prev != 0){ //
          prev->next = next; //
            n = prev; //
        }

       if (next != 0) { //
        next = prev->prev; //

        if (n == 0){ //
            n = next; //
          }
      }
         delete p;
         return n; //
      }
      void Node::display_list_after() {
      Node *head = this;
        while(head != 0) {
       cout << head->data << " ";
        head = head->next;
      }
        cout << '\n';
     }

       void Node::display_list_prev() {
          Node *tail = this; //
          while(tail != 0) { //
          cout << tail->data << " "; //
         tail = tail->prev; //
      }
       cout<<'\n'; //

      }
     int main(){
     Node *head= new Node(10);
     Node *tail = head->insert_after(40);
     Node *p = head->insert_after(20);
     p = p->insert_after(25);
     p = p->insert_after(30);
     p = p->insert_after(35);
     p->insert_after(40);
     head->display_list_after();
     tail->display_list_prev(); //
       }

The problem with your code is in the node insertion routine: 您代码的问题在于节点插入例程中:

Node* Node::insert_after(int value) {
    Node *p = new Node(value);
    Node *n = p; //
    p->next = next; // this is right (usually NULL)
    p->prev = prev; // this is wrong (this node is the previous of the next one)
    next = p;
    prev = n; // this is also wrong (previous is not the next one)
    return p; // this is right since this is the next node
    return n; // this is the same stuff and will never get executed
   }

change it to: 更改为:

Node* Node::insert_after(int value) {
    Node *p = new Node(value);
    Node *n = p; //
    p->next = next;
    p->prev = this; // this is the previous node for the next one
    next = p;
    return p;
}

and tail needs to be initialized too: tail需要初始化:

tail = p->insert_after(40);

Try it out 试试看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM