顺序表的相关操作

顺序表的相关操作

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAXN 100
#define OK 1
#define ERROR 0

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

typedef struct SeqList
{
	DataType data[MAXN];
	int length;
}SeqList;

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

Status InitList(SeqList *L)
{
	L->length=0;
	return OK;
}

BOOL ListEmpty(SeqList L)
{
	if(L.length==0)
		return 1;
	else
		return 0;
}

Status ClearList(SeqList *L)
{
	memset(L->data,0,sizeof(L->data));
	L->length=0;
	return OK;
}

Status GetElem(SeqList L,int i,DataType *e)
{
	if(i<1||i>L.length||L.length==0)
		return ERROR;
	else
	{
		(*e)=L.data[i];
		return OK;
	}
}

int LocateElem(SeqList L,DataType e)//查找成功返回在顺序表中的位置,否则
{   
	int i;                            //查找失败则返回0
	for(i=L.length;i>=1;i--)
	{
		if(L.data[i]==e)
			break;
	}
	return i;
}

Status ListInsert(SeqList *L,int i,DataType e)
{
	int k;
	if(L->length==MAXN)//顺序表已满
		return ERROR;
	if(i<1||i>L->length+1)//插入位置错误
		return ERROR;
	if(i==L->length+1)//插入位置为结尾
	{
		L->data[i]=e;
		(L->length)++;
		return OK;
	}
	else
	{
		for(k=L->length;k>=i;k--)
		{
			L->data[k+1]=L->data[k];
		}
		L->data[i]=e;
		(L->length)++;
		return OK;
	}
}

Status ListDelete(SeqList *L,int i,DataType *e)
{
	int k;
	if(L->length==0)//顺序表为空
		return ERROR;
	if(i<1||i>L->length)//删除位置错误
		return ERROR;
	*e=L->data[i];
	for(k=i+1;k<=L->length;k++)
		L->data[k-1]=L->data[k];
	(L->length)--;
	return OK;
}

int ListLength(SeqList L)
{
	return L.length;
}

Status ListUnion(SeqList *La,SeqList Lb)//简单的合并
{
	int la_len=ListLength(*La);
	int lb_len=ListLength(Lb);
	int i;
	DataType e;
	for(i=1;i<=lb_len;i++)
	{
		GetElem(Lb,i,&e);
		if(!LocateElem(*La,e))
		{
			ListInsert(La,++la_len,e);
		}
	}
	return OK;
}

Status Display(SeqList L)
{
	int i;
	for(i=1;i<=L.length;i++)
		printf("%3d",L.data[i]);
	printf("\n");
	return OK;
}

int main(int argc,char *argv[])
{
	SeqList L;
	SeqList Lb;
	int i,choice,index;
	DataType e;
	while(1)
	{
		Menu();
		scanf("%d",&choice);
		switch (choice)
		{
			case 1:
				if(InitList(&L))
					printf("Init success!\n");
				else 
					printf("Init failure!\n");
				break;
			case 2:
				if(ListEmpty(L))
					printf("The List is empty!\n");
				else
					printf("The List is not empty!\n");
				break;
			case 3:
				if(ClearList(&L))
					printf("Clear success!\n");
				else
					printf("Clear failure!\n");
				break;
			case 4:
				printf("Please input the index of the element:");
				scanf("%d",&index);
				if(GetElem(L,index,&e))
					printf("Get it! The element is: %d\n",e);
				else
					printf("Sorry! The index is illegal!\n");
				break;
			case 5:
				printf("Please input the element you want to check:");
				scanf("%d",&e);
				index=LocateElem(L,e);
				if(index)
					printf("Find it! The index is: %d\n",index);
				else
					printf("Sorry! Can not find it!\n");
				break;
			case 6:
				printf("Please input the index you want to insert:");
				scanf("%d",&index);
				printf("Please input the element you want to insert:");
				scanf("%d",&e);
				if(ListInsert(&L,index,e))
					printf("Insert success!\n");
				else
					printf("Insert failure!\n");
				break;
			case 7:
				printf("Please input the index you want to delete:");
				scanf("%d",&index);
				if(ListDelete(&L,index,&e))
					printf("Delete success! The element is: %d\n",e);
				else
					printf("Delete failure!\n");
				break;
			case 8:
				printf("The length of the list is: %d\n",ListLength(L));
				break;
			case 9:
				printf("Before union the list please create List Lb!\n");
				printf("Please intput the length of List Lb:");
				scanf("%d",&(Lb.length));
				printf("Please input the element of List Lb:");
				for(i=1;i<=Lb.length;i++)
					scanf("%d",&(Lb.data[i]));
				if(ListUnion(&L,Lb))
				{
					printf("Union success!\n");
					printf("After union: ");
					Display(L);
				}
				else
					printf("Union faliure!\n");
				break;
			case 10:
				Display(L);
				break;
			case 11:
				printf("Quit...\n");
				exit(0);
			default:
				printf("Illegal input!\n");
				break;
		}
	}

	return 0;
}


你可能感兴趣的:(数据结构,顺序表)