双向循环链表

#include<stdio.h>
#include<stdlib.h>
typedef struct Node{
	int data;
	struct Node* pre;
	struct Node* next;
}Node;
Node* initlist()
{
	Node* head=(Node*)malloc(sizeof(Node));
	head->data=0;
	head->next=head;
	head->pre=head;
}
void head_insert(Node* head,int data)
{
	Node* node=(Node*)malloc(sizeof(Node));
    node->data=data;
    if(head->data==0)
    {
    	node->pre=head;
    	node->next=head->next;
		head->next=node;
		head->pre=node;
		head->data++;
	}
	else
	{
		node->next=head->next;
		node->pre=head;
		head->next->pre=node;
		head->next=node;
		head->data++;	
	}
}

void tail_list(Node* head,int data)
{
	Node* node=(Node*)malloc(sizeof(Node));
	Node* n=(Node*)malloc(sizeof(Node));
	n->data=data;
	node=head;
	while(node->next!=head)
	{
		node=node->next;
	}
	n->pre=node;
	n->next=head;
	node->next=n;
	head->pre=n;
	head->data++;
}
void delete_list(Node* head,int data)
{
	Node* node=(Node*)malloc(sizeof(Node));
	node=head->next;
	while(node!=head)
	{
		if(node->data==data)
		{
			node->pre->next=node->next;
			node->next->pre=node->pre;
			free(node);
			head->data--;
		}
			node=node->next;
	}
}
void print_list(Node* head)
{
	Node* node=(Node*)malloc(sizeof(Node));
	node=head->next; 
	while(node!=head)
	{
		printf("%d ",node->data);	
		node=node->next; 
	}
}
int main()
{
	Node* list=initlist();
	head_insert(list,1);
	head_insert(list,2);
	head_insert(list,3);
	tail_list(list,4);
	tail_list(list,5);
	print_list(list);
	printf("\n");
	delete_list(list,2);
	print_list(list);
	return 0;
	
} 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值