/*
* 实现顺序表各种基本运算的方法
*/
#include <stdio.h>
#include <malloc.h>
#include <stdbool.h>
#define MAX_SIZE 50
typedef char ElemType;
typedef struct {
int length; //存放顺序表的长度
ElemType data[MAX_SIZE]; //存放顺序表元素
}SqList; //声明顺序表的类型
/*--------------------整体建立顺序表------------------------*/
void create_list(SqList *&L, ElemType a[], int n)//指针的引用
{
L = (SqList *)malloc(sizeof(SqList));
for(int i = 0; i < n; i++)
{
L->data[i] = a[i];
}
L->length = n;
}
/*--------------------初始化线性表------------------------*/
void init_list(SqList *&L)//指针的引用
{
L = (SqList *)malloc(sizeof(SqList)); // 分配存放线性表的空间
L->length = 0;
}
/*--------------------销毁线性表------------------------*/
void destroy_list(SqList *&L)//指针的引用
{
free(L);
}
/*--------------------判断线性表是否为空表------------------------*/
bool list_empty(SqList *L)
{
return (L->length == 0);
}
/*--------------------求线性表的长度------------------------*/
int list_length(SqList *L)
{
return (L->length);
}
/*--------------------输出线性表------------------------*/
void display_list(SqList *L)
{
for(int i = 0; i < L->length; i++)
{
printf("%c ", L->data[i]);
}
printf("\n");
}
/*--------------------求线性表中第i个元素值------------------------*/
bool get_elem(SqList *L, int i, ElemType &e)//引用类型
{
if(i < 1 || i > L->length)
return false;
e = L->data[i - 1];
return true;
}
/*--------------------查找第一个值域为e的元素序号------------------------*/
int locate_elem(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;
}
/*--------------------插入第i个元素------------------------*/
bool list_insert(SqList *&L, int i, ElemType e)//指针的引用
{
int j;
if(i < 1 || i > L->length + 1)
return false;
i--;//将顺序表位序转化为data下标
for(j = L->length; j > i; j--)
L->data[j] = L->data[j - 1];// 将data[i]及后面元素后移一个位置
L->data[i] = e;
L->length++; // 顺序表长度增1
return true;
}
/*--------------------删除第i个元素------------------------*/
bool list_delete(SqList *&L, int i, ElemType &e)
{
int j;
if(i < 1 || i > L->length)
return false;
i--; //将顺序表位序转化为data下标
e = L->data[i];
for(j = i; j < L->length - 1; j++)
L->data[j] = L->data[j + 1];//将data[i]之后的元素前移一个位置
L->length--;//顺序表长度减1
return true;
}
int main(void)
{
SqList *L;
ElemType e;
printf("顺序表的基本运算如下:\n");
printf("(1)初始化顺序表L\n");
init_list(L);
printf("(2)依次插入a,b,c,d,e元素\n");
list_insert(L, 1, 'a');
list_insert(L, 2, 'b');
list_insert(L, 3, 'c');
list_insert(L, 4, 'd');
list_insert(L, 5, 'e');
printf("(3)输出顺序表L:");
display_list(L);
printf("(4)顺序表L长度: %d\n", list_length(L));
printf("(5)顺序表L为%s\n", (list_empty(L) ? "空" : "非空"));
get_elem(L, 3, e);
printf("(6)顺序表L的第3个元素:%c\n", e);
printf("(7)元素a的位置:%d\n", locate_elem(L, 'a'));
printf("(8)在第4个元素位置上插入f元素\n");
list_insert(L, 4, 'f');
printf("(9)输出顺序表L:");
display_list(L);
printf("(10)删除L的第3个元素\n");
list_delete(L, 3, e);
printf("(11)输出顺序表L:");
display_list(L);
printf("(12)释放顺序表L\n");
destroy_list(L);
return 0;
}
运行结果:
顺序表的基本运算如下:
(1)初始化顺序表L
(2)依次插入a,b,c,d,e元素
(3)输出顺序表L:a b c d e
(4)顺序表L长度: 5
(5)顺序表L为非空
(6)顺序表L的第3个元素:c
(7)元素a的位置:1
(8)在第4个元素位置上插入f元素
(9)输出顺序表L:a b c f d e
(10)删除L的第3个元素
(11)输出顺序表L:a b f d e
(12)释放顺序表L