【问题标题】:Reverse of a doubly circular linked list having one element具有一个元素的双向循环链表的逆向
【发布时间】:2020-06-28 05:56:13
【问题描述】:

我怀疑如果我们有一个具有一个节点(即)头的双循环链表,并且如果我写head.next = NULL,那么head.prev 是否也会指向NULL

【问题讨论】:

    标签: data-structures linked-list circular-dependency


    【解决方案1】:

    如果head.next = NULL,则如下所示:

    head.prev 仍指向head,如您的帖子所示。

    【讨论】:

      【解决方案2】:

      如果head.next = NULL,它不会改变head.prev(它仍然会指向HEAD

      您可以运行以下代码来检查它是如何工作的。 (C++实现)

      struct Node 
      { 
          int data; 
          struct Node *next; 
          struct Node *prev; 
      };
      void insertEnd(struct Node** start, int value) 
      { 
          if (*start == NULL) 
          { 
              struct Node* new_node = new Node; 
              new_node->data = value; 
              new_node->next = new_node->prev = new_node; 
              *start = new_node; 
              return; 
          } 
        
          Node *last = (*start)->prev; 
        
          struct Node* new_node = new Node; 
          new_node->data = value; 
        
          new_node->next = *start; 
        
          (*start)->prev = new_node; 
        
          new_node->prev = last; 
        
          last->next = new_node; 
      } 
      int32_t main(){
          FastIO;
          struct Node* start = NULL; 
          insertEnd(&start, 5); 
          cout << start -> next->data << endl;
          cout << start -> prev->data << endl;
          start -> next = NULL;
          if(start->next) cout << "Next is Null\n";
          else cout << "Next is not Null\n";
          if(start->prev) cout << "Prev is Null\n";
          else cout << "Prev is not Null\n";
          return 0;
      }
      

      【讨论】:

        猜你喜欢
        • 2013-11-15
        • 2021-07-05
        • 1970-01-01
        • 2023-03-11
        • 2018-03-28
        • 2011-12-15
        • 1970-01-01
        • 2021-06-01
        • 1970-01-01
        相关资源
        最近更新 更多