/*顺序表头文件SeqList.h*/ #include "stdio.h" #include "math.h" //#define LISTSIZE 100 //typedef int DataType; /*typedef struct { DataType list[LISTSIZE]; int length; }SeqList; */ //这里定义主函数就不用定义了 /****************************************************** C 语言中 结构体变量用 . 运算符来访问结构体的成员 指向结构体的指针用 -> 来访问其指向的结构体的成员 *******************************************************/ // 1 初始化顺序表,将顺序表的长度置为0 void InitList(SeqList *L) { L->length = 0; } // 2 判断顺序表是否为空,为空返回1,不空返回0 int ListEmpty(SeqList L) { if (L.length == 0) return 1; else return 0; } // 3 按序号查找操作 int GetElem(SeqList L, int i, DataType *e) { /*查找顺序表中第 i 个元素 查找成功将该值返回给 e ,并返回 1 ;否则返回 -1 ,表示失败*/ if (i<1 || i>L.length) //查找之前判断序号是否合法 return -1; *e = L.list[i - 1]; //将第 i 个元素赋值给 e return 1; } // 4 按内容查找操作 int LocateElem(SeqList L, DataType e) { /*查找顺序表中元素值为 e 的元素 查找成功,将对应元素的序号返回;否则返回0,表示失败*/ int i; for (i = 1; i <= L.length; i++) { if (L.list[i-1] == e) return i; //返回的序号要搞清楚,从0开始 } return 0; } // 5 插入操作 /* 插入 i 长度j 序号 1 2 3 4 5 6 7 8 9 10 list[i-1] list[j-1] list[j] list 0 1 2 3 4 5 6 7 8 9 */ int InsertList(SeqList *L, int i, DataType e) { /*在顺序表的第 i 个位置插入 元素 e ,插入成功返回 1; 插入位置不和法,返回 -1; 顺序表满返回 0 */ int j; if (i<1 || i > L->length + 1) { printf("插入位置不合法 !"); return -1; } else if (L->length >= LISTSIZE) { printf("顺序表已满不能插入 !"); return 0; } else { for (j = L->length; j >= i; j--) L->list[j] = L->list[j - 1]; L->list[i - 1] = e; //插入元素到底 i 个位置 L->length = L->length + 1; //顺序表长度 +1 return 1; } } // 6 删除操作 /* 删除 i 长度j 序号 1 2 3 4 5 6 7 8 9 10 list[i-1] list[j-1] list 0 1 2 3 4 5 6 7 8 9 */ int DeleteList(SeqList *L, int i, DataType *e) { int j; if (i<1 || i>L->length) { printf("删除位置不合法!"); return -1; } else if (L->length <= 0) { printf("顺序表已空,不能进行删除操作!"); return 0; } else { *e = L->list[i - 1]; //删除的值保存到 e 中,以防要用 for (j = i; j <= L->length - 1; j++) L->list[j - 1] = L->list[j]; L->length = L->length - 1; return 1; } } // 7 获取长度 int ListLength(SeqList L) { return L.length; } //清空操作 void ClearList(SeqList *L) { L->length = 0; }
#include<stdio.h>#define LISTSIZE 100typedef int DataType;typedef struct{DataType list[LISTSIZE];int length;}SeqList;#include "SeqList.h"void DelElem(SeqList *A, SeqList B);void main(){int i, j, flag;DataType e;SeqList A, B;InitList(&A);InitList(&B);for (i = 1; i <= 10; i++){if (InsertList(&A, i, i) == 0){printf("位置不合法");return;}}for (i = 1, j = 1; j <= 6; i = i + 2, j++){if (InsertList(&B, j, i * 2) == 0){printf("位置不合法");return;}}printf("顺序表A中的元素:\n");for (i = 1; i <= A.length; i++){flag = GetElem(A, i, &e);if (flag == 1)printf("%4d", e);}printf("\n");printf("顺序表B中的元素:\n");for (i = 1; i <= B.length; i++){flag = GetElem(B, i, &e);if (flag == 1)printf("%4d", e);}printf("\n");printf("将在A中出现B的元素删除后A中的元素:\n");DelElem(&A, B);for (i = 1; i <= A.length; i++){flag = GetElem(A, i, &e);if (flag == 1)printf("%4d", e);}printf("\n");}void DelElem(SeqList *A, SeqList B){int i, flag, pos;DataType e;for (i = 1; i <= B.length; i++){flag = GetElem(B, i, &e);if (flag == 1){pos = LocateElem(*A, e);if (pos > 0)DeleteList(A, pos, &e);}}}
顺序表实例1
最新推荐文章于 2022-12-19 15:25:21 发布