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;//指向头节点 
	unsigned 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;
		pL->size++;
		return;
	}
	Node *cur=pL->head;
	while(cur->next)
	{
		cur=cur->next;
	}
	cur->next=newnode;
	newnode->prior=cur;
	pL->size++;
 } 
void push_front_list(list *pL,DAT data)//头插法插入链表
{
	Node *newnode=mack_node(data);
	if(pL->head==NULL)
	{
		pL->head=newnode;
		pL->size++;
		return;
	}
	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==NULL)
	{
		free(cur);
		pL->head=NULL;
		cur=NULL;
		pL->size--;
		return;
	}
	pL->head=cur->next;
	pL->head->prior=NULL;
	free(cur);
	cur=NULL;
	pL->size--;
 }
void pop_back_list(list *pL)//尾删法删除链表
{
	if(pL->head==NULL)
	{
		printf("链表为空,删除失败。");
		return; 
	}
	Node *cur=pL->head;
	Node *pre=NULL; 
	if(cur->next==NULL)
	{
		free(cur);
		pL->head=NULL;
		cur=NULL;
		pL->size--;
		return;
	}
	while(cur->next)
	{
		pre=cur;
		cur=cur->next;
	}
	free(cur);
	pre->next=NULL;
	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;
		free(pre);
		pre=NULL;
		cur=cur->next;
	 } 
	pL->head=NULL;
	pL->size=0;
 } 
void print_list(list *pL)//打印链表
{
	if(pL->head==NULL)
	{
		printf("空链表\n");
		return;
	}
	Node *cur=pL->head;
	printf("NULL");
	while(cur)
	{
		printf("<- %d -> ",cur->data);
		cur=cur->next;
	 }
	 printf("NULL  %d个节点\n",pL->size);	
 } 
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; 
 } 
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值