数据结构线性表的顺序存储实现(C语言)
2020-06-29 05:00
204 查看
这是我根据上课,作业要求填写的代码整理而来的。
代码涵盖了与线性表基本算法:初始化、遍历、元素插入,取数据、定位、删除以及注销。
#include <iostream> #include <stdio.h> #include <stdlib.h> #define ok 1 #define error 0 #define listinitsize 50 #define listincrement 10 typedef int status; typedef char elemtype; typedef struct{ elemtype *elem; int length; int listsize; }sqlist; status initlist(sqlist &L) { // 0 初始化 L.elem = (elemtype * )malloc(listinitsize*sizeof(elemtype)); L.length = 0; L.listsize = listinitsize; return ok; } status listinsert(sqlist &L, int i, elemtype e) { // 1 插入 int j; if(i<1&&i>L.length+1) return error; for(j=L.length;j>=i;j--){ L.elem[j]=L.elem[j-1]; } L.elem[i-1]=e; L.length++; return ok; } status listtraverse(sqlist L) { // 2 遍历 printf("The length of the sqlist: %d\n",L.length); printf("The data of the sqlist: "); for(int i=0;i<L.length;i++) printf("%c ",L.elem[i]); printf("\n"); return ok; } status getelem(sqlist L,int i, elemtype &e) { // 3 取数据 if(i<1||i>L.length) return error; e=L.elem[i-1]; return ok; } int locateelem(sqlist L,elemtype e) { // 4 定位 int i; for(i=0;i<L.length;i++) if(L.elem[i]==e) return i+1; } status listdelete(sqlist &L,int i,elemtype &e) { // 5 删除 if(i<1||i>L.length) return error; elemtype *q; e=L.elem[i-1]; for(q=&L.elem[i-1];q<=&L.elem[L.length-1];q++) *q=*(q+1); L.length--; return ok; } status destroylist(sqlist &L) { // 6 注销 delete []L.elem; L.length=0; L.listsize=0; return ok; } int main() { // 线性表的顺序存储实现 elemtype e; sqlist L; int TestCase; scanf("%d", &TestCase); printf("(1)初始化线性表L:\n"); initlist(L); printf("(2)依次在线性表L尾插入字符a,b,c,d,e:\n"); listinsert(L,1,'a'); listinsert(L,2,'b'); listinsert(L,3,'c'); listinsert(L,4,'d'); listinsert(L,5,'e'); printf("(3)在线性表L的第2个位置之前插入字符f:\n"); listinsert(L,2,'f'); switch( TestCase ){ case 1: // 遍历 printf("(4)输出线性表L:\n"); listtraverse(L); break; case 2: // 取元素 printf("(5)输出线性表L的第3个元素:"); getelem(L,3,e); printf("%c\n",e); break; case 3: // 定位 printf("(6)输出元素d在线性表L的位置:%d\n",locateelem(L,'d')); break; case 4: // 删除 printf("(7)删除线性表L的第4个元素:\n"); listdelete(L,4,e); printf("(8)输出线性表L:\n"); listtraverse(L); break; default: // 综合 (遍历、取数、定位、删除、遍历) printf("(4)输出线性表L:\n"); listtraverse(L); printf("(5)输出线性表L的第3个元素:"); getelem(L,3,e); printf("%c\n",e); printf("(6)输出元素d在线性表L的位置:%d\n",locateelem(L,'d')); printf("(7)删除线性表L的第4个元素:\n"); listdelete(L,4,e); printf("(8)输出线性表L:\n"); listtraverse(L); } printf("(9)释放线性表L。\n"); destroylist(L); return ok; }
这是代码运行结果:
最后,小生不才,感谢浏览。
相关文章推荐
- 数据结构(严蔚敏)C语言实现---线性表的顺序存储
- 数据结构之---C语言实现二叉树的顺序存储
- 数据结构2----线性表顺序存储和链式存储的实现(霜之小刀)
- 【数据结构作业一】写出顺序表的结构体类型定义及查找、插入、删除算法,并以顺序表作存储结构,实现线性表的插入、删除
- 数据结构之线性表顺序存储实现-顺序表(C++)
- 数据结构之---C语言实现串的顺序存储
- 数据结构之线性表代码实现顺序存储,链式存储,静态链表(选自大话数据结构)
- 数据结构(C语言实现)-线性表(1)(线性表的顺序表示)
- 数据结构复习——线性表的顺序存储实现
- 【数据结构之线性表顺序存储】简单的数组的方式实现
- 已知线性表中的元素以值递增顺序排列,并以双向链表为存储结构。试实现以下功能:删除表中所有[MinNum, MaxNum]范围内的数据。
- 数据结构之线性表的顺序存储实现(1)
- 数据结构之---c语言实现栈的顺序存储
- C语言实现数据结构顺序存储
- C语言 数据结构 线性表的顺序表示
- 线性表(顺序存储)结构与功能的简易实现
- 线性结构--->栈的顺序存储实现
- 利用线性表的顺序结构求集合的并、交、差、补(C语言实现)
- [C语言][数据结构]基础的线性表的顺序表示和实现
- 数据结构——简单线性表(顺序存储)