设计练习 线性表(顺序表)的抽象数据类型定义构造

数据结构(C语言版) 严蔚敏 吴伟民

设计练习 线性表的定义构造

代码块:

#include <stdio.h>
#include <stdlib.h>

//Function result status code
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASALBE -1
#define OVERFLOW -2

//Status is the type of function whose value is the function result status code
typedef int Status;

typedef struct{
	int *data;
	int length;
}List;

void output(List *L);
Status InitList(List *L);
Status DestroyList(List *L);
Status ClearList(List *L);
Status ListEmpty(List *L);
Status ListLength(List *L);
Status GetElem(List *L, int i, int *e);
Status compare(List *a, int b);
Status LocateElem(List *L, int e, Status (*pfun)(List *ce, int num));
Status PriorElem(List *L, int cur_e, int *pre_e);
Status NextElem(List *L, int cur_e, int *next_e);
Status ListInsert(List *L, int i, int e);
Status ListDelete(List *L, int i, int *e);
Status visit(List *L);
Status ListTraverse(List *L, Status (*pfun)(List *ce));

void output(List *L){
	int i;
	printf("Result: ");
	for(i=0; i<ListLength(L); i++)
		printf("%d ", L->data[i]);
	printf("\n");
}
Status InitList(List *L){
	L->data=(int*)malloc(30*sizeof(int));
	if(!L) exit(OVERFLOW);
	L->length=0;
	return OK;
}//InitList
Status DestroyList(List *L){
	free(L); 
	return OK;
}//DestroyList
Status ClearList(List *L){
	int i;
	for(i=0; i<=ListLength(L); i++){
		if(L->data[i]!=0)
			L->data[i]=0;
		L->length--;
	}
	L->length--;
	return OK;
}//ClearList
Status ListEmpty(List *L){
	if(L->length!=0)
		return FALSE;
	else
		return TRUE;
}//ListEmpty
Status ListLength(List *L){
	return L->length;
}//ListLength
Status GetElem(List *L, int i, int *e){
	int j;
	for(j=0; j<ListLength(L); j++)
		if(j==i-1){
			e=&L->data[j];
			break;
		}
	return *e;
}//GetElem
Status compare(List *a, int b){
	int i, judge;
	for(i=0, judge=0; i<ListLength(a); i++){
		if(a->data[i]==b){
			judge=1;
			return TRUE;
			break;
		}
	}
	if(judge==0)
		return FALSE;
}//compare
Status LocateElem(List *L, int e, Status (*pfun)(List *ce, int num)){
	int i;
	for(i=0; i<ListLength(L); i++){
		if(L->data[i]==e){
			return i+1;
			break;
		}
	}
	if(!pfun(L, e))
		return 0;
}//LocateElem
Status PriorElem(List *L, int cur_e, int *pre_e){
	int i, judge;
	for(i=0, judge=0; i<ListLength(L); i++){
		if(i!=0&&cur_e==L->data[i]){
			judge=1;
			pre_e=&L->data[i-1];
			break;
		}
	}
	if(i==0||judge==0)
		return ERROR;
	else
		return *pre_e;
}//PriorElem
Status NextElem(List *L, int cur_e, int *next_e){
	int i, judge;
	for(i=0, judge=0; i<ListLength(L); i++)
		if(i!=ListLength(L)-1&&cur_e==L->data[i]){
			judge=1;
			next_e=&L->data[i+1];
			break;
		}
	if(i==ListLength(L)-1||judge==0)
		return ERROR;
	else
		return *next_e;
}//NextElem
Status ListInsert(List *L, int i, int e){
	int j;
	if(i<1||(i-1)>ListLength(L)) return ERROR;
	else{
		for(j=ListLength(L); j>=i; j--)
			L->data[j]=L->data[j-1];
		L->length++;
		L->data[j]=e;
		return OK;
	}
}
Status ListDelete(List *L, int i, int *e){
	int j, temp;
	if(i<0||i>ListLength(L)) return ERROR;
	temp=L->data[i];
	for(j=i; j<ListLength(L)-1; j++)
		L->data[j]=L->data[j+1];
	L->length--;
	e=&temp;
	return *e;
}//ListDelete
Status visit(List *L){
	int i, judge;
	for(i=0, judge=0; i<ListLength(L); i++){
		if(L->data[i]!=NULL)
			judge=1;
		else{
			judge=0;
			break;
		}
	}
	if(judge==0)
		return ERROR;
	else
		return OK;
}//visit
Status ListTraverse(List *L, Status (*pfun)(List *ce)){
	if(pfun(L))
		return TRUE;
	else
		return FALSE;
}//ListTraverse

int main()
{
	List *Li=(List*)malloc(sizeof(List));
	printf("InitList Li Status: %d\n", InitList(Li));
	int i, n, *r=NULL;
	printf("Enter List Li Length: ");
	scanf("%d", &n);
	printf("Enter %d numbers: ", n);
	for(i=0; i<n; i++){
		scanf("%d", &Li->data[i]);
		Li->length++;
	}

	printf("ListLength: %d\n", ListLength(Li));
	printf("ListEmpty Status: %d\n", ListEmpty(Li));
	printf("GetElem: %d\n", GetElem(Li, 3, r));
	printf("LocateElem: %d\n", LocateElem(Li, 13, compare));
	printf("PriorElem %d\n", PriorElem(Li, 16, r));
	printf("NextElem: %d\n", NextElem(Li, 2, r));
	printf("ListInsert Status: %d\n", ListInsert(Li, 1, 15));
	output(Li);
	printf("ListDelete Status: %d\n", ListDelete(Li, 2, r));
	output(Li);
	printf("ListTraverse Status: %d\n", ListTraverse(Li, visit));
	printf("ClearList Status: %d\n", ClearList(Li));
	printf("ListEmpty Status: %d\n", ListEmpty(Li));
	printf("DestroyList Status: %d\n", DestroyList(Li));
	system("pause");
	return 0;
}

相关推荐
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页