顺序表
SqList.h
#define OK 1
#define FALSE 0
#define MAXSIZE 30 //存储空间的初始值
typedef int ElemType;
typedef int Status;//返回函数运行后状态
typedef struct
{
ElemType data[MAXSIZE];//存储数据的数组
int length;//顺序表长度
}SqList;
//用一个长度为n的数组初始化顺序表
Status ListInit(SqList * L, ElemType * array, int n);
//返回位置i处的元素,放在e所指向的变量中
Status GetElem(SqList * L, ElemType * e, int i);
//查找e所指向的元素是否在顺序表中存在,若存在返回下标
int GetLocation(SqList * L, ElemType * e);
//指定第i个元素后插入
Status ListBackInsert(SqList * L, int i, ElemType e);
//指定第i个元素前插入
Status ListFrontInsert(SqList * L, int i, ElemType e);
//删除第i个元素
Status ListDelete(SqList * L, int i, ElemType * e);
//打印顺序表
void ListPrint(SqList * L);
SqList.c
#include"SqList.h"
//用一个长度为n的数组初始化顺序表
Status ListInit(SqList * L, ElemType * array, int n)
{
if (n > MAXSIZE)
return FALSE;
for (int i = 0; i < n; i++)
L->data[i] = *(array + i);
L->length = n;
return OK;
}
//返回位置i处的元素,放在e所指向的变量中
Status GetElem(SqList * L, ElemType * e, int i)
{
if (L->length == 0 || i < 1 || i > L->length)
return FALSE;
*e = L->data[i - 1];
return OK;
}
//查找e所指向的元素是否在顺序表中存在,若存在返回下标
int GetLocation(SqList * L, ElemType * e)
{
if (L->length == 0)
return -1;//顺序表为空,查找失败
for (int i = 0; i < L->length; i++)
if (L->data[i] == *e)
return i;
}
//指定第i个元素后插入
Status ListBackInsert(SqList * L, int i, ElemType e)
{
//当表已满或插入位置不恰当时,返回错误
if (i < 1 || i > L->length || L->length == MAXSIZE)
return FALSE;
if (i < L->length)//当插入位置不在表尾
{
//把要插入位置后的所有数据向后移动一位
for (int k = L->length; k > i; k--)
L->data[k] = L->data[k-1];
}
L->data[i] = e;//插入新元素
L->length++;
return OK;
}
//指定第i个元素前插入
Status ListFrontInsert(SqList * L, int i, ElemType e)
{
//当表已满或插入位置不恰当时,返回错误
if (i < 1 || i > L->length || L->length == MAXSIZE)
return FALSE;
if (i <= L->length)//当插入位置不在表尾
{
//把要插入位置后的所有数据向后移动一位
for (int k = L->length - 1; k > i - 2; k--)
L->data[k + 1] = L->data[k];
}
L->data[i - 1] = e;//插入新元素
L->length++;
return OK;
}
//删除第i个元素
Status ListDelete(SqList * L, int i, ElemType * e)
{
//如果表为空或删除位置有误,返回错误
if (L->length == 0 || i < 1 || i > L->length)
return FALSE;
*e = L->data[i - 1];
//如果删除位置不是表尾部
if (i < L->length)
{
//将第i个位置后的所有元素向前挪动一位,覆盖其值
for (int j = i - 1; j < L->length; j++)
L->data[j] = L->data[j + 1];
}
L->length--;
return OK;
}
//打印顺序表
void ListPrint(SqList * L)
{
if (L->length == 0)
printf("表为空!");
for (int i = 0; i < L->length; i++)
printf("%d ",L->data[i]);
printf("\n");
}
main.c
#include<stdio.h>
#include"SqList.h"
int main()
{
ElemType m;
SqList L;
ElemType a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
ListInit(&L, a, 10);
GetElem(&L, &m, 3);
printf("%d\n", m);
printf("元素%d的下标是:%d\n", m, GetLocation(&L, &m));
printf("Status:%d\n", ListBackInsert(&L, 1, 11));
ListPrint(&L);
printf("Status:%d\n", ListBackInsert(&L, 6, 55));
ListPrint(&L);
printf("Status:%d\n", ListBackInsert(&L, 12, 1010));
ListPrint(&L);
printf("Status:%d,Value:\n",m,ListDelete(&L, 2, &m));
printf("Status:%d,Value:\n", m, ListDelete(&L, 6, &m));
printf("Status:%d,Value:\n", m, ListDelete(&L, 11, &m));
ListPrint(&L);
printf("Status:%d\n", ListFrontInsert(&L, 1, 11));
ListPrint(&L);
printf("Status:%d\n", ListFrontInsert(&L, 6, 55));
ListPrint(&L);
printf("Status:%d\n", ListFrontInsert(&L, 12, 1010));
ListPrint(&L);
system("pause");
return 0;
}