数据结构之线性表的顺序存储
2015-04-22 21:55
232 查看
为了复习下数据结构,最近开始着手捡捡原来学过的数据结构,从最简单的顺序存储的线性表开始...
代码很简单,对于自己来说起到一个督促作用
个人总结:数据结构实现时的大体框架都是一样的,自己可以整理出一个自己的框架来,以后每次写程序时就可以和填空一样来填写代码,这样很节省自己的时间
代码很简单,对于自己来说起到一个督促作用
#include<stdio.h> #include<string.h> #define MAXSIZE 100 #define OK 1 #define ERROR 0 typedef struct { char elem[MAXSIZE]; int last; }SeqList; void InitList(SeqList *L) { L->last = -1; printf("init is ok\n"); } void DestroyList(SeqList *L) { L->last = -1; } void ClearList(SeqList *L) { L->last = -1; } int EmptyList(SeqList *L) { if(L->last == -1) return ERROR; else return OK; } int ListLength(SeqList *L) { return L->last; } char GetData(SeqList *L,int i) { if(i < 0||i > L->last) return ERROR; else return L->elem[i]; } int InsList(SeqList *L,int i,char data) { int j; if(i < 0||i>L->last) return ERROR; else { for(j=ListLength(L)+1;j > i;j--) L->elem[j] = L->elem[j - 1]; L->elem[j] = data; L->last++; return OK; } } int DelList(SeqList *L,int i) { int j; if(i < 0||i>L->last) return ERROR; else { for(j = i;j < ListLength(L);j++) L->elem[j] = L->elem[j + 1]; L->last-=1; return OK; } } void CreList(SeqList *L) { int j = 0; char ch; for(j = 0;j < MAXSIZE; j++) { scanf("%c",&ch); if(ch == '#') break; else L->elem[j] = ch; } L->last = j-1; printf("the length = %d\n",j-1); } void PrintList(SeqList *L) { int length; int i; length = ListLength(L); if(length <= 0) printf("there is on List!!!!\n"); else { for(i = 0;i <= length;i++) { printf("%c",L->elem[i]); } printf("\n"); //printf("\n"); } } int main() { SeqList List; int choice; int i; char e; printf("=============线性表练习==============\n"); printf("选项:\n"); printf("1初始化\n"); printf("2销毁线性表\n"); printf("3清除线性表\n"); printf("4判断线性表是否为空\n"); printf("5求线性表长度\n"); printf("6得到某个位置的数据\n"); printf("7在某个位置插入数据\n"); printf("8删除某个位置的数据\n"); printf("9创建线性表\n"); printf("10打印当前线性表的所有数据\n"); printf("输入你的选项:\n"); scanf("%d",&choice); while(choice) { switch(choice) { case 1: InitList(&List); break; case 2: DestroyList(&List); break; case 3: ClearList(&List); break; case 4: if(EmptyList(&List) == OK) printf("no null\n"); if(EmptyList(&List) == ERROR) printf("NULL\n"); break; case 5: printf("length = %d\n",ListLength(&List)); break; case 6: printf("input the number of the data:\n"); scanf("%d",&i); e = GetData(&List,i); printf("data = %c\n",e); break; case 7: printf("input the number and the data:\n"); scanf("%d%c",&i,&e); if(InsList(&List,i,e) == OK) printf("insert ok\n"); else printf("insert failed\n"); break; case 8: scanf("%d",&i); if(DelList(&List,i) == OK) printf("delete ok\n"); else printf("delete failed\n"); break; case 9: printf("input your data:\n"); CreList(&List); break; case 10: PrintList(&List); break; } scanf("%d",&choice); } return 0; }
个人总结:数据结构实现时的大体框架都是一样的,自己可以整理出一个自己的框架来,以后每次写程序时就可以和填空一样来填写代码,这样很节省自己的时间
相关文章推荐
- #1:数据结构:线性表 - 顺序存储类型;
- 线性表-顺序存储(Java数据结构)
- 数据结构 线性表的顺序存储
- 数据结构-线性表的顺序存储
- 数据结构:线性表的顺序存储--Java实现
- 数据结构学习笔记-线性表顺序存储(C语言实现)
- 【数据结构】对于线性表的顺序和链接两种存储方式的主要优缺点
- [SDUT](1130)数据结构上机测试1:顺序表的应用 ---顺序存储(线性表)
- 【数据结构】线性表的顺序存储完整程序
- 数据结构系列-线性表的顺序存储及基本操作
- 数据结构之线性表代码实现顺序存储,链式存储,静态链表(选自大话数据结构)
- java实现数据结构中线性表的顺序存储
- C/C++ 数据结构---线性顺序存储数据:查询快,增删慢
- 线性表的顺序存储实现(陈越数据结构版)
- 跟我学数据结构--线性表的顺序存储
- 数据结构:线性表的顺序存储
- 数据结构例程——线性表顺序存储的应用
- 数据结构-线性表的顺序存储实现及各种操作代码
- 顺序存储线性表的C++实现——严蔚敏版《数据结构》
- 数据结构-----线性表的顺序存储