#include <stdio.h>
#include <stdlib.h>
/*--------------------------------
* 宏定义与类型声明
*--------------------------------*/
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define MAXSIZE 100 /* 顺序表最大容量 */
typedef int Status; /* 状态码类型 */
typedef int ElemType; /* 数据元素类型 */
/*--------------------------------
* 顺序表类型定义
*--------------------------------*/
typedef struct {
ElemType *elem; /* 存储空间基址 */
int length; /* 当前长度 */
} SqList;
/*--------------------------------
* 基本操作函数原型
*--------------------------------*/
Status InitList(SqList *L);
void DestroyList(SqList *L);
void ClearList(SqList *L);
int GetLength(SqList L);
int IsEmpty(SqList L);
Status GetElem(SqList L, int position, ElemType *e);
int LocateElem(SqList L, ElemType e);
Status InsertList(SqList *L, int position, ElemType e);
Status DeleteList(SqList *L, int position);
void TraverseList(SqList L);
/*--------------------------------
* 基本操作函数实现
*--------------------------------*/
/* 初始化顺序表 */
Status InitList(SqList *L) {
L->elem = (ElemType *)malloc(MAXSIZE * sizeof(ElemType));
if (L->elem == NULL) {
return OVERFLOW;
}
L->length = 0;
return OK;
}
/* 销毁顺序表 */
void DestroyList(SqList *L) {
if (L->elem != NULL) {
free(L->elem);
L->elem = NULL;
L->length = 0;
}
}
/* 清空顺序表 */
void ClearList(SqList *L) {
L->length = 0;
}
/* 获取顺序表长度 */
int GetLength(SqList L) {
return L.length;
}
/* 判断顺序表是否为空 */
int IsEmpty(SqList L) {
return (L.length == 0) ? TRUE : FALSE;
}
/* 获取指定位置元素 */
Status GetElem(SqList L, int position, ElemType *e) {
if (position < 1 || position > L.length) {
return ERROR;
}
*e = L.elem[position - 1];
return OK;
}
/* 查找元素位置(返回下标+1) */
int LocateElem(SqList L, ElemType e) {
int i;
for (i = 0; i < L.length; i++) {
if (L.elem[i] == e) {
return i + 1;
}
}
return 0; /* 未找到 */
}
/* 插入元素到指定位置 */
Status InsertList(SqList *L, int position, ElemType e) {
int j;
if (position < 1 || position > L->length + 1) {
return ERROR;
}
if (L->length >= MAXSIZE) {
return OVERFLOW;
}
for (j = L->length - 1; j >= position - 1; j--) {
L->elem[j + 1] = L->elem[j];
}
L->elem[position - 1] = e;
L->length++;
return OK;
}
/* 删除指定位置的元素 */
Status DeleteList(SqList *L, int position) {
int j;
if (position < 1 || position > L->length) {
return ERROR;
}
for (j = position; j < L->length; j++) {
L->elem[j - 1] = L->elem[j];
}
L->length--;
return OK;
}
/* 遍历顺序表 */
void TraverseList(SqList L) {
int i;
for (i = 0; i < L.length; i++) {
printf("%d ", L.elem[i]);
}
printf("\n");
}
/*--------------------------------
* 主函数:测试顺序表操作
*--------------------------------*/
int main(void) {
SqList L;
ElemType e;
printf("初始化状态: %d\n", InitList(&L));
printf("是否为空: %d\n", IsEmpty(L));
/* 插入数据 */
InsertList(&L, 1, 10);
InsertList(&L, 2, 20);
InsertList(&L, 3, 30);
printf("当前顺序表: ");
TraverseList(L);
printf("元素20的位置: %d\n", LocateElem(L, 20));
printf("删除第1个元素,状态: %d\n", DeleteList(&L, 1));
InsertList(&L, 1, 40);
GetElem(L, 2, &e);
printf("第2个位置的元素: %d\n", e);
printf("当前长度: %d\n", GetLength(L));
printf("当前顺序表: ");
TraverseList(L);
DestroyList(&L);
return 0;
}
}
2.1顺序表
于 2025-09-29 13:08:30 首次发布
2148

被折叠的 条评论
为什么被折叠?



