寒假作业-day4

单向链表增删

#include <myhead.h>
typedef struct a
{
	int date;
	struct a *next;
}aa;
//创建节点
aa *Creat()
{
	aa *s=(aa*)malloc(sizeof(aa));
	s->date=0;
	s->next=NULL;
	return s;
}
//头插
aa *insert(aa *head,int num)
{
	aa *s=Creat();
	s->date=num;
	if(NULL==head)
	{
		head=s;
		return head;
	}
	s->next=head;
	head=s;
	return head;
}
//显示
void show(aa *head)
{
	if(head==NULL)
		return;
	aa*p=head;
	while(p!=NULL)
	{
		printf("%d\t",p->date);
		p=p->next;
	}
	puts("");
}
//头删
aa *del(aa *head)
{
	if(NULL==head)
		return head;
	if(head->next==NULL)
	{	
		free(head);
		head=NULL;
		return head;
	}
	aa*p=head;
	head=head->next;
	free(p);
	return head;
}
int main(int argc, const char *argv[])
{
	aa *head=NULL;
	int n;
	printf("please enter n:");
	scanf("%d",&n);
	int num;
	for(int i=0;i<n;i++)
	{
		printf("please enter num:");
		scanf("%d",&num);
		head=insert(head,num);
	}
	show(head);
	head=del(head);
	show(head);
	return 0;
}

双向链表的增删:

#include <myhead.h>
typedef struct a
{
	int date;
	struct a *next;
	struct a *front;
}aa;
//创建节点
aa *Creat()
{
	aa *s=(aa*)malloc(sizeof(aa));
	s->date=0;
	s->next=NULL;
	s->front=NULL;
	return s;
}
//头插
aa *insert(aa *head,int num)
{
	aa *s=Creat();
	s->date=num;
	if(NULL==head)
	{
		head=s;
		return head;
	}
	s->next=head;
	head->front=s;
	head=s;
	return head;
}
//显示
void show(aa *head)
{
	if(head==NULL)
		return;
	aa*p=head;
	while(p!=NULL)
	{
		printf("%d\t",p->date);
		p=p->next;
	}
	puts("");
}
//头删
aa *del(aa *head)
{
	if(NULL==head)
		return head;
	if(head->next==NULL)
	{	
		free(head);
		head=NULL;
		return head;
	}
	aa*p=head;
	head=head->next;
	free(p);
	return head;
}
int main(int argc, const char *argv[])
{
	aa *head=NULL;
	int n;
	printf("please enter n:");
	scanf("%d",&n);
	int num;
	for(int i=0;i<n;i++)
	{
		printf("please enter num:");
		scanf("%d",&num);
		head=insert(head,num);
	}
	show(head);
	head=del(head);
	show(head);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值