双向链表
想来个维基百科上的解释:双向链表,又稱為双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱。所以,从双向链表中的任意一个结点开始,都可以很方便地访问它的前驱结点和后继结点。
图片
双向循环链表是结合了单链表和单循环链表,也就是将双链表的头和尾指针指向的 NULL 改为相互指向,即可;且判断条件克根据单循环链表以及双链表来做小改动
关于双链表的插入和删除的图分别如下
代码部分
#include <stdio.h>
#include <stdlib.h>
typedef int ElemType;
typedef struct Node
{
ElemType data;
struct Node *next;
struct Node *prior;
}Nose;
typedef struct Node *LinkList;
LinkList Creat_List()
{
int len;
printf("Please enter the length of the doubly circular linked list: ");
scanf("%d", &len);
LinkList pHead = (LinkList)malloc(sizeof(Node));
if (pHead == NULL)
{
printf("Insufficient memory space!!");
exit(0);
}
pHead->data = 0;
pHead->next = NULL;
pHead->prior = NULL;
LinkList pTail = pHead;
int value;
for (int i = 0; i < len; i++)
{
printf("Please enter the element of the node:");
scanf("%d", &value);
LinkList pNew = (LinkList)malloc(sizeof(Node));
if (pNew == NULL)
{
printf("Insufficient memory space!!");
exit(0);
}
pNew->data = value;
pNew->next = NULL;
pNew->prior = pTail;
pTail->next = pNew;
pTail = pNew;
}
return pHead;
}
void Traver_list(LinkList pHead)
{
LinkList p = pHead->next;
while (p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
int GetList_Length(LinkList pHead)
{
int len = 0;
LinkList p = pHead->next;
while(p != NULL)
{
++len;
p = p->next;
}
return len;
}
void Insert_List(LinkList &pHead)
{
int pos;
int value;
LinkList p = pHead;
LinkList pt;
printf("Please enter a location to insert:");
scanf("%d", &pos);
if (pos > 0 && pos < GetList_Length(pHead) + 1)
{
printf("Please enter the value to insert:");
scanf("%d", &value);
LinkList pNew = (LinkList)malloc(sizeof(Node));
if (pNew == NULL)
{
printf("Insufficient memory space!!");
exit(0);
}
while(1)
{
--pos;
if (pos == 0)
{
break;
}
p = p->next;
}
pt = p->next;
pNew->data = value;
pNew->next = pt;
if (pt != NULL)
{
pt->prior = pNew;
}
pNew->prior = pHead;
p->next = pNew;
}
else
{
printf("The enter location is illegal!!\n");
}
}
void Del_List(LinkList &pHead, ElemType &e)
{
int pos;
printf("Please enter the location to delete :");
scanf("%d", &pos);
if (pos > 0 && pos < GetList_Length(pHead) + 1)
{
LinkList p = pHead;
LinkList pt;
while(1)
{
--pos;
if (0 == pos)
{
break;
}
p = p->next;
}
pt = p->next->next;
e = p->next->data;
free(p->next);
p->next = pt;
if (NULL != pt)
{
pt->prior = p;
}
}
else
{
printf("The enter location is illegal!!\n");
}
}
bool Clear_list(LinkList &pHead)
{
LinkList pt = NULL;
while(pHead != NULL)
{
pt = pHead->next;
free(pHead);
if (NULL != pt)
{
pt->prior = NULL;
}
pHead = pt;
}
if (pHead == NULL)
{
return true;
}
else
{
return false;
}
}
int main()
{
LinkList pHead = Creat_List(); //Creat a doubly linked list
Traver_list(pHead);//Print list
Insert_List(pHead); //Insert element in the doubly linked list
Traver_list(pHead);
int e;
Del_List(pHead, e); //Delete a node of the doubly linked list
printf("The value of the deleted node is: %d\n", e);
Traver_list(pHead);
if (Clear_list(pHead)) //Start emptying the doubly linked list
{
printf("Doubly linked list has been emptied!!\n");
}
return 0;
}