循环双向链表

2020-05-29 23:41:51

上一页下一页循环双向链表是一种线性数据结构,其中的元素以节点的形式存储。每个节点包含三个子元素。存储元素值的数据部分、存储指向上一个节点的指针的上一个部分和存储指向下一个节点的指针的下一个部分,如下图所示:

第一个节点(也称为Head)始终用作遍历列表的引用。最后一个元素包含指向作为下一个的第一个元素的链接,第一个元素包含作为上一个的最后一个元素的链接。可以将双向链接的圆形可视化为节点链,其中每个节点都指向上一个节点和下一个节点。

在C语言中,可以使用Structure创建节点。在C++中,可以使用类创建循环双向链表,使用结构创建节点。LinkedList类包含作为类成员的Node。

在Java、Python、C#和PHP中,循环双向链表可以表示为一个类,节点可以表示为一个单独的类。LinkedList类包含Node类类型的引用。

//node structurestruct Node{int data;Node*Next;Node*Prev;};类LinkedList{public:node*head;public://创建空LinkedList LinkedList(){head=null;}}的构造函数;

#node structureclass Node:#创建新节点的构造函数def__init__(self,data):self.data=data self.next=None self.prev=None#class LinkedList:#创建空的LinkedList的构造函数def__init__(Self):self.head=None。

//节点结构类Node{int data;Node Next;Node Prev;};类LinkedList{Node Head;//创建空LinkedList LinkedList(){head=null;}}的构造函数;

//node structureclass Node{public int data;public Node Next;public Node prev;};class LinkedList{public Node head;//创建空LinkedList public LinkedList(){head=null;}}的构造函数;

//node structureclass Node{public$data;public$next;public$prev;}类LinkedList{public$head;//创建空LinkedList public函数的构造函数__Construction(){$this->;head=null;}};

#include<;iostream>;using命名空间std;//node structurestruct Node{int data;Node*Next;Node*prev;};class LinkedList{public:node*head;public://创建空的LinkedList(){head=null;}};//测试代码int main(){//创建空的LinkedList LinkedList MyList;//add first。node*first=new Node();first-gt;data=10;first->;next=null;first->;prev=null;//链接头部节点MyList.head=first;//链接头部first节点的下一个节点->;next=MyList.head;//链接头部MyList.head->;prev=first;//添加第二个节点。Node*Second=new Node();Second->;Data=20;Second->;Next=null;//链接第一个节点Second->;prev=first;First->;Next=Second;//链接头部为Second的节点的下一个->;Next=MyList.head;//链接头部MyList.head->;prev=Second;//添加第三个节点。节点*Third=new Node();Third->;Data=30;Third->;Next=NULL;//正在链接第二个节点Third->;prev=Second;Second->;Next=Third;//链接头部为Third->;Next=MyList.head;//链接头部MyList.head->;Prev=Third;Return 0;}。

#include<;stdio.h>;#include<;stdlib.h>;//node structurestruct Node{int data;struct Node*next;struct Node*prev;};//测试代码int main(){//创建名为MyList struct Node*MyList=null的头节点;//添加第一个节点。struct Node*first;//分配堆中的第二个节点first=(struct Node*)malloc(sizeof(Struct Node));first->;data=10;first->;next=null;first->;prev=null;//正在链接头部节点MyList=first;//链接头部节点first->;next=MyList;//链接头部MyList->;struct Node*Second;//分配堆中的第二个节点Second=(struct Node*)malloc(sizeof(Struct Node));Second->;data=20;Second->;Next=null;//正在与第一个节点Second链接-&>;prev=first;first->;next=Second;//链接头部为Second的节点的下一个->;Next=MyList;//链接头部MyList的上一个-。struct Node*Third;//分配堆中的第三个节点Third=(struct Node*)malloc(sizeof(Struct Node));Third->;data=30;Third->;Next=null;//与第二个节点Third->;prev=Second;Second->;Next=Third;//链接头为Third的节点的下一个-&>;Next=MyList;//链接头MyList的上一个-。

#node structureclass Node:#创建新节点的构造函数def__init__(self,data):self.data=data self.next=None self.prev=None#class LinkedList:#创建空的LinkedList def__init__(Self):self.head=None#测试代码#创建一个空的LinkedList MyList=LinkedList()#add first nokedList。#添加第二个节点。Second=Node(20)#使用第一个节点链接。prev=firstfirst.next=Second#链接头的下一个节点。Next=MyList.head#链接头的前一个MyList.head.prev=Second;#添加第三个节点。Third=Node(30)#使用第二个节点链接。prev=Second。Next=Third#链接头三的下一个节点。Next=MyList.head#链接上一个。

//node structureclass Node{int data;Node Next;Node Prev;};class LinkedList{Node head;//创建空LinkedList LinkedList(){head=null;}}的构造函数;//测试代码公共类实现{public static void main(string[]args){//创建空LinkedList LinkedList MyList=new LinkedList();//添加第一个节点。Node First=new Node();first.data=10;first.next=null;first.prev=null;//链接头部节点MyList.head=first;//链接头部为first的下一个节点.next=MyList.head;//链接头部MyList.head.prev=first;//添加第二个节点。Node Second=new Node();Second d.data=20;Secd.next=null;//链接第一个节点Second。prev=first;first.next=Second;//链接头部为Second的节点的下一个。next=MyList.head;//链接头部MyList.head.prev=Second;//添加第三个节点。Node Third=new Node();Third d.data=30;Third d.next=null;//链接第二个节点Third.prev=Second;Secd.Next=Third;//链接头部为Third的节点的下一个.Next=MyList.head;//链接头部MyList.head.prev=Third;}}。

使用system;//node structureclass Node{public int data;public Node Next;public Node prev;};class LinkedList{public Node head;//构造函数创建空的LinkedList public LinkedList(){head=null;}};//测试代码类实现

#include<;iostream&>使用命名空间std;//node structurestruct Node{int data;Node*Next;Node*prev;};class LinkedList{public:node*head;public://创建空的LinkedList LinkedList(){head=null;}//显示列表的内容void PrintList(){Node*temp=head;if(temp!=null){cout。data<;<;";";;temp=temp->;next;if(temp==head)Break;}}Else{cout<;<;";\n列表为空。";;};//测试代码int main(){//创建空的LinkedList LinkedList MyList;//添加第一个节点。node*first=new Node();first-gt;data=10;first->;next=null;first->;prev=null;//链接头部节点MyList.head=first;//链接头部first节点的下一个节点->;next=MyList.head;//链接头部MyList.head->;prev=first;//添加第二个节点。Node*Second=new Node();Second->;Data=20;Second->;Next=null;//链接第一个节点Second->;prev=first;First->;Next=Second;//链接头部为Second的节点的下一个->;Next=MyList.head;//链接头部MyList.head->;prev=Second;//添加第三个节点。节点*Third=new Node();Third->;Data=30;Third->;Next=null;//正在链接第二个节点Third-&>;prev=Second;Second->;Next=Third;//链接头部为Third->;Next=MyList.head;//链接头部MyList.head->;Prev=Third;//打印列表MyList.PrintList()的内容。

#include<;stdio.h>;#include<;stdlib.h>;//node structurestruct Node{int data;struct Node*Next;struct Node*prev;};//显示listvoid PrintList(struct Node*head_ref){struct Node*temp=head_ref;if(head_ref!=null){printf(";\n。temp=temp->;next;if(temp==head_ref)Break;}}Else{printf(";)\n列表为空。";);}//测试代码int main(){//创建名为MyList struct Node*MyList=null的头节点;//添加第一个节点。struct Node*first;//分配堆中的第二个节点first=(struct Node*)malloc(sizeof(Struct Node));first->;data=10;first->;next=null;first->;prev=null;//正在链接头部节点MyList=first;//链接头部节点first->;next=MyList;//链接头部MyList->;struct Node*Second;//分配堆中的第二个节点Second=(struct Node*)malloc(sizeof(Struct Node));Second->;data=20;Second->;Next=null;//正在与第一个节点Second链接-&>;prev=first;first->;next=Second;//链接头部为Second的节点的下一个->;Next=MyList;//链接头部MyList的上一个-。struct Node*Third;//分配堆中的第三个节点Third=(struct Node*)malloc(sizeof(Struct Node));Third->;data=30;Third->;Next=null;//与第二个节点Third->;prev=Second;Second->;Next=Third;//链接头为Third的节点的下一个-&>;Next=MyList;//链接头MyList的上一个-。

#node structureclass Node:#创建新节点的构造函数def__init__(self,data):self.data=data self.next=one self.prev=None#class LinkedList:#创建空的LinkedList def__init__(Self):self.head=None#显示列表的内容def PrintList(Self):temp=self.head if(temp。)While(True):print(temp.data,end=";";)temp=temp.next if(temp==self.head):Break Else:打印(";\n列表为空。";)#测试代码#创建一个空的LinkedList MyList=LinkedList()#添加第一个节点。first=Node(10)#链接头部节点MyList.head=first#链接下一个节点。#添加第二个节点。Second=Node(20)#使用第一个节点链接。prev=firstfirst.next=Second#链接头的下一个节点。Next=MyList.head#链接头的前一个MyList.head.prev=Second;#添加第三个节点。Third=Node(30)#使用第二个节点链接。prev=Second。Next=Third#链接头三的下一个节点。Next=MyList.head#链接上一个。

//节点结构类Node{int data;Node Next;Node Prev;};类LinkedList{Node Head;//创建空LinkedList LinkedList(){head=null;}//显示列表的内容void PrintList(){Node temp=new Node();temp=this.head;if(temp!=null){System.out.print(";\n列表。temp=temp.next;if(temp==this.head)Break;}}Else{System.out.print(";)\n列表为空。";);};//测试代码公共类实现