数据结构(1):线性表的顺序表示和实现
2016-03-15 20:42
676 查看
// 线性表的顺序表示和实现 #include <iostream> #define OK 1 #define ERROR 0 #define OVERFLOW -2 #define MAXSIZE 100 // Status是函数返回值类型,其值是函数结果状态代码 typedef int Status; // ElemType是自定义数据类型 typedef int ElemType; // 循环变量 int i,j; using namespace std; //顺序表的存储结构 typedef struct { ElemType *elem; // 存储空间的基地址 int length; // 当前长度 }SqList; // 顺序表的结构类型为SqList // 顺序表的初始化 Status InitList(SqList &L) { // 构造一个空的顺序表L // 为顺序表分配一个大小为MAXSIZE的数组空间 L.elem = new ElemType[MAXSIZE]; if(!L.elem) exit(OVERFLOW); // 存储分配失败退出 L.length = 9; return OK; } // 顺序表的取值 Status GetElem(SqList L,int i,ElemType &e) { // 判断i值是否合理,若不合理,返回ERROR if(i<1 || i>L.length) return ERROR; e = L.elem[i-1]; // L.elem[i-1]存储第i个数据元素 return OK; } // 顺序表的查找 int LocateElem(SqList L,ElemType e) { // 在顺序表L中查找值为e的数据元素,返回其序号 for(i=0; i < L.length; ++i) if(L.elem[i] == e) return i+1; // 查找成功,返回序号i+1 return ERROR; // 查找失败,返回0 } // 顺序表的插入 Status ListInsert(SqList &L,int i,ElemType e) { // 在顺序表L中第i个位置之前插入新的元素e,i值的合法范围是 // 1<=i<=L.length+1 if(i<1 || i>L.length+1) return ERROR; // i值不合法 if(L.length == MAXSIZE) return ERROR; // 当前存储空间已满 for(j=L.length; j >= i-1; --j) L.elem[j+1] = L.elem[j]; // 插入位置及之后的元素后移 L.elem[i-1] = e; // 将新的元素e放入第i个位置 ++L.length; // 表长加1 return OK; } // 顺序表的删除 Status ListDelete(SqList &L,int i) { // 在顺序表L中删除第i个元素,i值的合法范围是1<=i<=L.length if(i<1 || i>L.length) return ERROR; // i值不合法 for(j=i; j <= L.length-1; --j) L.elem[j-1] = L.elem[j]; // 被删除元素之后的元素前移 --L.length; // 表长减1 return OK; } int main() { /***************************************************/ }
优化版:
#include <iostream> using namespace std; #define OK 1 #define ERROR 0 #define OVERFLOW -2 #define MAXSIZE 100 typedef int Status; typedef int ElemType; typedef struct { ElemType *elem; int length; }SqList; // 1、初始化 Status InitList(SqList &L) { L.elem = new ElemType[MAXSIZE]; if(!L.elem) exit(OVERFLOW); L.length = 0; return OK; } // 2、取值 Status GetElem(SqList L, int i, ElemType &e) { if(i-1<0 || i>L.length) return ERROR; e = L.elem[i-1]; return OK; } // 3、查找 int LocateElem(SqList L, ElemType e) { for(int i=0; i < L.length; ++i) if(L.elem[i] == e) return i+1; return ERROR; } // 4、插入 Status ListInsert(SqList &L, int i, ElemType e) { if(i-1<0 || i>L.length+1) return ERROR; if(L.length == MAXSIZE) return ERROR; for(int j=L.length-1; j >= i-1; --j) L.elem[j+1] = L.elem[j]; L.elem[i-1] = e; ++L.length; return OK; } // 5、删除 Status ListDelete(SqList &L, int i) { if(i-1<0 || i>L.length) return ERROR; for(int j=i-1; j < L.length-1; ++j) L.elem[j] = L.elem[j+1]; --L.length; return OK; } int main() { cout<<"************************************"<<endl; cout<<"* 1、初始化 *"<<endl; cout<<"* 2、取值 *"<<endl; cout<<"* 3、查找 *"<<endl; cout<<"* 4、插入 *"<<endl; cout<<"* 5、删除 *"<<endl; cout<<"* 6、打印顺序表 *"<<endl; cout<<"* 7、退出 *"<<endl; cout<<"************************************"<<endl; SqList L; ElemType e; int n,m,i,choice; while(1) { cout<<"请输入你的选择: "; cin>>choice; switch(choice) { case 1: if(InitList(L)) cout<<"初始化成功!"<<endl<<endl; else cout<<"初始化失败!"<<endl<<endl; continue; case 2: cout<<"请输入你要取的值的序号: "; cin>>n; if(GetElem(L,n,e)) cout<<"取值成功!"<<endl<<"得到的值为: "<<e<<endl<<endl; else cout<<"取值失败!你输入的序号有误,请重新选择。"<<endl<<endl; continue; case 3: cout<<"请输入要查找的数: "; cin>>e; if(LocateElem(L,e)) cout<<"数 "<<e<<" 在第 "<<LocateElem(L,e)<<" 位."<<endl<<endl; else cout<<"数 "<<e<<" 查找失败!请确认你输入的数字存在于表中. 请重新操作。"<<endl<<endl; continue; case 4: cout<<"请输入要插入的数的个数( <"<<MAXSIZE-L.length<<" ): "; ; cin>>n; if(n > MAXSIZE-L.length) { cout<<"输入异常,请重新操作."<<endl<<endl; continue; } for(i=0; i<n;++i) { cout<<endl<<"请输入要插入的位置: "; cin>>m; cout<<"请输入要插入的数字: "; cin>>e; if(ListInsert(L,m,e)) cout<<"数 "<<e<<" 插入成功!"<<endl<<endl; else { cout<<"数字 "<<e<<" 插入失败!请重新输入."<<endl<<endl; i++; } } continue; case 5: cout<<"请输入要删除的数的位置: "; cin>>m; if(ListDelete(L,m)) cout<<" 删除成功!"<<endl<<endl; else cout<<"请重新操作."<<endl<<endl; continue; case 6: for(i=0;i<L.length;++i) { cout<<L.elem[i]<<" "; } cout<<endl; continue; case 7: cout<<"正在退出..."<<endl; for(i=0;i<99999999;++i){} cout<<"程序已经结束.欢迎下次再使用0.0"<<endl; break; default: for(i=0;i<99999999;++i){} cout<<"程序异常退出! "<<endl; break; } break; } return 0; }
相关文章推荐
- C#数据结构之顺序表(SeqList)实例详解
- Lua教程(七):数据结构详解
- 解析从源码分析常见的基于Array的数据结构动态扩容机制的详解
- C#数据结构之队列(Quene)实例详解
- C#数据结构揭秘一
- C#数据结构之单链表(LinkList)实例详解
- 数据结构之Treap详解
- 用C语言举例讲解数据结构中的算法复杂度结与顺序表
- C#数据结构之堆栈(Stack)实例详解
- C#数据结构之双向链表(DbLinkList)实例详解
- JavaScript数据结构和算法之图和图算法
- Java数据结构及算法实例:冒泡排序 Bubble Sort
- Java数据结构及算法实例:插入排序 Insertion Sort
- Java数据结构及算法实例:考拉兹猜想 Collatz Conjecture
- java数据结构之java实现栈
- java数据结构之实现双向链表的示例
- Java数据结构及算法实例:选择排序 Selection Sort
- Java数据结构及算法实例:朴素字符匹配 Brute Force
- Java数据结构及算法实例:汉诺塔问题 Hanoi
- Java数据结构及算法实例:快速计算二进制数中1的个数(Fast Bit Counting)