简体   繁体   English

C ++了解一个循环的双向链表

[英]c++ understanding a circular doubly linked list

I'm having a problem figuring out how to set my ptr's for prev and next when I add a node with at least one existing node. 添加一个至少包含一个现有节点的节点时,我在解决如何为上一个和下一个设置ptr时遇到问题。 Adding the first node is easy, just setting the ptrs to the front. 添加第一个节点很容易,只需将ptrs设置在最前面即可。 I need help viewing this mentally, also this program is a queue so each node gets added to the back of the list. 我需要心理上的帮助,这个程序也是一个队列,因此每个节点都被添加到列表的后面。

if (Empty())
    {
        front = new qnode;
        front->next=front;
        front->prev=front;
        front->data = item;
    }
    else if (front->prev=front)
    {
        front->prev = new qnode;
        front->prev->next= front;
        front->next=front->prev;
        front->prev->data = item;
    }
    else
    {

    }

What i have now still not getting it 我现在仍然没有得到它

else
    {
        front->prev= new qnode;
        front->prev->data= item;
        front->prev->next=front;
        front->prev=front->prev->prev;

    }

I hope this image helps a bit 希望这张图片对您有所帮助 在此处输入图片说明

I have created pictures for 1 item 2 items and 3 items 我为1个项目,2个项目和3个项目创建了图片

the pointers just point to the actual object meaning the black rectangle is the whole object if front being blue and prev being brown(those are just there as references) 指针仅指向实际对象,如果正面为蓝色而上一个为棕色,则黑色矩形是整个对象(这些都作为参考)

I really hope this helps linked list can get really tricky and drawing always helps me. 我真的希望这可以帮助链表变得非常棘手,并且绘图总是对我有帮助。

so to add the item at the front of the list you have some code like this: 因此,要将该项目添加到列表的最前面,您需要使用以下代码:

 //ok first I'll define some variables for you
 //last === the last node in the list
 //head === the first node in the list
 //node === the new node you are adding;
 qnode node = new qnode;
 node.data = data; //whatever data you are holding
 node->next = last; //last element in the list since it is circular;
 node->prev = head; //you want the new node to point the the first node since it's getting added before that;
 head->next = node; //you want the head of the node to point to the new node not the last item
 last->prev = node; //last node now should point to the new node you just added not the head;

在此处输入图片说明

I am not sure why you need an else if , as I think the case with one element is not different from that with multiple elements. 我不确定为什么else if ,因为我认为一个元素的情况与多个元素的情况没有什么不同。 In any case you have a syntax error there - you wanted to write == instead of = . 无论如何,那里都存在语法错误-您想编写==而不是=

More to the point, what you want to do is: 更重要的是,您想要做的是:

  • Create a new qnode 创建一个新的qnode
  • Set the data of the new node to the item 将新节点的数据设置为该项
  • Set the next of the new node to the front 将新节点的下一个设置为最前面
  • Set the prev of the new node to front->prev 将新节点的prev设置为front-> prev
  • Set the prev of the front to the new node. 将前面板的prev设置为新节点。

You may check for yourself on a piece of paper how this works. 您可以在纸上检查一下它是如何工作的。

In a circular doubly linked list, you have 2 pointers, one to the head and one to the tail (last element in the list). 在圆形双向链接列表中,您有2个指针,一个指向头部,一个指向尾部(列表中的最后一个元素)。 If a element has a next, the next element's prev should be the element pointing at it through it's next element. 如果一个元素具有下一个元素,则下一个元素的prev应该是通过下一个元素指向它的元素。 Your head pointer should never move unless the head element is removed. 除非删除head元素,否则您的头部指针永远不会移动。 Your tail will move along always ensuring that the element it points to has head as it's next element. 您的尾巴将一直移动,始终确保其指向的元素在下一个元素时具有头部。 Hopefully this helps 希望这会有所帮助

Since you always add at the end, the case of a queue with a single element is identical with the case of a list with multiple elements. 由于您总是在末尾添加,因此具有单个元素的队列的情况与具有多个元素的列表的情况相同。 Take a paper and a pen, make some sketches and it will come very easy to you. 拿纸和笔,做一些草图,对您来说很容易。

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

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