链表的相关操作

链表的相关操作

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define OK 1
#define ERROR 0

typedef int BOOL;
typedef int DataType;
typedef int Status;

typedef struct LNode
{
	DataType data;
	struct LNode *next;
}LNode;

Status InitListHead(LNode **L,int n)
{
	LNode *p;
	int i;
	srand(time(0));
	(*L)=(LNode *)malloc(sizeof(LNode));
	if(!(*L))
	{
		printf("No enough memory!\n");
		return ERROR;
	}
	(*L)->next=NULL;
	for(i=0;i<n;i++)
	{
		p=(LNode *)malloc(sizeof(LNode));
		if(!p)
		{
			printf("No enough memory!\n");
			return ERROR;
		}
		p->data=rand()%100+1;
		p->next=(*L)->next;
		(*L)->next=p;
	}
	return OK;
}

Status InitListTail(LNode **L,int n)
{
	int i;
	LNode *p,*r;
	srand(time(0));
	(*L)=(LNode *)malloc(sizeof(LNode));
	if(!(*L))
	{
		printf("No enough memory!\n");
		return ERROR;
	}
	(*L)->next=NULL;
	r=(*L);
	for(i=0;i<n;i++)
	{
		p=(LNode *)malloc(sizeof(LNode));
		if(!p)
		{
			printf("No enough memory!\n");
			return ERROR;
		}
		p->data=rand()%100+1;
		p->next=NULL;
		r->next=p;
		r=p;
	}
	return OK;
}

Status ClearList(LNode **L)
{
	LNode *p,*q;
	p=(*L)->next;
	while(p)
	{
		q=p->next;
		free(p);
		p=q;
	}
	(*L)=NULL;
	return OK;
}

BOOL ListEmpty(LNode *L)
{
	if(L==NULL)
		return 1;
	else
		return 0;
}

int ListLength(LNode *L)
{
	int length=0;
	LNode *p;
	p=L->next;
	while(p)
	{
		length++;
		p=p->next;
	}
	return length;
}

Status GetElem(LNode *L,int i,DataType *e)
{
	int j;
	LNode *p;
	j=1;
	p=L->next;
	while(p&&j<i)
	{
		p=p->next;
		j++;
	}
	if(!p||j>i)
		return ERROR;
	(*e)=p->data;
	return OK;
}

Status LocateElem(LNode *L,DataType e)
{
	LNode *p;
	p=L->next;
	while(p)
	{
		if(p->data==e)
		{
			return OK;
		}
		p=p->next;
	}

	return ERROR;
}

Status ListInsert(LNode **L,int i,DataType e)
{
	int j;
	LNode *p,*q;
	j=1;
	p=(*L);
	while(p&&j<i)
	{
		p=p->next;
		j++;
	}
	if(!p||j>i)
		return ERROR;
	q=(LNode *)malloc(sizeof(LNode));
	if(!q)
	{
		printf("No enough memory!\n");
		return ERROR;
	}
	q->data=e;
	q->next=p->next;
	p->next=q;

	return OK;
}

Status ListDelete(LNode **L,int i,DataType *e)
{
	int j;
	LNode *p,*q;
	j=1;
	p=(*L);
	while(p->next&&j<i)
	{
		p=p->next;
		j++;
	}
	if(!(p->next)||j>i)
		return ERROR;
	q=p->next;
	p->next=q->next;
	(*e)=q->data;
	free(q);

	return OK;
}

Status Display(LNode *L)
{
	LNode *p;
	p=L->next;
	while(p)
	{
		printf("%3d",p->data);
		p=p->next;
	}
	printf("\n");
	return OK;
}

void Menu()
{
	printf("-----------------------------------------\n");
	printf("               Menu         Version1.0   \n");
	printf("-----------------------------------------\n");
	printf("   1.InitListHead       2.InitListTail   \n");
	printf("   3.ClearList          4.ListEmpty      \n");
	printf("   5.ListLength         6.ListInsert     \n");
	printf("   7.ListDelete         8.GetElem        \n");
	printf("   9.LocateElem        10.Display        \n");
	printf("  11.Quit                                \n");
	printf("-----------------------------------------\n");
	printf("Please input your choice: ");
}

int main(int argc,char *argv[])
{
	LNode *L;
	int choice,n,i;
	DataType e;
	while(1)
	{
		Menu();
		scanf("%d",&choice);
		switch (choice)
		{
			case 1:
				printf("Please input the number of List elements: ");
				scanf("%d",&n);
				if(InitListHead(&L,n))
					printf("Init success!\n");
				else
					printf("Init failure!\n");
				break;
			case 2:
				printf("Please input the number of list elements: ");
				scanf("%d",&n);
				if(InitListTail(&L,n))
					printf("Init success!\n");
				else
					printf("Init failure!\n");
				break;
			case 3:
				if(ClearList(&L))
					printf("Clear success!\n");
				else
					printf("Clear failure!\n");
				break;
			case 4:
				if(ListEmpty(L))
					printf("The List is empty!\n");
				else
					printf("The List is not empty!\n");
				break;
			case 5:
				printf("The Length of the List is: %d\n",ListLength(L));
				break;
			case 6:
				printf("Please input the index you want to insert: ");
				scanf("%d",&i);
				printf("Please input the element you want to insert: ");
				scanf("%d",&e);
				if(ListInsert(&L,i,e))
					printf("Insert success!\n");
				else
					printf("Insert failure!\n");
				break;
			case 7:
				printf("Please input the index you want to delete: ");
				scanf("%d",&i);
				if(ListDelete(&L,i,&e))
				{
					printf("Delete success! ");
					printf("%d is deleted!\n",e);
				}
				else
					printf("Delete failure!\n");
				break;
			case 8:
				printf("Please input the index you want to get: ");
				scanf("%d",&i);
				if(GetElem(L,i,&e))
					printf("The %dth number is : %d\n",i,e);
				else
					printf("Operation failure!\n");
				break;
			case 9:
				printf("Please input the element you want to locate: ");
				scanf("%d",&e);
				if(LocateElem(L,e))
					printf("Find it!\n");
				else
					printf("Can not find it!\n");
				break;
			case 10:
				Display(L);
				break;
			case 11:
				printf("Quit...!\n");
				exit(0);
			default:
				printf("Illegal input!\n");
				break;
		}
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值