线性表_顺序存储/基本操作
#include <stdio.h>
#include <stdlib.h>
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define Elemtype int
#define OK 1
#define OVERFLOW -2
#define status int
#define TRUE 1
#define FALSE 0
#define ERROR 0
typedef struct
{
Elemtype *head;
int length;
int listsize;
}Sqlist;
status initSqlist(Sqlist &L)
{//构造顺序表
L.head=(Elemtype *)malloc(LIST_INIT_SIZE*sizeof(Elemtype));
if(!L.head) exit(OVERFLOW);//分配失败
L.length=0;//表长度置空
L.listsize=LIST_INIT_SIZE;
return OK;
}//initlist
void clearSqlist(Sqlist &L)
{
L.length=0;
}//clearlist
status destroySqlist( Sqlist &L)
{//销毁顺序表
clearSqlist(L);
free(L.head);
}//destroylist
status SqlisTempty(Sqlist L)
{
if(!L.length) return TRUE;
else return FALSE;
}//listempty
int SqlistLength(Sqlist L)
{
return (L.length);
}//listlength
status getSqlistElem(Sqlist L,int i,Elemtype &e)
{
if(i<1||i>L.length) return FALSE;
e=L.head[i-1];
return OK;
}//getSqlist
status locateElem(Sqlist L,int i)
{
}//locateElem
status priorElem(Sqlist L,Elemtype cur_e,Elemtype &pre_e)
{
int i=1;
while(i<=L.length&&L.head[i-1]!=cur_e){
i++;
}
if(i>L.length||i==1) return FALSE;
else {pre_e=L.head[i-2];return TRUE;}
}//priorElem
status nextElem(Sqlist L,Elemtype cur_e,Elemtype &next_e)
{
int i=1;
while(i<=L.length&&L.head[i-1]!=cur_e){i++;}
if(i>=L.length) return FALSE;
else next_e=L.head[i];
}//nextElem
status insertSqlist(Sqlist &L,int i,Elemtype e)
{
if(i<1||i>L.length+1) return FALSE;
if(L.length>=L.listsize){
Elemtype *newhead;
newhead=(Elemtype *)realloc(L.head,(LIST_INIT_SIZE+LISTINCREMENT)*sizeof(Elemtype));
if(!newhead) exit(OVERFLOW);
L.head=newhead;
L.listsize+= LISTINCREMENT;
}
Elemtype *q,*p=&L.head[i-1];
for(q=p;q<=&L.head[L.length-1];q++){
*(q+1)=*q;
}
*p=e;
++L.length;
return OK;
}//insertSqlist
status deleteSqlist(Sqlist &L,int i,Elemtype &e)
{
if(i<1||i>L.length) return ERROR;
int *q,*p=&L.head[i-1];
e=*p;
for(q=++p;q<&L.head[L.length-1];q++){
*(q-1)=*q;
}
--L.length;
return OK;
}//deleteSqlist