上面是双向循环链表的物理结构。
代码实现如下:
list.h(函数的声明和头文件的引用)
test.c(函数之间的调用关系)
list.c(每个函数功能的具体实现)
#pragma once
#include<stdio.h>
#include<assert.h>
#include<malloc.h>
#include<stdlib.h>
#include<stdbool.h>
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;
}