C语言 带有头结点的循环双链表的实现和相关操作。

C语言 带有头结点的循环双链表的实现和相关操作。

#include<stdio.h>
#include<stdlib.h>
typedef int DAT;
typedef struct node
{
	struct node *prior;//指向前一节点 
	DAT data;//存储数据 
	struct node *next;//指向下一节点 
}Node;
typedef struct
{
	Node *head;//指向头节点 
	int size;//存储节点个数 
 }list;
void init_list(list *pL)//初始化链表
{
	pL->head=NULL;
	pL->size=0;
 }
Node* mack_node(DAT data)//创建新的节点
{
	Node *newnode=(Node*)malloc(sizeof(Node));
	if(newnode==NULL) 
	{
		printf("内存分配失败");
		return; 
	}
	newnode->prior=NULL;
	newnode->data=data;
	newnode->next=NULL;
	return newnode;
 } 
void push_back_list(list *pL,DAT data)//尾插法插入链表
{
	Node *newnode=mack_node(data);
	if(pL->head ==NULL)
	{
		pL->head=newnode;
		newnode->prior=newnode;
		newnode->next=newnode;
		pL->size++;
		return;
	}
	Node *cur=pL->head->prior;
	cur->next=newnode;
	newnode->prior=cur;
	newnode->next=pL->head;
	pL->head->prior=newnode;
	pL->size++;
 } 
void push_front_list(list *pL,DAT data)//头插法插入链表
{
	Node *newnode=mack_node(data);
	if(pL->head==NULL)
	{
		pL->head=newnode;
		newnode->prior=newnode;
		newnode->next=newnode;
		pL->size++;
		return;
	}
	Node *cur=pL->head->prior;
	cur->next=newnode;
	newnode->prior=cur;
	newnode->next=pL->head;
	pL->head->prior=newnode;
	pL->head=newnode; 
	pL->size++;
 }
void pop_front_list(list *pL)//头删法删除链表
{
	if(pL->head==NULL)
	{
		printf("链表为空,删除失败。");
		return; 
	}
	Node *cur=pL->head;
	if(cur->next==pL->head)
	{
		pL->head=NULL;
		free(cur);
		pL->size--;
		return;
	}
	cur->prior->next=pL->head->next;
	cur->next->prior=cur->prior;
	pL->head=cur->next;
	free(cur);
	cur=NULL;
	pL->size--; 
 }
void pop_back_list(list *pL)//尾删法删除链表
{
		if(pL->head==NULL)
	{
		printf("链表为空,删除失败。");
		return; 
	}
	Node *cur=pL->head;
	if(cur->next==pL->head)
	{
		pL->head=NULL;
		free(cur);
		pL->size--;
		return;
	}
	cur=cur->prior;
	cur->prior->next=cur->next;
	cur->next->prior=cur->prior;
	free(cur);
	cur=NULL; 
	pL->size--;
 }
void destroy_list(list *pL)//销毁链表
{
	if(pL->head==NULL)
	return;
	Node *cur=pL->head;
	Node *pre=NULL;
	while(cur)
	{
		pre=cur;
		cur=cur->next;
		free(pre);
		if(cur==(pL->head))
		break;
	}
	pL->head=NULL;
	cur=NULL;
	pre=NULL;
	pL->size=0;
 } 
void print_list(list *pL)//打印链表
{
	if(pL->head==NULL)
	{
		printf("空链表\n");
		return; 
	}
	Node *cur=pL->head;
	while(cur)
	{
		printf("<-%d-> ",cur->data);
		if(cur->next==pL->head)
		{
			printf("  %d个节点\n",pL->size);
			return;
		 } 
		cur=cur->next;
	}
 } 
int main()
{
	list List;
	init_list(&List);//初始化链表
	print_list(&List);//打印链表
	push_back_list(&List,1);//尾插法插入链表 
	print_list(&List);//打印链表
	push_back_list(&List,12);//尾插法插入链表 
	print_list(&List);//打印链表	
	push_back_list(&List,123);//尾插法插入链表 
	print_list(&List);//打印链表	
	push_front_list(&List,4);//头插法插入链表
	print_list(&List);//打印链表 
	push_front_list(&List,45);//头插法插入链表
	print_list(&List);//打印链表	
	push_front_list(&List,456);//头插法插入链表
	print_list(&List);//打印链表	
	pop_front_list(&List);//头删法删除链表
	print_list(&List);//打印链表
	pop_back_list(&List);//尾删法删除链表
	print_list(&List);//打印链表	
	destroy_list(&List);//销毁链表
	print_list(&List);//打印链表	
	return 0;
 } 
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值