静态顺序表
来源:互联网 发布:类似沙盘的软件 编辑:程序博客网 时间:2024/05/22 00:18
静态顺序表:用一组地址连续的存储单元依次存储数据元素的线性结构
静态顺序表结构:
#define MAX_SIZE 10typedef int Datatype;typedef struct SeqLists{ Datatype data[MAX_SIZE]; size_t size;}Seqlist, *PSeqlist;
静态顺序表基本操作:
#include<stdio.h>#include<assert.h>#define MAX_SIZE 10typedef int Datatype;typedef struct SeqLists{ Datatype data[MAX_SIZE]; size_t size;}Seqlist, *PSeqlist;void IniSeqList(PSeqlist seq)//初始化{ int i = 0; for (i = 0; i < MAX_SIZE;++i) { seq->data[i] = 0; } seq->size = 0;}void PushBack(PSeqlist pSeq, Datatype data)//尾插{ assert(pSeq); if (pSeq->size < MAX_SIZE) { pSeq->data[pSeq->size] = data; pSeq->size += 1; }}void PopBack(PSeqlist pSeq)//尾删{ assert(pSeq); if (pSeq->size>0) { pSeq->size -= 1; }}void PushFront(PSeqlist pSeq, Datatype data)//头插{ assert(pSeq); int i = 0; if (pSeq->size < MAX_SIZE) { for (i = pSeq->size; i > 0;--i) { pSeq->data[i] = pSeq->data[i-1]; } pSeq->data[i] = data; pSeq->size += 1; }}void PopFront(PSeqlist pSeq)//头删{ assert(pSeq); size_t i = 0; for (i=0; i < pSeq->size-1; ++i) { pSeq->data[i] = pSeq->data[i + 1]; } pSeq->size -= 1;}void Insert(PSeqlist pSeq, size_t pos, Datatype data)//在pos位置插入值为data的数据{ assert(pSeq); size_t i = 0; for (i = pSeq->size; i > pos; --i) { pSeq->data[i] = pSeq->data[i - 1]; } pSeq->data[i] = data; pSeq->size += 1;}void Erase(PSeqlist pSeq, size_t pos)//删除pos位置上的元素{ assert(pSeq); size_t i = 0; for (i = pos; i < pSeq->size-1; ++i) { pSeq->data[i] = pSeq->data[i + 1]; } pSeq->size -= 1;}int Find(PSeqlist pSeq, Datatype data)//查找值为data的数据{ assert(pSeq); size_t i = 0; for (; i < pSeq->size; ++i) { if (pSeq->data[i] == data) return i; } return -1; }void Remove(PSeqlist pSeq, Datatype data)//删除第一个值为data的数据{ assert(pSeq); Erase(pSeq, Find(pSeq, data));}void RemoveAll(PSeqlist pSeq, Datatype data)//删除所有值为data的数据{ assert(pSeq); size_t i = 0,count = 0; for (; i < pSeq->size; ++i) { if (pSeq->data[i] == data) { ++count; } else { pSeq->data[i - count] = pSeq->data[i]; } } pSeq->size -= count;}void PrintSeq(PSeqlist pSeq)//打印顺序表{ assert(pSeq); size_t i = 0; for (; i < pSeq->size;++i) { printf("%d ",pSeq->data[i]); } printf("\n");}
0 0
- 静态顺序表
- 静态顺序表
- 静态顺序表
- 顺序表(静态)
- 静态顺序表
- 静态顺序表
- 静态顺序表
- 静态顺序表
- 简易静态顺序表
- 静态顺序表
- 静态顺序表
- 静态顺序表
- 静态顺序表
- 静态顺序表
- 顺序表(静态)
- 顺序表(静态)
- 静态,动态顺序表
- 静态顺序表
- 【LeetCode】236. Lowest Common Ancestor of a Binary Tree
- 分组密码模式的总结
- MySQL自带数据库
- 轮播图组件
- ViewPager底部圆点指示器
- 静态顺序表
- Logger日志
- Payment:微信支付发起支付请求文档
- 1
- 二叉树的中序遍历
- 2
- 京东2017实习生招聘——在线笔试编程题总结
- 怎样成为一名黑客
- 【百度百科】Nosql科普