数据结构之双向循环链表。

 

 上面是双向循环链表的物理结构。

代码实现如下:

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;
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值