头文件SeqList.h#define SEQLIST_INIT_SIZE 8 #define INC_SIZE 3 #define _CRT_SECURE_NO_WARNINGS #include<malloc.h> #include<assert.h> #include<stdlib.h> #include<stdbool.h> typedef int ElemType; typedef struct SeqList { ElemType *base;//存储空间基址 int capacity; int size;//真实数据大小 }SeqList; void InitSeqList(SeqList *list);//初始化顺序表 void push_back(SeqList *list,ElemType x);//尾插 void push_front(SeqList *list, ElemType x);//头插 void show_list(SeqList *list);//显示结果 void pop_back(SeqList *list);//后删 void pop_front(SeqList*list);//头删 void insert_pos(SeqList*list,ElemType x,ElemType e);//指定位置插入元素 ElemType find(SeqList *list,ElemType x);//查询指定元素 ElemType length(SeqList *list);//查询表长 void delete_pos(SeqList *list, ElemType pos);//删除下标pos的元素 void delete_val(SeqList *list, ElemType val);//删除值为val的元素 void sort(SeqList *list);//冒泡排序 void resver(SeqList *list);//逆序 void clear(SeqList *list);//清除数据 void destory(SeqList *list);//摧毁顺序表 bool inc(SeqList *list);//空间自增函数 void merge(SeqList *list1, SeqList *list2, SeqList *list3);//合并顺序表到list3
源文件SeqList.c(函数实现)
#include"SeqList.h" #define _CRT_SECURE_NO_WARNINGS #include<stdlib.h> #include<stdio.h> void InitSeqList(SeqList *list) { list->base = (ElemType *)malloc(sizeof(ElemType)*SEQLIST_INIT_SIZE); assert(list != NULL); list->capacity = SEQLIST_INIT_SIZE; list->size = 0; } void push_back(SeqList *list, ElemType x) {//尾插 if (list->size >=list->capacity&&!inc(list)) printf("空间已满,无法实现尾插!\n"); else { list->base[list->size] = x; list->size++; } } void push_front(SeqList *list, ElemType x) {//头插 if (list->size >= list->capacity && !inc(list)) printf("空间已满,无法实现尾插!\n"); else { for (int i = list->size; i > 0; i--) { list->base[i] = list->base[i - 1]; } list->base[0] = x; list->size++; } } void show_list(SeqList *list) {//显示结果 int i = 0; for (i = 0; i < list->size; i++) { printf("%d ", list->base[i]); } } void pop_back(SeqList *list) {//后删 if (list->size == 0) printf("表已空,无法删除\n"); else { list->size--; } } void pop_front(SeqList *list) {//头删 if (list->size == 0) printf("表已空,无法删除\n"); else { for (int i = 0; i < list->size; i++) { list->base[i] = list->base[i + 1]; } list->size--; } } void insert_pos(SeqList*list, ElemType x,ElemType e) {//指定位置插入元素 if (x<0 || x>list->size) printf("插入位置不合法!\n"); if ((list->size >= list->capacity && !inc(list))) { printf("空间已满,无法实现尾插!\n"); } else { for (int i = list->size; i >= x; i--) { list->base[i + 1] = list->base[i]; } list->base[x] = e; list->size++; } } ElemType find(SeqList *list,ElemType x) { for (int i = 0; i < list->size; i++) { if (x == list->base[i]) { return i; } } return -1; } ElemType length(SeqList *list) { return list->size; } void delete_pos(SeqList *list, ElemType pos) {//删除下标pos的元素 int i = 0; if (pos < 0 || pos >= list->size) printf("pos位置输入错误\n"); else for (i = pos; i < list->size - 1; i++) { list->base[i] = list->base[i + 1]; } list->size--; return 0; } void delete_val(SeqList *list, ElemType val) {//删除值为val的元素 int pos = find(list, val); if (pos == -1) printf("要删除的%d不在顺序表中", val); else { delete_pos(list, pos); } return 0; } void sort(SeqList *list) {//冒泡排序 int i = 0, j = 0, temp = 0; for (i = 1; i <= list->size - 1; i++) { for (j = 0; j <= list->size - 1 - i; j++) { if (list->base[j] > list->base[j + 1]) { temp = list->base[j]; list->base[j] = list->base[j+1]; list->base[j + 1] = temp; } } } } void resver(SeqList *list) {//逆序 int k = 0, temp = 0; for (k = 0; k < (list->size) / 2; k++) { temp = list->base[k]; list->base[k] = list->base[list->size - k - 1]; list->base[list->size - k - 1] = temp; } } void clear(SeqList *list) {//清除数据 list->size = 0; } void destory(SeqList *list) {//摧毁顺序表 free(list->base); list->base = NULL; list->capacity = 0; list->size = 0; } bool inc(SeqList *list){//自增空间函数 ElemType *newbase = (ElemType *)realloc(list->base, sizeof(ElemType)*(list->capacity + INC_SIZE)); if (newbase == NULL) { printf("增配空间失败,内存不足!\n"); return false; } else { list->base = newbase; list->capacity += INC_SIZE; return true; } } void merge(SeqList *list1, SeqList *list2, SeqList *list3) { int ia = 0, ib = 0, ic = 0; list3->capacity = list1->size + list2->size; list3->base = (ElemType*)malloc(sizeof(ElemType)*list3->capacity); assert(list3->base != NULL); while (ia < list1->size&&ib < list2->size) { if (list1->base[ia] < list2->base[ib]) list3->base[ic++] = list1->base[ia++]; else list3->base[ic++] = list2->base[ib++]; } while (ia < list1->size) { list3->base[ic++] = list1->base[ia++]; } while (ib < list2->size) { list3->base[ic++] = list2->base[ib++]; } list3->size = list1->size + list2->size; }
测试文件Main.c
#include"SeqList.h" #include<stdio.h> #include<stdlib.h> #define _CRT_SECURE_NO_WARNINGS int main() { SeqList mylist, list1, list2, list3; InitSeqList(&mylist); ElemType elem = 0, pos = 0; int select = 1; while (select) { printf("\n**************************************\n"); printf("*[1] push_back [2] push_front*\n"); printf("*[3] show_list [4] pop_back*\n"); printf("*[5] pop_front [6] insert_pos*\n"); printf("*[7] find [8] length*\n"); printf("*[9] delete_pos [10]delete_val*\n"); printf("*[11] sort [12] resver*\n"); printf("*[13] clear [14]merge*\n"); printf("*[0] quit_system\n "); printf("\n**************************************\n"); printf("请选择你要进行的操作>\n"); scanf("%d", &select); if (select == 0) break; switch (select) { case 1: printf("请输入要插入的数据(-1)结束:\n"); while (scanf("%d", &elem), elem != -1) push_back(&mylist, elem); break; case 2: printf("请输入要插入的数据(-1)结束:\n"); while (scanf("%d", &elem), elem != -1) push_front(&mylist, elem); break; case 3: printf("结果如下:\n"); show_list(&mylist); break; case 4: pop_back(&mylist); break; case 5: pop_front(&mylist); break; case 6: printf("请选择要插入的下标和元素:\n"); scanf("%d%d", &pos, &elem); insert_pos(&mylist, pos, elem); break; case 7: printf("请输入你要查询的元素:\n"); scanf("%d", &elem); pos = find(&mylist, elem); if (pos == -1) printf("查找的%d不在顺序表中!\n", elem); else printf("查找的%d在顺序表中的下标为%d\n", elem, pos); case 8: printf("顺序表的长度为%d", length(&mylist)); break; case 9: printf("请输入你要删除元素的下标:\n"); scanf("%d", &pos); delete_pos(&mylist, pos); break; case 10: printf("请输入你要删除的数据:\n"); scanf("%d", &elem); delete_val(&mylist, elem); break; case 11: sort(&mylist); break; case 12: resver(&mylist); break; case 13: clear(&mylist); break; case 14: InitSeqList(&list1); InitSeqList(&list2); push_back(&list1, 1); push_back(&list1, 3); push_back(&list1, 5); push_back(&list2, 2); push_back(&list2, 4); push_back(&list2, 6); merge(&list1, &list2, &list3); show_list(&list3); break; default: printf("输入数据有误,请重新输入\n"); break; } } destory(&mylist); }