Linux C 单链表实现

#include <stdio.h>
#include <malloc.h>
typedef struct Link_node
{
	int data;
	struct Link_node *next;
}Lnode;
int InitList(Lnode *head)
{
	head = (struct Link_node *)malloc(sizeof(struct Link_node));
	head->next = NULL;
	return 0;
}
int Link_end(Lnode *head)
{
	int i,a=0;
	Lnode *p = head,*q = head;
	if(head = NULL)
	{
		printf("Not Init Link_list Head!\n");
		return -1;
	}
	printf("Please Input 5 number : ");
	for(i=0;i<5;i++)
	{
		scanf("%d",&a);
		p = (struct Link_node *)malloc(sizeof(struct Link_node));
		p->data = a;
		p->next = NULL;
		q->next = p;
		q = p;
	}
}
int Link_head(Lnode *head)
{	
	int i,a=0;
	Lnode *p=head;
	if(head->next = NULL)
	{
		printf("Not Init Link_list Head!\n");
		return -1;
	}
	printf("Please Input 5 number : ");
	for(i=0;i<5;i++)
	{
		scanf("%d",&a);
		p=(struct Link_node *)malloc(sizeof(struct Link_node));
		p->next = head->next;
		p->data = a;
		head->next = p;
	}
	return 0;
}
int clear_link(Lnode *head)
{
	Lnode *p = head,*q = head;
	p->next = head->next;
	while( p->next != NULL )
	{
		q = p->next;
		p->next = q->next;
		free(q);
	}
	free(p);
	return 0;
}
int Display(Lnode *head)
{
	Lnode *p = head;
	p = p->next;
	while(p->next != NULL)
	{
		printf("%d\t",p->data);
		p = p->next;
	}
	printf("%d\n",p->data);
	return 0;
}
int insert(Lnode *head,int n)
{
	Lnode *p = head;
	Lnode *in = (struct Link_node*)malloc(sizeof(struct Link_node));
	in->data = 100;
	n--;
	while(n)
	{
		p = p->next;
		n--;
	}
	in->next = p->next;
	p->next = in;
	return 0;
}
int delete(Lnode *head,int n)
{
	Lnode *p = head;
	Lnode *q = head;
	n--;
	while(n)
	{
		p = p->next;
		n--;
	}
	q = p->next;
	p->next = q->next;
	free(q);
	return 0;
}
int add(Lnode *head)
{
	Lnode *p = head;
	Lnode *add = (struct Link_node*)malloc(sizeof(struct Link_node));
	add->data = 200;
	add->next = NULL;
	while(p->next != NULL)
	{
		p = p->next;
	}
	p->next = add;
	return 0;
	
}
int main()
{
	Lnode *head = (struct Link_node *)malloc(sizeof(struct Link_node));
	InitList(head);
	Link_head(head);
	Display(head);
	insert(head,1);
	Display(head);
	delete(head,1);
	Display(head);
	add(head);
	Display(head);
	clear_link(head);
	return 0;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值