# include <stdio.h>
# include <malloc.h>
# include <stdlib.h>
# define N 100
typedef int ElenType;
typedef struct
{
ElenType date[N];
int length;
} SqList;
SqList * CreteList(SqList * q,ElenType a[], int len);//创建表
SqList * initList(SqList *);//初始化
void destroyList();//销毁线性表
int ListEmpty(SqList *q);//判断线性表是否为空
int ListLength(SqList *q);//返回线性表的长度
void dispList(SqList *q);//便利输出
void getList(SqList *q,int i,ElenType &e);//获取第i个元素的值,获取值保存在e中
int locateElem(SqList *q,ElenType e); //在线性表中查找与e相等的元素,返回序数,不存在返回0;
SqList * listInsert(SqList * q,int i, ElenType e);//插入第i个元素的前面
SqList * listDelete(SqList * q,int i, ElenType e); //删除第i个元素
int main()
{
SqList * p;
ElenType a[6]={1,34,56,35,55,77};
int s,t,t1;
p=CreteList(p,a,6);
//p=initList(p);
//destroyList(&p);//不对
//ListEmpty(p);
//ListLength(p);
// dispList(p);//获取此值
//getList(p,3,t);//用t来
//printf("%d \n",t);
// s=locateElem(p,55);
//printf("%d",s);
//listInsert(p,3,36);
//dispList(p);//获取此值
listDelete(p,3,t1);
dispList(p);//获取此值
return 0;
}
SqList * CreteList(SqList *q, ElenType a[],int len)
{
q = (SqList *)malloc(sizeof(SqList));
for(int i=0;i<len;i++)
q->date[i]=a[i];
q->length=len;
return q;
}
/*
void CreateList(SqList *&L,ElemType a[],int n)相当于void CreateList((SqList *)(&L),ElemType a[],int n) 相当于&L=p,即L引用p前面的SqList只是类型
//建立顺序表
{
int i;
L=(SqList *)malloc(sizeof(SqList));
for (i=0;i<n;i++)
L->data[i]=a[i];
L->length=n;
}
*/
SqList * initList(SqList *q)
{
q=(SqList *)malloc(sizeof(SqList));
q->length=0;
return q;
}
/*
void InitList(SqList *&L)//初始化
{
L=(SqList *)malloc(sizeof(SqList)); //分配存放线性表的空间
L->length=0;
}
*/
/*void destroyList(SqList **q)
{
ferr(*q);
}
*/
int ListEmpty(SqList * q)
{
return (q->length==0);
}
int ListLength(SqList * q)
{
return (q->length);
}
void dispList(SqList * q)
{
for(int i=0;i<q->length;i++)
printf("%d ",q->date[i]);
}
void getList(SqList * q,int i,ElenType &e)
{
if(i<1||i>q->length)
{
printf("输入错误,没有此元素\n");
exit(-1);
}
e=q->date[i-1];
}
/*
int GetElem(SqList *L,int i, &e)
{
if (i<1 || i>L->length)
return 0;
e=L->data[i-1];
return 1;
}
*/
int locateElem(SqList * q,ElenType e)
{
for(int i=0;i<q->length;i++)
if(q->date[i]==e)
return i+1;
return 0;
}
/*
int LocateElem(SqList *L, ElemType e)
{
int i=0;
while (i<L->length && L->data[i]!=e) i++;
if (i>=L->length)
return 0;
else
return i+1;
}
*/
SqList * listInsert(SqList *q,int i,ElenType e)
{
if(i<1||i>q->length)
{
printf("元素的位置错误!");
exit(-1);
}
else
for(int j=q->length;j>=i;j--)
q->date[j]=q->date[j-1];
q->date[i-1]=e;
q->length++;
return q;
}
/*
int ListInsert(SqList *&L,int i,ElemType e)
{
int j;
if (i<1 || i>L->length+1)
return 0;
i--; //将顺序表位序转化为elem下标
for (j=L->length;j>i;j--) //将data[i]及后面元素后移一个位置
L->data[j]=L->data[j-1];
L->data[i]=e;
L->length++; //顺序表长度增1
return 1;
*/
SqList * listDelete(SqList * q,int i,ElenType e)
{
if(i<1||i>q->date[q->length-1])
{
printf("输入错误!\n");
exit(-1);
}
else
{
e = q->date[i-1];
for(int j=i;j<q->length;j++)
q->date[j-1]=q->date[j];
q->length--;
}
return q;
}
/*
int ListDelete(SqList *&L,int i,ElemType &e)
{
int j;
if (i<1 || i>L->length)
return 0;
i--; //将顺序表位序转化为elem下标
e=L->data[i];
for (j=i;j<L->length-1;j++) //将data[i]之后的元素前移一个位置
L->data[j]=L->data[j+1];
L->length--; //顺序表长度减1
return 1;
}
*/