这个顺序表实现数组采用的静态分配方法,一旦空间占满,再加入新的数据是将会产生溢出,进而导致程序崩溃!
文末有完整的代码示例。
顺序表从存储类型描述如下:
#define MaxSize 50
typedef int ElemType;
typedef struct{
ElemType data[MaxSize];
int length;
}SqList;
这里给出了顺序表的一些基本操作方法:
void InitList(SqList *L); //初始化顺序表
bool InsertList(SqList *L, int i, ElemType e);//在顺序表的第i个位置插入元素
bool DeletList(SqList *L, int i, ElemType *e); //删除顺序表达的第i个元素的位置,并用e返回
int LocateElem(SqList L, ElemType e); //按值顺序查找元素,并返回其位序
ElemType GetElem(SqList L, int i, ElemType *e); //查找顺序表中指定位置的元素,并用e返回
对应的实现过程如下代码:
void InitList(SqList *L)
{
L->length = 0;
}
bool InsertList(SqList *L, int i, ElemType e)
{
int j=0;
if(i < 1 || i > L->length + 1)
{
return false;
}
if(L->length >= MaxSize)
{
return false;
}
for(j = L->length; j >= i; j--)
{
L->data[j] = L->data[j-1];
}
L->data[i-1] = e;
L->length++;
return true;
}
bool DeletList(SqList *L, int i, ElemType *e)
{
int j;
if(i < 1 || i > L->length + 1)
{
return false;
}
(*e) = L->data[i-1];
for(j = i; j < L->length; j++)
{
L->data[j-1] = L->data[j];
}
L->length--;
return true;
}
int LocateElem(SqList L, ElemType e)
{
int i;
for(i = 0; i < L.length; i++)
{
if(L.data[i] == e)
{
return i+1;
}
}
return 0;
}
ElemType GetElem(SqList L, int i, ElemType *e)
{
if(i < 0 || i > L.length - 1)
{
return false;
}
(*e) = L.data[i];
return 1;
}
写一个简单的main()函数测试,如下:
int main()
{
int i = 1, s, e = 0;
SqList sqList;
InitList(&sqList);
scanf("%d", &s);
while(s != 9999)
{
InsertList(&sqList, i++, s);
scanf("%d", &s);
}
printf("所有元素:");
for(i = 0; i < sqList.length; i++)
{
printf("%d ", sqList.data[i]);
}
DeletList(&sqList, 2, &e);
printf("\n删除的元素为:%d", e);
printf("\n所有元素:");
for(i = 0; i < sqList.length; i++)
{
printf("%d ", sqList.data[i]);
}
printf("\n元素值为3的位序:%d \n", LocateElem(sqList, 3));
GetElem(sqList, 2, &e);
printf("顺序表中位置为2的元素值:%d \n", e);
return 0;
}
#include "stdio.h"
#define MaxSize 50
typedef int ElemType;
typedef struct{
ElemType data[MaxSize];
int length;
}SqList;
void InitList(SqList *L); //初始化顺序表
bool InsertList(SqList *L, int i, ElemType e);//在顺序表的第i个位置插入元素
bool DeletList(SqList *L, int i, ElemType *e); //删除顺序表达的第i个元素的位置,并用e返回
int LocateElem(SqList L, ElemType e); //按值顺序查找元素,并返回其位序
ElemType GetElem(SqList L, int i, ElemType *e); //查找顺序表中指定位置的元素,并用e返回
int main()
{
int i = 1, s, e = 0;
SqList sqList;
InitList(&sqList);
scanf("%d", &s);
while(s != 9999)
{
InsertList(&sqList, i++, s);
scanf("%d", &s);
}
printf("所有元素:");
for(i = 0; i < sqList.length; i++)
{
printf("%d ", sqList.data[i]);
}
DeletList(&sqList, 2, &e);
printf("\n删除的元素为:%d", e);
printf("\n所有元素:");
for(i = 0; i < sqList.length; i++)
{
printf("%d ", sqList.data[i]);
}
printf("\n元素值为3的位序:%d \n", LocateElem(sqList, 3));
GetElem(sqList, 2, &e);
printf("顺序表中位置为2的元素值:%d \n", e);
return 0;
}
void InitList(SqList *L)
{
L->length = 0;
}
bool InsertList(SqList *L, int i, ElemType e)
{
int j=0;
if(i < 1 || i > L->length + 1)
{
return false;
}
if(L->length >= MaxSize)
{
return false;
}
for(j = L->length; j >= i; j--)
{
L->data[j] = L->data[j-1];
}
L->data[i-1] = e;
L->length++;
return true;
}
bool DeletList(SqList *L, int i, ElemType *e)
{
int j;
if(i < 1 || i > L->length + 1)
{
return false;
}
(*e) = L->data[i-1];
for(j = i; j < L->length; j++)
{
L->data[j-1] = L->data[j];
}
L->length--;
return true;
}
int LocateElem(SqList L, ElemType e)
{
int i;
for(i = 0; i < L.length; i++)
{
if(L.data[i] == e)
{
return i+1;
}
}
return 0;
}
ElemType GetElem(SqList L, int i, ElemType *e)
{
if(i < 0 || i > L.length - 1)
{
return false;
}
(*e) = L.data[i];
return 1;
}