静态顺序表
来源:互联网 发布:mac卸载海马玩模拟器 编辑:程序博客网 时间:2024/05/24 06:21
顺序表
顺序表 :用一段地址连续的存储单元一次存储数据元素的线性结构
连续的地址空间 ,一般情况下采用数组,但是数组有静态数组和动态数组,所以顺序表分为:静态顺序表和动态顺序表
静态顺序表
1. 结构:
#define MAX_SiZE 10typedef int DataType;typedef struct SeqList { DataType array[MAX_SIZE]; int size; // 表示顺序表中有效元素的个数 }SeqList, *PSeqList;
开始定义了一个宏 MAX_SiZE 用来表示最大个数,这里设置为10,可以根据自己的情况进行修改数字。重定义了 int 类型为 DataTypr 这样做更加方便我们进行数据类型的设置,比如此时想要存储的是 char类型的,那只需要修改此处的 int 就可以了。结构体 SeqList 包含了一个存放数据的容量固定的数组,类型设置为 DataTypr;和一个int型的 size,表示顺序表中有效元素的个数,在声明的同时重定向为一个顺序表结构 SeqList 和指向该顺序表的指针 *PSeqList。
2. 功能
主要实现初始化,尾插,尾删,头插,头删,任意位置上的插入和删除和输出的功能
void InitSeqList(PSeqList pSeq);// 初始化顺序表 void SeqListPushBack(PSeqList pSeq, DataType data);// 在顺序表的尾部插入元素data void SeqListPopBack(PSeqList pSeq);// 删除顺序表尾部的元素 void SeqListPushFront(PSeqList pSeq, DataType data);// 在顺序表的头部插入元素data void SeqListPopFront(PSeqList pSeq);// 删除顺序表头部元素 void SeqListInsert(PSeqList pSeq, int pos, DataType data);// 在顺序表的pos位置插入元素data void SeqListErase(PSeqList pSeq, int pos);// 删除顺序表pos位置上的元素 void SeqListPrintf(PSeqList pSeq);//输出整个静态表
3. 各部分功能实现
- 初始化顺序表
void InitSeqList(PSeqList pSeq){ assert(pSeq); if (!(pSeq->size > 10)) { for (int i = 0; i < MAX_SIZE; i++) { pSeq->array[i] = 0; } pSeq->size = 0; } else printf("The number of people exceeds!\n");}
- 尾插
void SeqListPushBack(PSeqList pSeq, DataType data){ assert(pSeq); if (pSeq->size < 10) { pSeq->array[pSeq->size++] = data; } else printf("The number of people exceeds!\n");}
- 尾删
void SeqListPopBack(PSeqList pSeq)// 删除顺序表尾部的元素 { assert(pSeq); if (!(pSeq->size > 10)) { pSeq->size--; } else printf("The number of people exceeds!\n");}
- 头插
void SeqListPushFront(PSeqList pSeq, DataType data)// 在顺序表的头部插入元素data { assert(pSeq); if (!(pSeq->size >= 10)) { for (int i = pSeq->size++; i > 0; i--) { pSeq->array[i] = pSeq->array[i - 1]; } pSeq->array[0] = data; } else printf("The number of people exceeds!\n");}
- 头删
void SeqListPopFront(PSeqList pSeq)// 删除顺序表头部元素 { assert(pSeq); if (!(pSeq->size > 10)) { for (int i = 0; i < pSeq->size-1; i++) { pSeq->array[i] = pSeq->array[i + 1]; } pSeq->size--; } else printf("The number of people exceeds!\n");}
- 任意位置的插入和删除
void SeqListInsert(PSeqList pSeq, int pos, DataType data)// 在顺序表的pos位置插入元素data{ assert(pSeq); if (!(pSeq->size >= 10)) { for (int i = pSeq->size++; i >= pos - 1; i--) { pSeq->array[i] = pSeq->array[i - 1]; } pSeq->array[pos - 1] = data; } else printf("The number of people exceeds!\n");}void SeqListErase(PSeqList pSeq, int pos)// 删除顺序表pos位置上的元素 { assert(pSeq); if (!(pSeq->size > 10)) { for (int i = pos-1; i < pSeq->size; i++) { pSeq->array[i] = pSeq->array[i + 1]; } pSeq->size--; } else printf("The number of people exceeds!\n");}
- 输出
void SeqListPrintf(PSeqList pSeq)//输出整个静态{ for (int i = 0; i < pSeq->size; i++) { printf("%d ", pSeq->array[i]); } printf("\n");}
关键点 : (1)每个函数的开始都要进行参数检测,看是否为空指针 (2)在进行添加数据时一定要注意数据长度,确定没有超出设定值 (3)在改变删除和添加时,不要忘记对 size 数值进行修改(容易忘记)
代码
代码实现(Visual Studio 2017)
- test.c 文件
int main(){ SeqList_test(); system("pause"); return 0;}
- SeqList.h 文件
#pragma once#ifndef _SEQLIST_H_#define _SEQLIST_H_#define MAX_SIZE 10 typedef int DataType;typedef struct SeqList// 静态顺序表 { DataType array[MAX_SIZE]; int size; // 表示顺序表中有效元素的个数 }SeqList, *PSeqList;////////////////////////////////////// void InitSeqList(PSeqList pSeq);// 初始化顺序表 void SeqListPushBack(PSeqList pSeq, DataType data);// 在顺序表的尾部插入元素data void SeqListPopBack(PSeqList pSeq);// 删除顺序表尾部的元素 void SeqListPushFront(PSeqList pSeq, DataType data);// 在顺序表的头部插入元素data void SeqListPopFront(PSeqList pSeq);// 删除顺序表头部元素 void SeqListInsert(PSeqList pSeq, int pos, DataType data);// 在顺序表的pos位置插入元素data void SeqListErase(PSeqList pSeq, int pos);// 删除顺序表pos位置上的元素 void SeqListPrintf(PSeqList pSeq);//输出整个静态表#endif // !_SEQLIST_H_
- SeqList.c 文件
#include <stdio.h>#include <windows.h>#include <assert.h>#include "SeqList.h"#pragma warning (disable : 4996)void InitSeqList(PSeqList pSeq){ assert(pSeq); if (!(pSeq->size > 10)) { for (int i = 0; i < MAX_SIZE; i++) { pSeq->array[i] = 0; } pSeq->size = 0; } else printf("The number of people exceeds!\n");}void SeqListPushBack(PSeqList pSeq, DataType data){ assert(pSeq); if (pSeq->size < 10) { pSeq->array[pSeq->size++] = data; } else printf("The number of people exceeds!\n");}void SeqListPopBack(PSeqList pSeq)// 删除顺序表尾部的元素 { assert(pSeq); if (!(pSeq->size > 10)) { pSeq->size--; } else printf("The number of people exceeds!\n");}void SeqListPushFront(PSeqList pSeq, DataType data)// 在顺序表的头部插入元素data { assert(pSeq); if (!(pSeq->size >= 10)) { for (int i = pSeq->size++; i > 0; i--) { pSeq->array[i] = pSeq->array[i - 1]; } pSeq->array[0] = data; } else printf("The number of people exceeds!\n");}void SeqListPopFront(PSeqList pSeq)// 删除顺序表头部元素 { assert(pSeq); if (!(pSeq->size > 10)) { for (int i = 0; i < pSeq->size-1; i++) { pSeq->array[i] = pSeq->array[i + 1]; } pSeq->size--; } else printf("The number of people exceeds!\n");}void SeqListInsert(PSeqList pSeq, int pos, DataType data)// 在顺序表的pos位置插入元素data{ assert(pSeq); if (!(pSeq->size >= 10)) { for (int i = pSeq->size++; i >= pos - 1; i--) { pSeq->array[i] = pSeq->array[i - 1]; } pSeq->array[pos - 1] = data; } else printf("The number of people exceeds!\n");}void SeqListErase(PSeqList pSeq, int pos)// 删除顺序表pos位置上的元素 { assert(pSeq); if (!(pSeq->size > 10)) { for (int i = pos-1; i < pSeq->size; i++) { pSeq->array[i] = pSeq->array[i + 1]; } pSeq->size--; } else printf("The number of people exceeds!\n");}void SeqListPrintf(PSeqList pSeq)//输出整个静态{ for (int i = 0; i < pSeq->size; i++) { printf("%d ", pSeq->array[i]); } printf("\n");}void SeqList_mian(PSeqList pSeq){ InitSeqList(pSeq); SeqListPushBack(pSeq, 0); SeqListPushBack(pSeq, 1); SeqListPushBack(pSeq, 2); SeqListPushBack(pSeq, 3); SeqListPushBack(pSeq, 4); SeqListPrintf(pSeq); SeqListPopBack(pSeq); SeqListPrintf(pSeq); SeqListPushFront(pSeq, 5); SeqListPrintf(pSeq); SeqListPopFront(pSeq); SeqListPrintf(pSeq); SeqListInsert(pSeq, 3, 8); SeqListPrintf(pSeq); SeqListErase(pSeq,3); SeqListPrintf(pSeq);}void SeqList_test(){ SeqList Seq = {0}; PSeqList pSeq = &Seq; SeqList_mian(pSeq);}
阅读全文
0 0
- 静态顺序表
- 静态顺序表
- 静态顺序表
- 顺序表(静态)
- 静态顺序表
- 静态顺序表
- 静态顺序表
- 静态顺序表
- 简易静态顺序表
- 静态顺序表
- 静态顺序表
- 静态顺序表
- 静态顺序表
- 静态顺序表
- 顺序表(静态)
- 顺序表(静态)
- 静态,动态顺序表
- 静态顺序表
- 【Codevs1041】Car的旅行路线 Floyd 简单几何 (9/1000)
- 框架学习之坑
- MySqlServer安装到连接全过程
- 每日练习|Day011
- 深度增强学习(DRL)漫谈
- 静态顺序表
- Linux:使用bitnami向阿里云一键安装LAMP
- jsp 之 内置对象(9个)
- samba案例
- BigDecimal加减乘除以及大小比较
- 三位数的 黑洞数495 HNUST OJ 1868 ( 三位数的比较 )
- Activity中finish() onDestroy() 和System.exit()的区别
- spring学习笔记(三) --- bean中的引用赋值和数组/集合赋值
- 用深度学习构造聊天机器人简介