/**数据结构—–线性表——顺序表—**************/
#include "stdio.h"
#include "stdlib.h"
#define ERROR 0
#define FALSE 0
#define OK 1
#define TRUE 1
#define MAXSIZE 20
typedef int Elemtype;
typedef int Status;
/创建一个顺序表**/
typedef struct {
Elemtype data[MAXSIZE];
int length;
}Sqlist;
/初始化操作,建立一个新的线性表*/
Status ListInit(Sqlist *sqlist)
{
int i;
//i = sqlist ->length;
for(i=0;ilength;i++)
{
sqlist->data[i]=0;
}
sqlist->length = 0;
return OK ;
}
/若线性表为空,返回TRUE,否则返回FALSE***/
Status ListEmpty(Sqlist sqlist)
{
if(sqlist.length == 0)
return TRUE ;
else
return ERROR;
}
/将线性表的第i个元素返回给e*********/
Status GetElem(Sqlist sqlist,int i,Elemtype *e)
{
if(i<=0||i>MAXSIZE||sqlist.length==0)
{
return ERROR;
}
*e = sqlist.data[i-1];//第i个元素在数组的第i-1个位置
return OK;
}
/在线性表中查找与给定值e相等的元素,如果查找成功,返回该元素在表中的序号表示成功,否则返回0表示失败****/
Status LocateElem(Sqlist sqlist,Elemtype e )
{
int i,length1=0;
for(i=0;ilength;++i)
{
length1+=1;
if(sqlist.data [i] == e)
{
return length1;
}
}
return FALSE;
}
/在线性表sqlist中的第i个位置插入元素e****************/
Status InsertList(Sqlist *sqlist,int i,Elemtype e )
{
int f;
if(i<1||i>sqlist->length+1 )//当i不在范围时
{
return FALSE;
}
if(sqlist->length==MAXSIZE)
{
return FALSE;
}
if(ilength)//i不是最后一个
{
for(f=sqlist->length-1;f>=i-1;--f)
{
sqlist->data[f+1]=sqlist->data[f];
}
}
sqlist->data[i-1] = e;//插入
sqlist ->length ++;
return OK ;
}
/删除线性表sqlist中的第i个元素,并用e返回其值****/
Status DeleteList(Sqlist *sqlist,int i,Elemtype *e)
{
int f;
if(i>sqlist->length||i<1)//i的取值范围
{
return ERROR;
}
if(sqlist->length == 0)
{
return ERROR;
}
*e = sqlist->data[i-1];
if(ilength)
{
for(f=i;flength;++f)
{
sqlist->data[f-1] =sqlist->data[f];
}
}
sqlist->length -= 1;
return OK;
}
int main()
{
Sqlist a;
Status status;
Elemtype find,e1,e2;
int f1;
printf("hello word!!\n\n");
a.length =0;
a.data[2] = 12;
a.length=3;
status = ListEmpty(a);
printf("a.data[2]=%d\na.length=%d\nstatus=%d\n\n",a.data[2],a.length,status);
ListInit(&a);
status = ListEmpty(a);
printf("a.data[2]=%d\na.length=%d\nstatus=%d\n\n",a.data[2],a.length,status);
a.data[2] = 12;
a.length=3;
GetElem(a,3,&e1);
printf("a.data[2]=%d\ne1=%d\n\n",a.data[2],e1);
ListInit(&a);
for(f1=0;f11;++f1)
{
a.data [f1] = f1;
a.length+=1;
}
find = LocateElem(a,8);
if(find!=0)
printf("被查元素在线性表的第%d个位置\na.length = %d\n\n",find,a.length);
InsertList(&a,4,12);
printf("a.length=%d\n\n",a.length);
DeleteList(&a,5,&e2);
printf("%d已被删除\n\n",e2);
system("pause");
return 0;
}