线性表的顺序存储结构

[web@localhost d2]$ gcc --version
gcc (GCC) 4.4.4 20100726 (Red Hat 4.4.4-13)

Copyright (C) 2010 Free Software Foundation, Inc.


#include 
#include 
#include 

#define MAXSIZE 50


struct node{
	int data[MAXSIZE];
	int length;
};

typedef struct node SeqList;

void SeqListInit(SeqList *L){
	L->length=0;
}
int SeqListLength(SeqList *L){
	return L->length;
}
int SeqListGet(SeqList *L,int i){
	if(i>=1 && i<=L->length){
		return L->data[i-1];
	}else{
		printf("i is not valiue");
		exit(0);
	}
}
int SeqListLocate(SeqList *L,int i){
	if(L->length==0) return -1;
	int j;
	for(j=0;jlength;j++){
		if(L->data[j]==i) return j+1;     		
	}
	return -1;
}
int SeqListPrior(SeqList *L,int i){
	int j;
	j=SeqListLocate(L,i);
	if(j>1){
		return SeqListGet(L,j-1);
	}else{
		return -1;
	}
}
int SeqListNrior(SeqList *L,int i){
	int j,r;
	j=SeqListLocate(L,i);
	r=(j>0 && jlength)?SeqListGet(L,j+1):-1;
	return r;
}
void SeqListInsert(SeqList *L,int i,int j){
	if(j0){
		int h;
		for(h=L->length-1;h>=j-1;h--){
			L->data[h+1]=L->data[h];
		}
		if(L->data[j-1]=i){
			L->length++;
		}
	}
}
void SeqListDel(SeqList *L,int i){
	if(i>0 && L->length>0 && i<=L->length){
		int h;
		for(h=i-1;hlength;h++){
	   		L->data[h]=L->data[h+1];
    		}
		L->length--;	
	}
}
bool SeqListIsEmpty(SeqList *L){
	bool r;
	r=L->length>0?false:true;
	return r;
}
void SeqListEmpty(SeqList *L){
	L->length=0;
}
void SeqListTraverse(SeqList *L){
	int h=1;
	char *l;
	for(h;h<=L->length;h++){
		l=h==L->length?"":"->";
		printf("addr[%X]value[%d]%s",&L->data[h-1],L->data[h-1],l);
	}
}
void main(){
	SeqList *L;
	L=(SeqList *)malloc(sizeof(SeqList));	
	printf("init:");
	SeqListInit(L);
	printf("L\n");
	printf("length:%d\n",L->length);
	printf("add iterm 1\n");
	SeqListInsert(L,1,1);
	printf("length:%d\n",L->length);
	printf("add iterm 2\n");
	SeqListInsert(L,2,2);
	printf("add iterm 3\n");
	SeqListInsert(L,3,3);
	printf("add iterm 4\n");
	SeqListInsert(L,4,4);
	SeqListTraverse(L);
	SeqListInsert(L,5,1);
	printf("\n");
	SeqListTraverse(L);
	printf("\n");
	printf("the last value of %d\n",SeqListPrior(L,1));
	printf("the last value of %d\n",SeqListPrior(L,5));
	printf("the next value of %d\n",SeqListNrior(L,3));
	printf("the next value of %d\n",SeqListNrior(L,4));
	printf("is L empty ?%d\n",SeqListIsEmpty(L));
	SeqListDel(L,1);
	printf("is L empty ?%d\n",SeqListIsEmpty(L));
	SeqListDel(L,1);
	printf("is L empty ?%d\n",SeqListIsEmpty(L));
	SeqListDel(L,1);
	printf("is L empty ?%d\n",SeqListIsEmpty(L));
	SeqListDel(L,1);
	printf("is L empty ?%d\n",SeqListIsEmpty(L));
	SeqListDel(L,1);
	printf("is L empty ?%d\n",SeqListIsEmpty(L));
}



你可能感兴趣的:(c,数据结构预算法(c语言),存储,struct,gcc,2010,web,c)