关闭

线性表的顺序存储结构

标签: 存储structgcc2010webc
385人阅读 评论(0) 收藏 举报
分类:
[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));
}



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:127399次
    • 积分:1739
    • 等级:
    • 排名:千里之外
    • 原创:44篇
    • 转载:42篇
    • 译文:1篇
    • 评论:2条
    最新评论