- /*
- 简单的数组表
- SUDDEV
- my.oschina.net/SUDDEV
- */
- #include<iostream>
- #include<cstdlib>
- using namespace std;
- #define OK 1
- #define ERROR 0
- #define OVERFLOW -2
- #define MAXSIZE 20 //数组长度定义
- typedef int Status;
- typedef int ElemType; //根据情况定义数据的类型
- typedef struct
- {
- ElemType *elem;
- int length;
- }Sqlist;
- //语法规范定义
- Status InitList(Sqlist &L) //初始化
- {
- L.elem = new ElemType[MAXSIZE];
- if(!L.elem) return ERROR;
- L.length=0;
- return OK;
- }
- Status GetElem(Sqlist L,int i,ElemType &e) //获取i位置(不是下标)的元素
- {
- if(i<1||i>L.length) return ERROR;
- e=L.elem[i-1];
- return OK;
- }
- int LocateElem(Sqlist L,ElemType e)
- {
- for(int i=0;i<L.length;i++)
- {
- if(L.elem[i]==e) return i+1;
- }
- return ERROR;
- }
- Status ListInsert(Sqlist &L,int i,ElemType e) //在i位置插入
- {
- if(i<1||i>L.length) 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;
- }
- Status SortListInsert(Sqlist &L,ElemType e) //插入后不打乱之前排的顺序
- {
- if(L.length==MAXSIZE) return ERROR;
- if(e<=L.elem[0])
- {
- ListInsert(L,1,e);
- return OK;
- }
- if(e>=L.elem[L.length-1])
- {
- L.length++; //先增加长度才能插入
- ListInsert(L,L.length,e);
- L.length--;
- return OK; //Insert函数会++一次所以这里--
- }
- for(int i=L.length-1;i>=0;i--)
- {
- if(e<=L.elem[i]&&e>=L.elem[i-1])
- {
- ListInsert(L,i+1,e);
- return OK;
- }
- }
- }
- Status ListDelete(Sqlist &L,int i) //删除i位置的数据
- {
- if(i<1||i>L.length) return ERROR;
- for(int j=i;j<=L.length-1;j++)
- {
- L.elem[j-1]=L.elem[j];
- }
- L.length--;
- return OK;
- }
- Status ListInput(Sqlist &L) //数据录入
- {
- if(L.length==-1) return ERROR;
- cout<<"Length="<<L.length;
- cout<<"请输入录入数据数量:";
- int i=L.length;
- int n;
- cin>>n;
- L.length+=n;
- ElemType e;
- cout<<"Length="<<L.length;
- for(;i<L.length;i++)
- {
- cout<<"请输入第"<<i+1<<"个数据:";
- cin>>e;
- ListInsert(L,i+1,e);
- --L.length; //每次插入都会L.length++,所以此处--防止死循环
- }
- return OK;
- }
- Status ListSort(Sqlist &L) //排序,冒泡算法
- {
- if(L.length==-1) return ERROR;
- ElemType t;
- for(int i=0;i<L.length-1;i++)
- for(int j=0;j<L.length-i-1;j++)
- {
- if(L.elem[j]>L.elem[j+1])
- {t=L.elem[j+1];L.elem[j+1]=L.elem[j];L.elem[j]=t;}
- }
- return OK;
- }
- void ListPrint(Sqlist &L) //输出表内数据
- {
- ElemType e;
- for(int i=0;i<L.length;i++)
- {
- GetElem(L,i+1,e);
- cout<<e<<" ";
- }
- cout<<endl;
- }
- int main()
- {
- int Choice=-1,Ifsort=-1;
- Sqlist L;L.length=-1;
- while(1)
- {
- cout<<"+------------------------------------------------------------------------------+"<<endl;
- cout<<"+1.初始化 2.录入 3.显示 4.插入 5.删除 6.查找 7.排序 8.有序插入 0.退出+"<<endl;
- cout<<"+------------------------------------------------------------------------------+"<<endl;
- cout<<"请输入选择:";
- cin>>Choice;
- switch(Choice)
- {
- case 0:
- cout<<"谢谢使用"<<endl;
- system("pause");
- return 0;break;
- case 1:
- if(InitList(L)==ERROR)
- {
- cout<<"初始化失败!"<<endl;
- }
- else
- {
- cout<<"初始化成功!"<<endl;
- }
- break;
- case 2:
- if(ListInput(L)==ERROR)
- cout<<"请先初始化!"<<endl;
- else
- cout<<"数据录入完毕!"<<endl;
- break;
- case 3:
- if(L.length==-1)
- cout<<"请先录入!"<<endl;
- else
- {
- ListPrint(L);
- }
- break;
- case 4:
- if(L.length==-1)
- cout<<"请先录入!"<<endl;
- else
- {
- ListPrint(L);
- int InsertLocate;
- cout<<"请输入插入位置:";
- cin>>InsertLocate;
- cout<<"请输入插入内容:";
- ElemType Insert;
- cin>>Insert;
- if(ListInsert(L,InsertLocate,Insert)==ERROR)
- cout<<"插入失败"<<endl;
- else
- {
- cout<<"数据插入成功"<<endl;
- ListPrint(L);
- }
- }
- break;
- case 5:
- if(L.length==-1)
- cout<<"请先录入!"<<endl;
- else
- {
- int DeleteLocate;
- ListPrint(L);
- char choice2;
- cout<<"请输入删除位置:";
- cin>>DeleteLocate;
- cout<<"该位置数据为:"<<L.elem[DeleteLocate-1]<<endl<<"确认删除?Y/N :";
- cin>>choice2;
- if(choice2=='Y')
- {
- if(ListDelete(L,DeleteLocate)==ERROR)
- cout<<"删除失败"<<endl;
- else
- {
- cout<<"删除成功"<<endl;
- ListPrint(L);
- }
- }
- else
- cout<<"删除取消!"<<endl;
- }
- break;
- case 6:
- if(L.length==-1)
- cout<<"请先录入!"<<endl;
- else
- {
- cout<<"请输入查找的数据:";
- int Search;
- cin>>Search;
- int Result=LocateElem(L,Search);
- if(Result==ERROR)
- cout<<"未找到该数据!"<<endl;
- else
- cout<<"该数据位置为:"<<Result<<endl;
- }
- break;
- case 7:
- if(ListSort(L)==ERROR)
- cout<<"请先录入!"<<endl;
- else
- {
- cout<<"排序成功!"<<endl;
- Ifsort=1;
- ListPrint(L);
- }
- break;
- case 8:
- if(L.length==-1)
- cout<<"请先录入!"<<endl;
- else
- if(Ifsort==-1)
- cout<<"表未排序!"<<endl;
- else
- {
- ElemType SortInsert;
- ListPrint(L);
- cout<<"请输入需要插入的数据:";
- cin>>SortInsert;
- if(SortListInsert(L,SortInsert)==ERROR)
- {
- cout<<"插入失败!"<<endl;
- }
- else
- {
- cout<<"插入成功!"<<endl;
- ListPrint(L);
- }
- }
- break;
- default:cout<<"输入指令有误!"<<endl;
- }
- system("pause");
- system("cls");
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/0508201614899.html
来源: http://www.codesnippet.cn/detail/0508201614899.html