C语言实现静态顺序表的实例详解
C语言实现静态顺序表的实例详解
线性表
定义一张顺序表也就是在内存中开辟一段连续的存储空间,并给它一个名字进行标识。只有定义了一个顺序表,才能利用该顺序表存放数据元素,也才能对该顺序表进行各种操作。
接下来看看静态的顺序表,直接上代码:
SeqList.h
#define _CRT_SECURE_NO_WARNINGS 1 #ifndef __SEQLIST_H__ #define __SEQLIST_H__ #include <stdio.h> #include <stdlib.h> #include <assert.h> #include <string.h> #define MAX 10 typedef int DataType; typedef struct SeqList { DataType data[MAX]; int sz; }SeqList,*pSeqList; void InitSeqList(pSeqList ps); void PushBack(pSeqList ps, DataType d); void PopBack(pSeqList ps); void PushFront(pSeqList ps, DataType d); void PopFront(pSeqList ps); void Display(const pSeqList ps); int Find(pSeqList ps, DataType d); void Insert(pSeqList ps, DataType d, int pos); void Remove(pSeqList ps, DataType d); void RemoveAll(pSeqList ps, DataType d); void Reverse(pSeqList ps); void Sort(pSeqList ps); int BinarySearch(pSeqList ps, DataType d); #endif//__SEQLIST_H__
SeqList.c
#define _CRT_SECURE_NO_WARNINGS 1 #include "SeqList.h" void InitSeqList(pSeqList ps) { assert(ps); ps->sz = 0; memset(ps->data, 0, sizeof(DataType)*MAX); } void PushBack(pSeqList ps, DataType d) { assert(ps); if (ps->sz == MAX) { return; } ps->data[ps->sz] = d; ps->sz++; } void PopBack(pSeqList ps) { assert(ps); if (ps->sz == 0) { return; } ps->sz--; } void PushFront(pSeqList ps, DataType d) { int i = 0; assert(ps); for (i = ps->sz; i >= 1; i--) { ps->data[i] = ps->data[i - 1]; } ps->data[0] = d; ps->sz++; } void PopFront(pSeqList ps) { int i = 0; assert(ps); for (i = 0; i < ps->sz; i++) { ps->data[i] = ps->data[i + 1]; } ps->sz--; } void Display(const pSeqList ps) { int i = 0; assert(ps); for (i = 0; i < ps->sz; i++) { printf("%d ", ps->data[i]); } printf("\n"); } int Find(pSeqList ps, DataType d) { int i = 0; assert(ps); for (i = 0; i < ps->sz; i++) { if (ps->data[i] == d) { return i; } } return -1; } void Insert(pSeqList ps, DataType d, int pos) { int i = 0; assert(ps); if (ps->sz == MAX) { return; } //方式一 //for (i = ps->sz - 1; i >= pos; i--) //{ // ps->data[i + 1] = ps->data[i]; //} //方式二 memmove(ps->data + pos + 1, ps->data + pos, sizeof(DataType)*(ps->sz - pos)); ps->data[pos] = d; ps->sz++; } void Remove(pSeqList ps, DataType d) { int i = 0; int pos = 0; assert(ps); pos = Find(ps, d); if (pos != -1) { for (i = pos; i < ps->sz; i++) { ps->data[i] = ps->data[i + 1]; } ps->sz--; } } void RemoveAll(pSeqList ps, DataType d) { int i = 0; int pos = 0; assert(ps); pos = Find(ps, d); while ((pos = Find(ps, d)) != -1) { for (i = pos; i < ps->sz; i++) { ps->data[i] = ps->data[i + 1]; } ps->sz--; } } void Reverse(pSeqList ps) { int left = 0; int right = ps->sz - 1; assert(ps); while (left < right) { DataType tmp = ps->data[right]; ps->data[right] = ps->data[left]; ps->data[left] = tmp; left++; right--; } } void Sort(pSeqList ps) { int i = 0; int j = 0; assert(ps); for (i = 0; i < ps->sz; i++) { for (j = 0; j < ps->sz - i - 1; j++) { if (ps->data[j]>ps->data[j + 1]) { DataType tmp = ps->data[j]; ps->data[j] = ps->data[j + 1]; ps->data[j + 1] = tmp; } } } } int BinarySearch(pSeqList ps, DataType d) { int left = 0; int right = ps->sz - 1; while (left <= right) { int mid = left - ((left - right) >> 1); if (d > ps->data[mid]) { left = mid + 1; } else if (d < ps->data[mid]) { right = mid - 1; } else { return mid; } } return -1; }
test.c
#define _CRT_SECURE_NO_WARNINGS 1 #include "SeqList.h" void test1() { SeqList list; InitSeqList(&list); PushBack(&list, 1); PushBack(&list, 2); PushBack(&list, 3); PushBack(&list, 4); Display(&list); PopBack(&list); Display(&list); PopBack(&list); Display(&list); PopBack(&list); Display(&list); PopBack(&list); Display(&list); } void test2() { int pos = 0; SeqList list; InitSeqList(&list); PushFront(&list, 1); PushFront(&list, 2); PushFront(&list, 3); PushFront(&list, 2); PushFront(&list, 4); PushFront(&list, 2); Display(&list); pos = Find(&list, 3); printf("%d\n", list.data[pos]); PopFront(&list); Display(&list); PopFront(&list); Display(&list); PopFront(&list); Display(&list); PopFront(&list); Display(&list); } void test3() { int pos = 0; SeqList list; InitSeqList(&list); PushFront(&list, 1); PushFront(&list, 2); PushFront(&list, 3); PushFront(&list, 2); PushFront(&list, 4); PushFront(&list, 2); Display(&list); Insert(&list, 6, 2); Display(&list); Remove(&list, 1); Display(&list); RemoveAll(&list, 2); Display(&list); } void test4() { int pos = 0; SeqList list; InitSeqList(&list); PushFront(&list, 1); PushFront(&list, 2); PushFront(&list, 3); PushFront(&list, 2); PushFront(&list, 4); Display(&list); Reverse(&list); Display(&list); Sort(&list); Display(&list); pos = BinarySearch(&list, 3); printf("%d\n", list.data[pos]); } int main() { test4(); system("pause"); return 0; }
动态顺序表的实现://www.jb51.net/article/120876.htm
以上使用C语言动态顺序表的实现如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
上一篇:C++数据精度问题的解决方案(对浮点数保存指定位小数)
栏 目:C语言
本文标题:C语言实现静态顺序表的实例详解
本文地址:https://www.xiuzhanwang.com/a1/Cyuyan/1279.html
您可能感兴趣的文章
- 04-02c语言函数调用后清空内存 c语言调用函数删除字符
- 04-02c语言的正则匹配函数 c语言正则表达式函数库
- 04-02func函数+在C语言 func函数在c语言中
- 04-02c语言中对数函数的表达式 c语言中对数怎么表达
- 04-02c语言用函数写分段 用c语言表示分段函数
- 04-02c语言编写函数冒泡排序 c语言冒泡排序法函数
- 04-02c语言没有round函数 round c语言
- 04-02c语言分段函数怎么求 用c语言求分段函数
- 04-02C语言中怎么打出三角函数 c语言中怎么打出三角函数的值
- 04-02c语言调用函数求fibo C语言调用函数求阶乘
阅读排行
本栏相关
- 04-02c语言函数调用后清空内存 c语言调用
- 04-02func函数+在C语言 func函数在c语言中
- 04-02c语言的正则匹配函数 c语言正则表达
- 04-02c语言用函数写分段 用c语言表示分段
- 04-02c语言中对数函数的表达式 c语言中对
- 04-02c语言编写函数冒泡排序 c语言冒泡排
- 04-02c语言没有round函数 round c语言
- 04-02c语言分段函数怎么求 用c语言求分段
- 04-02C语言中怎么打出三角函数 c语言中怎
- 04-02c语言调用函数求fibo C语言调用函数求
随机阅读
- 01-10使用C语言求解扑克牌的顺子及n个骰子
- 01-11ajax实现页面的局部加载
- 04-02jquery与jsp,用jquery
- 01-10SublimeText编译C开发环境设置
- 08-05dedecms(织梦)副栏目数量限制代码修改
- 01-10C#中split用法实例总结
- 01-10delphi制作wav文件的方法
- 08-05织梦dedecms什么时候用栏目交叉功能?
- 01-11Mac OSX 打开原生自带读写NTFS功能(图文
- 08-05DEDE织梦data目录下的sessions文件夹有什