单循环链表

#include<stdio.h>
#include<stdlib.h>
typedef struct Node{
	struct Node* next;
	int data; 
}Node;
Node* initlist()
{
	Node* L=(Node*)malloc(sizeof(Node));
	L->data=0;
	L->next=L;
}
void head_insert(Node* L,int data)
{
	Node* node = (Node*)malloc(sizeof(Node));
	node->data=data;
	node->next=L->next;
	L->next=node;
}
void tail_insert(Node* L,int data)
{
	Node* node=(Node*)malloc(sizeof(Node));
	Node* n=(Node*)malloc(sizeof(Node));
	n=L;
	node->data=data;
	while(n->next!=L)
	{
		n=n->next;
	}
	n->next=node;
	node->next=L;
}
void delete_list(Node* L,int data)
{
	Node* pre=(Node*)malloc(sizeof(Node));
	Node* now=(Node*)malloc(sizeof(Node));
	now=L->next;
	while(now->next!=L)
	{
		if(now->data==data)
		{
			pre->next=now->next;
			free(now);
			break;
		}
		else
		{
			pre=now;
			now=now->next; 
		}
	}
}
void print_list(Node* L)
{
	Node* node=(Node*)malloc(sizeof(Node));
	node=L->next;
	while(node!=L)
	{
		printf("%d ",node->data);
		node=node->next;
	}
	printf("NULL");
}

int main()
{
	Node* l=initlist();
	head_insert(l,1);
	head_insert(l,2);
	head_insert(l,3);
	tail_insert(l,4);
	tail_insert(l,5);
	delete_list(l,4);
	print_list(l);
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值