# 线性表的顺序存储结构

[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?"":"->";
}
}
void main(){
SeqList *L;
L=(SeqList *)malloc(sizeof(SeqList));
printf("init:");
SeqListInit(L);
printf("L\n");
printf("length:%d\n",L->length);
SeqListInsert(L,1,1);
printf("length:%d\n",L->length);
SeqListInsert(L,2,2);
SeqListInsert(L,3,3);
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-20 2050

08-11 2576

07-30 671

03-19 1074

05-09 4808

05-06 5005

12-09 2829

07-07 811

07-08 1189

03-18 7753