本人是西安邮电大学计科专业的一个普通学生,以下是本人对于带头双向循环链表的理解。
先上图
带头双向循环链表的 头节点(head)不存数据
,因为如果数据是char类型的话,存放的数据多了 ,怎么表示?表示不了啊
如果大家对于链表还不是很熟悉的话,请点击传送门,那里有对于单链表的简单解释 传送门
prev:指向上一个结点
next:指向下一个结点
typedef struct ListNode
{
struct ListNode* prev;
struct ListNode* next;
DataType data;
}ListNode;
ListNode* BuyNewNode( DataType x)
{
ListNode* newnode = (ListNode*)malloc(sizeof(ListNode));
assert(newnode);
newnode->data = x;
newnode->next = newnode;
newnode->prev = newnode;
return newnode;
}
ListNode* Find(ListNode* phead, DataType x)
{
assert(phead->next != phead);
ListNode* pos = NULL;
ListNode* cur = phead->next;
while(cur!=phead)
{
if (cur->data==x)
{
pos = cur;
break;
}
cur = cur->next;
}
if(pos)
{
printf("找到!\n");
}
else
{
printf("没找到\n");
}
return pos;
}
这个很重要,头插尾插都能用到
void Insert(ListNode* pos, DataType x)
{
assert(pos);
ListNode* newnode = BuyNewNode(x);
ListNode* head = pos->prev;
head->next = newnode;
newnode->prev = head;
newnode->next = pos;
pos->prev = newnode;
}
void Erease(ListNode* pos)
{
ListNode* Prev = pos->prev;
ListNode* Next = pos->next;
Prev->next = Next;
Next->prev = Prev;
free(pos);
pos = NULL;
}
头插就是在第一个结点前插入,第一个结点就是 phead->next
void PushFront(ListNode* phead, DataType x)
{
ListNode* pos = phead->next;
Insert(pos, x);
}
尾插就是在最后结点插入,也就是在phead之前插入
void PushBack(ListNode* phead, DataType x)
{
ListNode* pos = phead;
Insert(pos, x);
}
头删就是删除phead->next
void PopFront(ListNode* phead, DataType x)
{
ListNode* pos = phead->next;
Erease(pos);
}
尾删就是删除phead->prev
void PopBack(ListNode* phead, DataType x)
{
ListNode* pos = phead->prev;
Erease(pos);
}
int Size(ListNode* phead)
{
assert(phead);
int sum = 0;
ListNode* cur = phead->next;
while (cur!=phead)
{
sum++;
cur = cur->next;
}
return sum;
}
void Print(ListNode* phead)
{
assert(phead);
ListNode* cur = phead->next;
while (cur!=phead)
{
printf("%d ", cur->data);
cur = cur->next;
}
printf("\n");
}
void Destory(ListNode* phead)
{
assert(phead);
ListNode* cur = phead->next;
while (cur!=phead)
{
ListNode* Next = cur->next;
free(cur);
cur = Next;
}
free(phead);
phead = NULL;
}
#pragma once
#include
#include
#include
#include
#include
typedef int DataType;
typedef struct ListNode
{
struct ListNode* prev;
struct ListNode* next;
DataType data;
}ListNode;
typedef struct ListNode ListNode;
//void Init(ListNode** pphead); 初始化头节点
ListNode* BuyNewNode(DataType x);//申请空间
void PushFront(ListNode* phead,DataType x);//头插
void PushBack(ListNode* phead, DataType x);//尾插
void PopFront(ListNode* phead);//头删
void PopBack(ListNode* phead);//尾删
void Insert(ListNode* pos,DataType x);//在指定位置插入
void Erease(ListNode* pos);//在指定位置删除
ListNode* Find(ListNode* phead, DataType x);//寻找
void Print(ListNode* phead);//打印
int Size(ListNode* phead);//计算链表的大小
void Destory(ListNode* phead);//摧毁链表
#define _CRT_SECURE_NO_WARNINGS 1
#include"list.h"
ListNode* BuyNewNode( DataType x)
{
ListNode* newnode = (ListNode*)malloc(sizeof(ListNode));
assert(newnode);
newnode->data = x;
newnode->next = newnode;
newnode->prev = newnode;
return newnode;
}
void PushFront(ListNode* phead, DataType x)
{
ListNode* pos = phead->next;
Insert(pos, x);
}
void PushBack(ListNode* phead, DataType x)
{
ListNode* pos = phead;
Insert(pos, x);
}
void PopFront(ListNode* phead, DataType x)
{
ListNode* pos = phead->next;
Erease(pos);
}
void PopBack(ListNode* phead, DataType x)
{
ListNode* pos = phead->prev;
Erease(pos);
}
//void Init(ListNode** pphead)
//{
// *pphead = BuyNewNode(0);
//}
ListNode* Find(ListNode* phead, DataType x)
{
assert(phead->next != phead);
ListNode* pos = NULL;
ListNode* cur = phead->next;
while(cur!=phead)
{
if (cur->data==x)
{
pos = cur;
break;
}
cur = cur->next;
}
if(pos)
{
printf("找到!\n");
}
else
{
printf("没找到\n");
}
return pos;
}
void Insert(ListNode* pos, DataType x)
{
assert(pos);
ListNode* newnode = BuyNewNode(x);
ListNode* head = pos->prev;
head->next = newnode;
newnode->prev = head;
newnode->next = pos;
pos->prev = newnode;
}
void Erease(ListNode* pos)
{
ListNode* Prev = pos->prev;
ListNode* Next = pos->next;
Prev->next = Next;
Next->prev = Prev;
free(pos);
pos = NULL;
}
void Print(ListNode* phead)
{
assert(phead);
ListNode* cur = phead->next;
while (cur!=phead)
{
printf("%d ", cur->data);
cur = cur->next;
}
printf("\n");
}
int Size(ListNode* phead)
{
assert(phead);
int sum = 0;
ListNode* cur = phead->next;
while (cur!=phead)
{
sum++;
cur = cur->next;
}
return sum;
}
void Destory(ListNode* phead)
{
assert(phead);
ListNode* cur = phead->next;
while (cur!=phead)
{
ListNode* Next = cur->next;
free(cur);
cur = Next;
}
free(phead);
phead = NULL;
}
#define _CRT_SECURE_NO_WARNINGS 1
#include"list.h"
void Test1()
{
//ListNode* plist = NULL;
ListNode* plist = BuyNewNode(0);
//Init(&plist);
PushFront(plist, 1);
PushFront(plist, 2);
PushFront(plist, 3);
PushBack(plist, 4);
Print(plist);
ListNode* pos= Find(plist,2);
Insert(pos, 200);
Print(plist);
PopFront(plist);
Print(plist);
PopBack(plist);
Print(plist);
printf("%d\n",Size(plist));
Destory(plist);
}
int main(void)
{
Test1();
return 0;
}