SeqList.h
#ifndef __SEQLIST_H__
#define __SEQLIST_H__
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXSIZE 20 /* 存储空间初始分配量 */
typedef int Status; /* Status 是函数的类型,其值是函数结果状态代码,如 OK 等 */
typedef int ElemType; /* ElemType 类型根据实际情况而定,这里假设为 int */
Status visit(ElemType c);
typedef struct {
ElemType data[MAXSIZE]; /* 数组,存储数据元素 */
int length; /* 当前顺序表长度 */
}SeqList;
/* 初始化顺序表 */
Status InitList(SeqList *L);
/* 判断顺序表是否为空,是则返回TRUE;否则返回FALSE */
Status ListEmpty(SeqList L);
/* 重置顺序表为空表 */
Status ClearList(SeqList *L);
/* 销毁顺序表 */
Status DestroyList(SeqList *L);
/* 获取顺序表中元素个数 */
Status ListLength(SeqList L);
/* 获取顺序表中第i个位置的元素,用e返回其值。i >= 1 && i <= ListLength(L) */
Status GetElem(SeqList L, int i, ElemType *e);
/* 返回顺序表中第一个与元素e满足关系的数据元素的位序 */
/* 若这样的元素不存在,则返回0 */
int LocateElem(SeqList L, ElemType e);
/* 在顺序表中第i个位置插入新的数据元素e,L的长度加1。i >= 1 && i <= ListLength(L) */
Status ListInsert(SeqList *L, int i, ElemType e);
/* 将顺序表中第i个位置的数据元素删除,并用e返回其值,L的长度减1。i >= 1 && i <= ListLength(L) */
Status ListDelete(SeqList *L, int i, ElemType *e);
/* 操作结果:依次输出 L 的每个数据元素 */
Status ListTraverse(SeqList L);
/* La = La U Lb */
void UnionL(SeqList *La, SeqList Lb);
#endif
SeqList.c
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <ctype.h>
#include <io.h>
#include <math.h>
#include <time.h>
#include "SeqList.h"
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXSIZE 20 /* 存储空间初始分配量 */
typedef int Status; /* Status 是函数的类型,其值是函数结果状态代码,如 OK 等 */
typedef int ElemType; /* ElemType 类型根据实际情况而定,这里假设为 int */
Status visit(ElemType c)
{
printf("%d ", c);
return OK;
}
//typedef struct {
// ElemType data[MAXSIZE]; /* 数组,存储数据元素 */
// int length; /* 当前顺序表长度 */
//}SeqList;
/* 初始化顺序表 */
Status InitList(SeqList *L) {
L->length = 0;
return OK;
}
/* 判断顺序表是否为空,是则返回TRUE;否则返回FALSE */
Status ListEmpty(SeqList L) {
// 初始条件:L存在
if (L.length == 0) {
return TRUE;
}
else {
return FALSE;
}
}
/* 重置顺序表为空表 */
Status ClearList(SeqList *L) {
if (L->length >= 0 && L->length < (MAXSIZE + 1)) {
L->length = 0;
return OK;
}
else {
return ERROR;
}
}
/* 销毁顺序表 */
Status DestroyList(SeqList *L) {
if (L->length >= 0 && L->length < (MAXSIZE + 1)) {
free(L);
return OK;
}
else {
return ERROR;
}
}
/* 获取顺序表中元素个数 */
int ListLength(SeqList L) {
if (L.length >= 0 && L.length < (MAXSIZE + 1)) {
return L.length;
}
else {
return ERROR;
}
}
/* 获取顺序表中第i个位置的元素,用e返回其值。i >= 1 && i <= ListLength(L) */
Status GetElem(SeqList L, int i, ElemType *e) {
if (L.length == 0 || i < 1 || i > L.length) {
return ERROR;
}
*e = L.data[i - 1];
return OK;
}
/* 返回顺序表中第一个与元素e满足关系的数据元素的位序 */
/* 若这样的元素不存在,则返回0 */
int LocateElem(SeqList L, ElemType e) {
int i;
if (L.length == 0) {
return 0;
}
for (i = 0; i < L.length; i++) {
if (L.data[i] == e) {
break;
}
}
if (i >= L.length) {
return 0;
}
return i + 1;
}
/* 在顺序表中第i个位置插入新的数据元素e,L的长度加1。i >= 1 && i <= ListLength(L) */
Status ListInsert(SeqList *L, int i, ElemType e) {
int k;
if (L->length == MAXSIZE) {
return ERROR;
}
if (i < 1 || i > L->length + 1) {
return ERROR;
}
if (i <= L->length) {
for (k = L->length - 1; k >= i - 1; k--) {
L->data[k + 1] = L->data[k];
}
}
L->data[i - 1] = e;
L->length++;
return OK;
}
/* 将顺序表中第i个位置的数据元素删除,并用e返回其值,L的长度减1。i >= 1 && i <= ListLength(L) */
Status ListDelete(SeqList *L, int i, ElemType *e) {
int k;
if (L->length == 0) /* 线性表为空 */
return ERROR;
if (i<1 || i>L->length) /* 删除位置不正确 */
return ERROR;
*e = L->data[i - 1];
if (i <= L->length) {
for (k = i; k <= L->length; k++)/* 将删除位置后继元素前移 */
L->data[k - 1] = L->data[k];
}
L->length--;
return OK;
}
/* 操作结果:依次输出 L 的每个数据元素 */
Status ListTraverse(SeqList L) {
int i;
for (i = 0; i <= L.length; i++) {
visit(L.data[i]);
}
printf("\n");
return OK;
}
/* La = La U Lb */
void UnionL(SeqList *La, SeqList Lb) {
int La_len, Lb_len, i;
ElemType e;
La_len = ListLength(*La);
Lb_len = ListLength(Lb);
for (i = 1; i <= Lb_len; i++) {
GetElem(Lb, i, &e);
if (!LocateElem(*La, e)) {
ListInsert(La, ++La_len, e);
}
}
}
main.c
#include <stdio.h>
#include <stdlib.h>
#include "SeqList.h"
int main() {
SeqList L;
SeqList Lb;
ElemType e;
Status i;
int j, k;
i = InitList(&L);
printf("初始化 L 后:L.length=%d\n", L.length);
printf("\n");
for (j = 1; j <= 8; j++)
i = ListInsert(&L, 1, j);
printf("在 L 的表头依次插入 1~8 后:L.data=");
ListTraverse(L);
printf("L.length=%d \n", L.length);
printf("\n");
i = ListEmpty(L);
printf("L 是否空:i=%d(1:是 0:否)\n", i);
printf("\n");
i = ClearList(&L);
printf("清空 L 后:L.length=%d\n", L.length);
i = ListEmpty(L);
printf("L 是否空:i=%d(1:是 0:否)\n", i);
printf("\n");
for (j = 1; j <= 12; j++)
ListInsert(&L, j, j);
printf("在 L 的表尾依次插入 1~12 后:L.data=");
ListTraverse(L);
printf("L.length=%d \n", L.length);
printf("\n");
ListInsert(&L, 1, 0);
printf("在 L 的表头插入 0 后:L.data=");
ListTraverse(L);
printf("L.length=%d \n", L.length);
printf("\n");
GetElem(L, 6, &e);
printf("第 6 个元素的值为:%d\n", e);
printf("\n");
for (j = 5; j <= 7; j++){
k = LocateElem(L, j);
if (k)
printf("第%d个元素的值为%d\n",k,j);
else
printf("没有值为%d 的元素\n", j);
}
printf("\n");
k = ListLength(L); /* k 为表长 */
for (j = k + 1; j >= k; j--){
i = ListDelete(&L, j, &e); /* 删除第 j 个数据
*/
if (i == ERROR)
printf("删除第%d个数据失败\n",j);
else
printf("删除第%d 个的元素值为:%d\n",j,e);
}
printf("依次输出 L 的元素:");
ListTraverse(L);
printf("\n");
j = 5;
ListDelete(&L, j, &e); /* 删除第 5 个数据 */
printf("删除第%d 个的元素值为:%d\n", j, e);
printf("依次输出 L 的元素:");
ListTraverse(L);
printf("此时 L 的长度 L.length=%d\n", L.length);
printf("\n");
//构造一个有 11 个数的 Lb
i = InitList(&Lb);
for (j = 8; j <= 18; j++)
i = ListInsert(&Lb, 1, j);
printf("依次输出 Lb 的元素:");
ListTraverse(Lb);
printf("此时 Lb 的长度 Lb.length=%d\n", Lb.length);
printf("\n");
UnionL(&L, Lb);
printf("依次输出合并了 Lb 的 L 的元素:");
ListTraverse(L);
printf("合并后 L 的长度 L.length=%d\n", L.length);
printf("\n");
/*DestroyList(&Lb);
DestroyList(&L);*/
system("pause");
return 0;
}