//库函数头文件包含
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
//函数状态码定义
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
//顺序表的存储结构定义
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef int ElemType; //假设线性表中的元素均为整型
typedef struct{
ElemType* elem; //存储空间基地址
int length; //表中元素的个数
int listsize; //表容量大小
}SqList; //顺序表类型定义
Status InitList(SqList &L); //构造一个线性表 1
Status DestroyList(SqList&L); //销毁 2
Status ClearList(SqList&L); //重置 3
Status ListEmpty(SqList L); //判断是否为空 4
Status ListLength(SqList L); //判断表长 5
Status GetElem(SqList L,int i,ElemType&e);//用e返回第i个元素值 6
//Status LocatElem(SqList L,ElemType ,compare()); //返回L中第一个满足关系compare()的为序
// 7
Status PriorElem(SqList L,ElemType cur_e,ElemType&pre_e); //若cur_e是L的元素,切不是第一个,则用pre_e返回他的前驱,否则操作失败
// 8
Status NextElem(SqList L,ElemType cur_e,ElemType&next_e);//若cur_e是L的元素,切不是最后一个,则用pre_e返回他的后继前驱,否则操作失败
// 9
Status ListInsert(SqList &L, int pos, ElemType e);//增加 10
Status ListDelete(SqList &L, int pos, ElemType &e);//删除 11
int ListLocate(SqList L, ElemType e);//定位 12
void ListPrint(SqList L);//输出 13
//构造一个线性表 1
Status InitList(SqList &L)
{ L.elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));//申请储存空间
if(!L.elem) exit(OVERFLOW);
L.length=0;
L.listsize=LIST_INIT_SIZE;
return OK;
}
//销毁 2
Status DestroyList(SqList&L)
{free(L.elem);
L.elem=NULL;
L.length=0;
L.listsize=0;
return OK;
}
//重置 3
Status ClearList(SqList&L)
{
L.length=0;
return OK;
}
//判断是否为空 4
Status ListEmpty(SqList L)
{
return (L.length==0)?TRUE:FALSE;
}
//判断表长 5
Status ListLength(SqList L)
{
return L.length;
}
//用e返回第i个元素值 6
Status GetElem(SqList L,int i,ElemType &e)
{ElemType *q;
if(i<1||i>L.length)
exit(ERROR);
q=L.elem+i-1;
e=*(q);
return OK;
}
//返回L中第一个满足关系compare()的为序 7
//Status LocatElem(SqList L,ElemType e,compare())
//{
//}
// 8
Status PriorElem(SqList L,ElemType cur_e,ElemType&pre_e)
{
int a;ElemType d;
a=ListLocate(L,cur_e);
if(!a||a==1)
{
printf("查找失败");
return ERROR;
}
pre_e=*(L.elem+a-2);
return OK;
}
//若cur_e是L的元素,切不是第一个,则用pre_e返回他的前驱,否则操作失败 9
Status NextElem(SqList L,ElemType cur_e,ElemType&next_e)
{
int a;
a=ListLocate(L,cur_e);
if(!a||a==1)
{
printf("查找失败");
return ERROR;
}
next_e=*(L.elem+a);
return OK;
}
//增加 10
Status ListInsert(SqList &L,int pos,ElemType e)
{//i的合法值为1<=i<=ListLength_Sq(L)+1;
if(pos<1||pos>=L.listsize)
{ElemType *news;
news=(ElemType *)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType));
if(!news) exit(OVERFLOW);
L.elem=news;
L.listsize+=LISTINCREMENT;
}
ElemType *p=&(L.elem[L.length-1]),*q=&(L.elem[pos-1]); //q为插入位置 p为末位置
for(p;p>=q;--p)
*(p+1)=*p;
*q=e;
++L.length;//表长加一
return OK ;
}
//删除 11
Status ListDelete( SqList&L,int pos,int &e)
{ if(pos<1||pos>L.length)
return ERROR ;
ElemType *p=&(L.elem[pos-1]),*q=&(L.elem[L.length-1]);
e=L.elem[pos-1];
for(p;p<q;p++)
*p=*(p+1);
--L.length;
return e;
}
//查找 12
int ListLocate(SqList L,ElemType e)
{ElemType *p=&(L.elem[0]),*q=&(L.elem[L.length-1]); //p 是当前位置,q是末位置
int n=1;
for(p;p<=q;p++,n++)
{if(*p==e)
return n;
}
return ERROR;
}
//输出 13
void ListPrint(SqList L)
{ElemType *p=&(L.elem[0]),*q=&(L.elem[L.length-1]); //p 是当前位置,q是末位置
for(p;p<=q;p++)
printf("%d ",*p);
}
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
//函数状态码定义
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
//顺序表的存储结构定义
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef int ElemType; //假设线性表中的元素均为整型
typedef struct{
ElemType* elem; //存储空间基地址
int length; //表中元素的个数
int listsize; //表容量大小
}SqList; //顺序表类型定义
Status InitList(SqList &L); //构造一个线性表 1
Status DestroyList(SqList&L); //销毁 2
Status ClearList(SqList&L); //重置 3
Status ListEmpty(SqList L); //判断是否为空 4
Status ListLength(SqList L); //判断表长 5
Status GetElem(SqList L,int i,ElemType&e);//用e返回第i个元素值 6
//Status LocatElem(SqList L,ElemType ,compare()); //返回L中第一个满足关系compare()的为序
// 7
Status PriorElem(SqList L,ElemType cur_e,ElemType&pre_e); //若cur_e是L的元素,切不是第一个,则用pre_e返回他的前驱,否则操作失败
// 8
Status NextElem(SqList L,ElemType cur_e,ElemType&next_e);//若cur_e是L的元素,切不是最后一个,则用pre_e返回他的后继前驱,否则操作失败
// 9
Status ListInsert(SqList &L, int pos, ElemType e);//增加 10
Status ListDelete(SqList &L, int pos, ElemType &e);//删除 11
int ListLocate(SqList L, ElemType e);//定位 12
void ListPrint(SqList L);//输出 13
//构造一个线性表 1
Status InitList(SqList &L)
{ L.elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));//申请储存空间
if(!L.elem) exit(OVERFLOW);
L.length=0;
L.listsize=LIST_INIT_SIZE;
return OK;
}
//销毁 2
Status DestroyList(SqList&L)
{free(L.elem);
L.elem=NULL;
L.length=0;
L.listsize=0;
return OK;
}
//重置 3
Status ClearList(SqList&L)
{
L.length=0;
return OK;
}
//判断是否为空 4
Status ListEmpty(SqList L)
{
return (L.length==0)?TRUE:FALSE;
}
//判断表长 5
Status ListLength(SqList L)
{
return L.length;
}
//用e返回第i个元素值 6
Status GetElem(SqList L,int i,ElemType &e)
{ElemType *q;
if(i<1||i>L.length)
exit(ERROR);
q=L.elem+i-1;
e=*(q);
return OK;
}
//返回L中第一个满足关系compare()的为序 7
//Status LocatElem(SqList L,ElemType e,compare())
//{
//}
// 8
Status PriorElem(SqList L,ElemType cur_e,ElemType&pre_e)
{
int a;ElemType d;
a=ListLocate(L,cur_e);
if(!a||a==1)
{
printf("查找失败");
return ERROR;
}
pre_e=*(L.elem+a-2);
return OK;
}
//若cur_e是L的元素,切不是第一个,则用pre_e返回他的前驱,否则操作失败 9
Status NextElem(SqList L,ElemType cur_e,ElemType&next_e)
{
int a;
a=ListLocate(L,cur_e);
if(!a||a==1)
{
printf("查找失败");
return ERROR;
}
next_e=*(L.elem+a);
return OK;
}
//增加 10
Status ListInsert(SqList &L,int pos,ElemType e)
{//i的合法值为1<=i<=ListLength_Sq(L)+1;
if(pos<1||pos>=L.listsize)
{ElemType *news;
news=(ElemType *)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType));
if(!news) exit(OVERFLOW);
L.elem=news;
L.listsize+=LISTINCREMENT;
}
ElemType *p=&(L.elem[L.length-1]),*q=&(L.elem[pos-1]); //q为插入位置 p为末位置
for(p;p>=q;--p)
*(p+1)=*p;
*q=e;
++L.length;//表长加一
return OK ;
}
//删除 11
Status ListDelete( SqList&L,int pos,int &e)
{ if(pos<1||pos>L.length)
return ERROR ;
ElemType *p=&(L.elem[pos-1]),*q=&(L.elem[L.length-1]);
e=L.elem[pos-1];
for(p;p<q;p++)
*p=*(p+1);
--L.length;
return e;
}
//查找 12
int ListLocate(SqList L,ElemType e)
{ElemType *p=&(L.elem[0]),*q=&(L.elem[L.length-1]); //p 是当前位置,q是末位置
int n=1;
for(p;p<=q;p++,n++)
{if(*p==e)
return n;
}
return ERROR;
}
//输出 13
void ListPrint(SqList L)
{ElemType *p=&(L.elem[0]),*q=&(L.elem[L.length-1]); //p 是当前位置,q是末位置
for(p;p<=q;p++)
printf("%d ",*p);
}