链表作业

功能包括:创建链表、删除关键字,删除重复元素,打印链表,清空链表、插入元素

【程序】

#include<stdio.h>
#include<stdlib.h>
typedef struct my_list
{
	int a;
	struct my_list *next; 
}List;

List *Creat();
void Print(List *h);
List *Insert(List *h);
List *Delkey(List *h);
List *Delsame(List *h);
List *Destroy(List *h);
void Menu(int *);

List *Creat()
{
	List *h,*r,*s;
	int a;
	h=(List*)malloc(sizeof(List));
	r=h;
	printf("**Creat a list**\n");
	printf("Input data(s), 0 marks the end!\n");
	printf("Data(s):");
	scanf("%d",&a);
	while(a!=0)
	{
		s=(List*)malloc(sizeof(List));
		s->a=a;r->next=s;r=s;
		scanf("%d",&a);
	}
	r->next='\0';
	printf("\n");
	return h;
}

void Print(List *h)
{
	printf("**Show data(s) of the list**\n");
	printf("Data(s):");
	if(h==NULL||h->next==NULL)//Destroy函数把head清为NULL.Creat函数只输入0,此时的head是随机数,head->next=NULL.
	{
		printf("The list is NULL now!\n");
		printf("\n");
		return ;
	}
	List *p;
	p=h->next;
	while(p!='\0')
	{
		printf("%d->",p->a);
		p=p->next;
	}
	printf("end\n");
	printf("\n");
}

List *Insert(List *h)//位置不存在则插入到尾部
{
	printf("**Insert a data in the list**\n");
	if(h==NULL)
	{
		printf("The list is NULL now, have no position to insert!\n");
		printf("\n");
		return h;
	}
	int a,x;
	List *p,*q,*s;
	printf("Position(after which data):");
	scanf("%d",&x);//插入的位置
	printf("Data(be to insert):");
	scanf("%d",&a);//插入的数据
    s=(List*)malloc(sizeof(List));
	s->a=a;
	p=h;q=h->next;
	while(q!='\0'&&p->a!=x)
	{
		p=q;
		q=q->next;
	}
	s->next=q;
	p->next=s;
	printf("\n");
	return h;
}

List *Delkey(List *h)
{
	printf("**Delete a data of the list**\n");
	if(h==NULL||h->next==NULL)//h==NULL时,p=h->next是不合法的
	{
		printf("The list is NULL now, have no data to delete!\n");
		printf("\n");
		return h;
	}
	List *p,*q;
	int a;
	printf("Data(cut which one):");
	scanf("%d",&a);
	p=h;q=h->next;
	while(q->a!=a&&q->next!='\0')
	{
		p=q;
		q=q->next;
	}
	if(q->a!=a&&q->next=='\0')
	{
		printf("%d is not find!\n",a);
		printf("\n");
	}
	else
	{
		p->next=q->next;
		free(q);
		printf("\n");
	}
	return h;
}

List *Delsame(List *h)
{
	printf("Delete the same data(s)!\n");
	if(h==NULL||h->next==NULL)
	{
		printf("The list is NULL now, have no data(s) to delete!\n");
		printf("\n");
		return h;
	}
	List *p,*q,*r;
	p=h;
	while(p->next!=NULL)
	{	
		p=p->next;
		q=p->next;
		r=p;
		while(q!=NULL)
		{
			if(q->a==p->a)
			{
				r->next=q->next;
				free(q);
				q=r->next;
			}
			else
			{
				r=r->next;
				q=q->next;
			}
		}	
	}
	printf("\n");
	return h;
}

List *Destroy(List *h)//为什么不能用void
{
	if(h==NULL)
	{
		printf("\n");
		return h;
	}
	List *p,*q;
	p=h;q=h->next;
	while(q)
	{
		free(p);
		p=q;
		q=q->next;
	}
	h=NULL;
	printf("\n");
	return h;
}

void Menu(int *order)
{
	printf("*********MENU*********\n");
	printf("1 Creat     2 Insert\n");
	printf("3 Delkey    4 Delsame\n");
	printf("5 Print     6 Destroy\n");
	printf("0 Leave\n");
    printf("----------------------\n");
	printf("Input your order:");
	scanf("%d",order);
	printf("\n");
}

int main()
{
	List *head=NULL;
	int order=1;
	while(order!=0)
	{
		Menu(&order);
		switch(order)
		{
		case 1:head=Destroy(head);head=Creat();break;
		case 2:head=Insert(head);break;
		case 3:Delkey(head);break;
		case 4:Delsame(head);break;
		case 5:Print(head);break;
		case 6:printf("**Clear the list**\n");head=Destroy(head);break;
			//为了Creat()的美观页面,把Destroy()的一条语句调出来打印.
		case 0:printf("Bye-Bye\n");printf("\n");break;
		}
	}
	return 0;
}


 

【运行】

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值