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

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值