数据结构顺序表功能的C语言版并附图验证
2018-03-28 15:43
471 查看
实现基于静态数组的顺序表的以下基本操作:
1. 初始化
2. 尾插
3. 尾删
4. 头插
5. 头删
6. 读任意位置元素
7. 修改任意位置元素
8. 查找指定元素值的下标
9. 在任意位置插入元素
10.删除顺序表中指定的值, 如果存在重复元素, 只删除第一个
11.删除顺序表中所有的指定的值, 另外要实现一个时间复杂度为 O(N) 的优化版本
12.获取顺序表元素个数
13.判定顺序表是否为空
14.冒泡排序
附验证图如下
1 初始化
2 尾插
3 尾删
4 头插
5 头删
6 任意位置插入
7 任意位置删除
8 读取任意位置元素值
9 修改任意位置的元素值
10 查找指定元素的下标
11 删除指定的值如果存在重复元素只删除第一个
12 删除指定的所有的元素
13 获取顺序表的长度
14 顺序表是否为空
15 冒泡排序升序
1. 初始化
2. 尾插
3. 尾删
4. 头插
5. 头删
6. 读任意位置元素
7. 修改任意位置元素
8. 查找指定元素值的下标
9. 在任意位置插入元素
10.删除顺序表中指定的值, 如果存在重复元素, 只删除第一个
11.删除顺序表中所有的指定的值, 另外要实现一个时间复杂度为 O(N) 的优化版本
12.获取顺序表元素个数
13.判定顺序表是否为空
14.冒泡排序
L.h //顺序表头文件
#pragma once //防止头文件重复定义 #include<stdio.h> #include<stdlib.h> #define MAX_SIZE 100 //表示数组的最大长度 #define TEST_HEADER printf("\n========%s=======\n",__FUNCTION__) //定义宏,当前函数的函数名 typedef char SeqListType; typedef struct SeqList //定义结构体 { SeqListType arr[MAX_SIZE]; //最大空间 size_t size; //有效元素 }SeqList; void SeqListInit(SeqList *L); //初始化 void SeqListPushBack(SeqList *L, SeqListType e); //尾插 void SeqListPopBack (SeqList *L); //尾删 void SeqListPushFront(SeqList *L, SeqListType e); //头插 void SeqListPopFront(SeqList *L); //头删 void SeqListInsert(SeqList *L, int pos, SeqListType e); //任意位置插入 void SeqListErase(SeqList *L, int pos); //人任意位置删除 void SeqListGetValue(SeqList *L, int pos); //读取任意位置元素 void SeqListSetValue(SeqList *L, int pos,SeqListType e); //修改任意位置元素 size_t SeqListGetPos(SeqList *L, SeqListType e); //查找指定元素的下标 void SeqListRemove(SeqList *L, SeqListType to_remove); //删除指定的值如果存在重复元素只删除第一个 void SeqListRemoveAll(SeqList *L, SeqListType to_delete); //删除所有的指定元素,实现时间复杂度O(N)的优化 size_t SeqListSize(SeqList *L); //获取顺序表元素个数 int SeqListEmpty(SeqList *L); //判断顺序表是否为空 void SeqListBubbleSort(SeqList *L); //冒泡排序
L.c //功能实现文件
#include "L.h" void SeqListInit(SeqList *L) //顺序表初始化 { if (L == NULL) { printf("非法输入\n"); return; } L->size = 0; } void SeqListPrintChar(SeqList*L, const char *msg) //打印函数 { if (L == NULL) { printf("非法输入"); return; } printf("[%s]\n", msg); size_t i = 0; for (i = 0; i < L->size; ++i) { printf("[%c]", L->arr[i]); } printf("\n"); } void SeqListPushBack(SeqList *L, SeqListType e) //尾插 { if (L == NULL) { printf("非法输入\n"); return; } if (L->size >= MAX_SIZE) { printf("顺序表已经满了"); return; } L->arr[L->size] = e; ++L->size; //更新元素个数 } void SeqListPopBack(SeqList *L) //尾删 { if (L == NULL) { printf("非法输入\n"); return; } if (L->size == 0) { printf("空顺序表\n"); return; } --L->size; //使最后一个元素无效 return; } void SeqListPushFront(SeqList *L, SeqListType e) //头插 { if (L == NULL) { printf("非法输入\n"); return; } if (L->size >= MAX_SIZE) { printf("顺序表已经满了\n"); return; } ++L->size; size_t i = L->size-1; for (; i > 0;i--) { L->arr[i] = L->arr[i - 1]; } L->arr[0] = e; return; } void SeqListPopFront(SeqList *L) //头删 { if (L == NULL) { printf("非法输入\n"); return; } if (L->size == 0) 4000 { printf("空顺序表"); return; } size_t i = 0; for (; i < L->size - 1; i++) { L->arr[i] = L->arr[i+1]; } --L->size; return; } void SeqListInsert(SeqList *L, int pos, SeqListType e) //任意位置插入 { if (L == NULL) { printf("非法输入\n"); return; } if (L->size == MAX_SIZE) { printf("顺序表已经满了"); return; } if (pos == 0) { SeqListPushFront(L, e); } ++L->size; int i = L->size - 1; for (; i > pos; i--) { L->arr[i] = L->arr[i-1]; } L->arr[pos] = e; return; } void SeqListErase(SeqList *L, int pos) //任意位置删除 { if (L == NULL) { printf("非法输入\n"); return; } if (L->size == 0) { printf("空顺序表"); return; } if (pos >= L->size) { return; } int i = pos; for (; i < L->size - 1; i++) { L->arr[i] = L->arr[i + 1]; } --L->size; return; } void SeqListGetValue(SeqList *L, int pos) //读取任意位置的元素 { if (L == NULL) { printf("非法输入\n"); return; } if (L->size == 0) { printf("空顺序表"); return; } if (pos >= L->size) { printf("读取超过范围"); return; } printf("要查找的元素为 %c\n", L->arr[pos]); return L->arr[pos]; } void SeqListSetValue(SeqList *L, int pos, SeqListType e) //修改任意位置的元素 { if (L == NULL) { printf("非法输入\n"); return; } if (pos >= L->size) { printf("读取超过范围"); return; } L->arr[pos] = e; return; } size_t SeqListGetPos(SeqList *L, SeqListType value){//查找指定元素值的下标 if (L == NULL){ printf("非法输入\n"); return; } size_t i = 0; for (; i <L->size; i++){ if (L->arr[i] == value){ printf("%d\n", i); return i; } } printf("非法坐标\n"); return -1; } void SeqListRemove(SeqList *L, SeqListType to_remove) //删除指定的值如果存在重复元素只删除第一个 { if (L == NULL) { printf("非法输入\n"); return; } size_t i = SeqListGetPos(L, to_remove); if (i < 0) { return; } SeqListErase(L, i); return; } void SeqListRemoveAll(SeqList* L, SeqListType to_delete){//删除顺序表中所有的指定的值,实现时间复杂度O(N)的优化 if (L == NULL) { printf("非法输入\n"); return; } size_t i = 0; for (; i < L->size - 1; i++) { size_t i = SeqListGetPos(L, to_delete); if (i < 0) { printf("没有找到"); return; } SeqListErase(L, i); } return; } size_t SeqListSize(SeqList *L) //获取顺序表元素的个数 { if (L == NULL) { printf("非法输入\n"); return; } if (L->size == 0) { printf("空顺序表\n"); return; } return L->size; } int SeqListEmpty(SeqList *L) //判断顺序表是否为空 { if (L == NULL) { printf("非法输入\n"); return; } int i = 1; if (L->size == 0) { printf("空顺序表\n"); return i; } return 0; } static void swapvalue(SeqListType *a, SeqListType *b) //交换函数 { SeqListType tmp = *a; *a = *b; *b = tmp; return; } void SeqListBubbleSort(SeqList *L) //冒泡排序 { if (L == NULL) { printf("非法输入\n"); return; } int i = 0; int j = 0; int flag = 0; for (i = 0; i < L->size - 1; i++) { for (j = 0; j < L->size - i - 1; j++) { if (L->arr[j]>L->arr[j + 1]) { flag = 1; swapvalue(&L->arr[j], &L->arr[j + 1]); } } if (flag == 0) { break; } } }
test.c //测试文件
#include "L.h" void TestInit() //测试初始化 { TEST_HEADER; SeqList L; SeqListInit(&L); printf("L.size expect 0,actual %lu\n", L.size); } void TestPushBack()//尾插 { TEST_HEADER; SeqList L; SeqListInit(&L); SeqListPushBack(&L, 'a'); SeqListPushBack(&L, 'b'); SeqListPushBack(&L, 'c'); SeqListPushBack(&L, 'd'); SeqListPrintChar(&L, "尾部插入四个元素"); } void TestPopBack() //尾删 { TEST_HEADER; SeqList L; SeqListInit(&L); SeqListPushBack(&L, 'a'); SeqListPushBack(&L, 'b'); SeqListPushBack(&L, 'c'); SeqListPushBack(&L, 'd'); SeqListPopBack(&L); SeqListPopBack(&L); SeqListPrintChar(&L, "尾部删除两个元素"); } void TestPushFront() //头插 { TEST_HEADER; SeqList L; SeqListInit(&L); SeqListPushBack(&L, 'a'); SeqListPushBack(&L, 'b'); SeqListPushBack(&L, 'c'); SeqListPushBack(&L, 'd'); SeqListPushFront(&L, 'x'); SeqListPushFront(&L, 'y'); SeqListPrintChar(&L, "头部插入两个元素"); } void TestPopFront() //头删 { TEST_HEADER; SeqList L; SeqListInit(&L); SeqListPushBack(&L, 'a'); SeqListPushBack(&L, 'b'); SeqListPushBack(&L, 'c'); SeqListPushBack(&L, 'd'); SeqListPopFront(&L); SeqListPopFront(&L); SeqListPrintChar(&L, "头部删除两个元素"); } void TestInsert() { TEST_HEADER; SeqList L; SeqListInit(&L); SeqListPushBack(&L, 'a'); SeqListPushBack(&L, 'b'); SeqListPushBack(&L, 'c'); SeqListPushBack(&L, 'd'); SeqListInsert(&L, 2, 'y'); SeqListPrintChar(&L, "任意位置插入"); } void TestErase() //任意位置删除 { TEST_HEADER; SeqList L; SeqListInit(&L); SeqListPushBack(&L, 'a'); SeqListPushBack(&L, 'b'); SeqListPushBack(&L, 'c'); SeqListPushBack(&L, 'd'); SeqListErase(&L, 1); SeqListPrintChar(&L, "任意位置删除"); } void TestGetValue() //读取任意位置的元素 { TEST_HEADER; SeqList L; SeqListInit(&L); SeqListPushBack(&L, 'a'); SeqListPushBack(&L, 'b'); SeqListPushBack(&L, 'c'); SeqListPushBack(&L, 'd'); SeqListGetValue(&L, 1); SeqListPrintChar(&L, "在2的位置读取元素值"); } void TestSetValue() //修改任意位置的元素值 { TEST_HEADER; SeqList L; SeqListInit(&L); SeqListPushBack(&L, 'a'); SeqListPushBack(&L, 'b'); SeqListPushBack(&L, 'c'); SeqListPushBack(&L, 'd'); SeqListSetValue(&L, 1, 'X'); SeqListPrintChar(&L, "把2的位置元素值改为X"); } void TestGetPos() // 查找指定元素的下标 { TEST_HEADER; SeqList L; SeqListInit(&L); SeqListPushBack(&L, 'a'); SeqListPushBack(&L, 'b'); SeqListPushBack(&L, 'c'); SeqListPushBack(&L, 'd'); SeqListGetPos(&L, 'b'); SeqListPrintChar(&L, "查找元素b的下标"); } void TestRemove() { TEST_HEADER; SeqList L; Se bf54 qListInit(&L); SeqListPushBack(&L, 'a'); SeqListPushBack(&L, 'b'); SeqListPushBack(&L, 'b'); SeqListPushBack(&L, 'c'); SeqListPushBack(&L, 'd'); SeqListRemove(&L, 'b'); SeqListPrintChar(&L, "删除第一个b"); } void TestRemoveAll() { TEST_HEADER; SeqList L; SeqListInit(&L); SeqListPushBack(&L, 'a'); SeqListPushBack(&L, 'b'); SeqListPushBack(&L, 'b'); SeqListPushBack(&L, 'c'); SeqListPushBack(&L, 'd'); SeqListRemoveAll(&L,'b'); SeqListPrintChar(&L, "删除所有的元素b"); } void TestSize() { TEST_HEADER; SeqList L; SeqListInit(&L); SeqListPushBack(&L, 'a'); SeqListPushBack(&L, 'b'); SeqListPushBack(&L, 'c'); SeqListPushBack(&L, 'd'); size_t t=SeqListSize(&L); SeqListPrintChar(&L, "获取顺序表的长度"); printf("%d\n", t); } void TestEmpty() { TEST_HEADER; SeqList L; SeqListInit(&L); SeqListPushBack(&L, 'a'); SeqListPushBack(&L, 'b'); SeqListPopBack(&L, 'a'); SeqListPopBack(&L, 'b'); SeqListPrintChar(&L, "顺序表是否为空"); SeqListEmpty(&L); } void TestBubbleSort() { TEST_HEADER; SeqList L; SeqListInit(&L); SeqListPushBack(&L, 'a'); SeqListPushBack(&L, 'd'); SeqListPushBack(&L, 'c'); SeqListPushBack(&L, 'b'); SeqListBubbleSort(&L); SeqListPrintChar(&L, "冒泡排序升序"); } int main() { //TestInit(); //初始化 //TestPushBack(); //尾插 //TestPopBack(); //尾删 //TestPushFront(); //头插 //TestPopFront(); //头删 //TestInsert(); //任意位置插入 //TestErase(); //任意位置删除 //TestGetValue(); //读取任意位置元素值 //TestSetValue(); //修改任意位置的元素值 //TestGetPos(); //查找指定元素的下标 //TestRemove(); //删除指定的值如果存在重复元素只删除第一个 //TestRemoveAll(); //删除所有的元素b //TestSize(); //获取顺序表的长度 //TestEmpty(); //顺序表是否为空 //TestBubbleSort(); //冒泡排序升序 return 0; }
附验证图如下
1 初始化
2 尾插
3 尾删
4 头插
5 头删
6 任意位置插入
7 任意位置删除
8 读取任意位置元素值
9 修改任意位置的元素值
10 查找指定元素的下标
11 删除指定的值如果存在重复元素只删除第一个
12 删除指定的所有的元素
13 获取顺序表的长度
14 顺序表是否为空
15 冒泡排序升序
相关文章推荐
- Sql2012如何将远程服务器数据库及表、表结构、表数据导入本地数据库 自定义日志记录功能,按日记录,很方便 C#常量和字段以及各种方法的语法总结 类型,对象,线程栈,托管堆在运行时的关系,以及clr如何调用静态方法,实例方法,和虚方法 asp.net webapi 自定义身份验证
- 数据结构——顺序表
- 数据结构--顺序栈基本功能实现程序…
- 标准功能模块组件 -- “文档管理组件,网络文档管理,网络存储”,B\S版本组件可独立运行,也可集成到其他项目里,数据结构清晰思路严谨
- 数据结构-顺序表(第二篇)
- c语言_数据结构_顺序表
- 数据结构(C语言版 严蔚敏著)——串
- 数据结构实验--顺序表操作
- 数据结构顺序表(2)(链表)
- 数据结构之线性表2顺序表
- 打造结构一体化、功能多元化的高效数据流水线
- 数据结构-顺序表(1)
- 数据结构顺序查找验证程序
- java虚拟机运行时数据区结构和功能分析
- 数据结构之插入排序的实现(C语言版)
- 数据结构实验1 链表 实现学生信息存储,功能 增删改查,实现 结构类型
- 数据结构顺序表
- 程序员面试宝典之数据结构基础----C++两个栈实现一个队列功能
- 标准功能模块组件 -- “文档管理组件,网络文档管理,网络存储”,B/S版本组件可独立运行,也可集成到其他项目里,数据结构清晰思路严谨
- 货物管理系统(数据结构顺序表)