#include<stdio.h>
#include<stdlib.h>
#define LISTSIZE 100
typedef struct
{
DataType list[LISTSIZE];
int length;
}SeqList;
void InitList(SeqList *L)
{
L->length = 0;
}
void ListEmpty(SeqList L)
{
if(L.length = 0)
return 1;
else
return 0;
}
int GetElem(SeqList L,int i,DataType * e)
{
//查找顺序表中第i个元素,查找成功将该值返回给e,并返回1表示成功,否则返回-1,表示失败
//步骤1:首先判断你要查找的元素位置是否合法
if(i<1 || i>L.length)
{
return -1;
}
else
{
*e = L.list[i-1];
return 1;
}
}
int LocateElem(SeqList L,DataType e)
{
//查找顺序表中元素值为e的元素,查找成功将对应的序号返回,否则返回0表示失败
int i;
for(i = 0;i<L.length;i++)
{
if(L.list[i] == e)
{
return i;
}
else
{
return 0;
}
}
}
int InsertList(SeqList *L,int i,DataType e)
{
//在顺序表的第i个位置插入元素e,插入成功返回1,插入位置不合法返回-1,顺序表满返回0
int j;
if(i <1 || i>L->length+1)
{
printf("插入位置i不合法!\n");
return -1;
}
//在插入元素前首先判断顺序表是否已经满
else if(L->length = ListSize)
{
printf("顺序表已经满,不能插入元素。\n");
return 0;
}
else
{
//1:将第i位置一次往后移动
for(j = L->length;j>=i;j--)
{
L->list[j] = L->list[j-1];
}
L->list[i-1] = e;
L->length = L->length +1;
return 1;
}
}
int DeleteList(SeqList *L,int i,DataType *e)
{
int j;
if(L->length <= 0)
{
printf("顺序表已经空,不能进行删除!\n");
return 0;
}else if(i <1 ||i>L->length)
{
printf("删除的位置不对!\n");
return -1;
}else{
*e = L->list[i -1];
for(j = i;j<= L->length -1;j++)
{
L->list[j-1] = L->list[j];
}
L->length = L->length -1;
return 1;
}
}
int ListLength(SeqList L)
{
return L.length;
}
void ClearList(SeqList *L)
{
L->length = 0;
}
#include<stdlib.h>
#define LISTSIZE 100
typedef struct
{
DataType list[LISTSIZE];
int length;
}SeqList;
void InitList(SeqList *L)
{
L->length = 0;
}
void ListEmpty(SeqList L)
{
if(L.length = 0)
return 1;
else
return 0;
}
int GetElem(SeqList L,int i,DataType * e)
{
//查找顺序表中第i个元素,查找成功将该值返回给e,并返回1表示成功,否则返回-1,表示失败
//步骤1:首先判断你要查找的元素位置是否合法
if(i<1 || i>L.length)
{
return -1;
}
else
{
*e = L.list[i-1];
return 1;
}
}
int LocateElem(SeqList L,DataType e)
{
//查找顺序表中元素值为e的元素,查找成功将对应的序号返回,否则返回0表示失败
int i;
for(i = 0;i<L.length;i++)
{
if(L.list[i] == e)
{
return i;
}
else
{
return 0;
}
}
}
int InsertList(SeqList *L,int i,DataType e)
{
//在顺序表的第i个位置插入元素e,插入成功返回1,插入位置不合法返回-1,顺序表满返回0
int j;
if(i <1 || i>L->length+1)
{
printf("插入位置i不合法!\n");
return -1;
}
//在插入元素前首先判断顺序表是否已经满
else if(L->length = ListSize)
{
printf("顺序表已经满,不能插入元素。\n");
return 0;
}
else
{
//1:将第i位置一次往后移动
for(j = L->length;j>=i;j--)
{
L->list[j] = L->list[j-1];
}
L->list[i-1] = e;
L->length = L->length +1;
return 1;
}
}
int DeleteList(SeqList *L,int i,DataType *e)
{
int j;
if(L->length <= 0)
{
printf("顺序表已经空,不能进行删除!\n");
return 0;
}else if(i <1 ||i>L->length)
{
printf("删除的位置不对!\n");
return -1;
}else{
*e = L->list[i -1];
for(j = i;j<= L->length -1;j++)
{
L->list[j-1] = L->list[j];
}
L->length = L->length -1;
return 1;
}
}
int ListLength(SeqList L)
{
return L.length;
}
void ClearList(SeqList *L)
{
L->length = 0;
}