线性表的顺序存储结构

[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 <stdlib.h>
#include <stdio.h>
#include <stdbool.h>

#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;j<L->length;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 && j<L->length)?SeqListGet(L,j+1):-1;
	return r;
}
void SeqListInsert(SeqList *L,int i,int j){
	if(j<MAXSIZE && j>0){
		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;h<L->length;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));
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值