Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- const int MAXSIZE=100;
- typedef struct{
- int *data; //顺序表的基地址
- int length; //顺序表的长度
- int size; //顺序表的空间大小
- }SqList;
- //初始化
- bool InitList(SqList &L){
- L.data=new int[MAXSIZE]; //为顺序表分配MAXSIZE个空间
- if(!L.data){
- return false;
- }
- L.length=0;
- L.size=MAXSIZE;
- return true;
- }
- //追加新元素
- bool ListAdd(SqList &L,int e){
- if(L.length==MAXSIZE){
- return false;
- }
- L.data[L.length]=e;
- L.length++;
- return true;
- }
- //插入(位置插入)
- bool ListInsert(SqList &L,int i,int e){
- if(L.length==MAXSIZE){ //表满了
- return false;
- }
- if(i<1||i>=L.length){ //i不在范围内
- return false;
- }
- if(i<=L.length){
- for(int j=L.length;j>=i-1;j--){
- L.data[j+1]=L.data[j];
- }
- }
- L.data[i-1]=e;
- L.length++;
- return true;
- }
- //删除(位置删除)
- bool ListDelete(SqList &L,int i){
- int e=L.data[i-1];
- cout<<"删除的元素为:"<<e<<endl;
- if(i<0||i>=L.length){
- return false;
- }
- if(i==L.length-1){
- L.length--;
- return true;
- }
- for(int j=i;j<L.length;j++){
- L.data[j-1]=L.data[j];
- }
- L.length--;
- return true;
- }
- //删除(值删除)
- bool ListDeleteValue(SqList &L,int v){
- for(int i=0;i<L.length;i++){
- if(L.data[i]==v){
- for(int j=i;j<L.length-1;j++){
- L.data[j]=L.data[j+1];
- }
- L.length--;
- }
- }
- return true;
- }
- //逆转
- bool ListReverse(SqList &L){
- if(L.length==0){
- return false;
- }
- for(int i=0,j=L.length-1;i<L.length/2;i++,j--){
- swap(L.data[i],L.data[j]);
- }
- return true;
- }
- //输出
- bool ListOutput(SqList &L){
- if(L.length==0){
- cout<<"当前表为空表"<<endl;
- return false;
- }
- cout<<"当前的顺序表为:";
- for(int i=0;i<L.length;i++){
- cout<<L.data[i]<<" ";
- }
- cout<<endl;
- return true;
- }
- //销毁
- bool ListDestory(SqList &L){
- if(L.data){
- delete[]L.data;
- }
- L.length=0;
- L.size=0;
- cout<<"顺序表已销毁"<<endl;
- }
- //置空表
- bool ListEmpty(SqList &L){
- for(int i=0;i<MAXSIZE;i++){
- L.data[i]=0;
- }
- L.length=0;
- return true;
- }
- //求表长
- bool ListLength(SqList &L){
- cout<<"当前表长为:"<<L.length<<endl;
- return true;
- }
- //查找元素
- bool ListSearch(SqList &L,int x){
- int flag=0;
- if(L.length==0){
- return false;
- }
- for(int i=0;i<L.length;i++){
- if(L.data[i]==x){
- cout<<"此元素在顺序表的第"<<i+1<<"位"<<endl;
- flag=1;
- break;
- }
- }
- if(flag==0){
- cout<<"未能找到此元素"<<endl;
- }
- return true;
- }
- //判断顺序表是否为空
- bool ListIsEmpty(SqList &L){
- if(L.length==0){
- cout<<"顺序表为空表"<<endl;
- }else{
- cout<<"顺序表不为空表"<<endl;
- }
- return true;
- }
- //实现顺序表元素的逆转
- bool ListReverseElem(SqList &L,int i,int j){
- if(L.length==0){
- return false;
- }
- swap(L.data[i],L.data[j]);
- return true;
- }
- int main(){
- SqList List;
- cout<<"1.创建空表"<<endl;
- cout<<"2.追加新元素"<<endl;
- cout<<"3.插入新元素(在第i位插入)"<<endl;
- cout<<"4.删除元素(位置删除)"<<endl;
- cout<<"5.删除元素(值删除)"<<endl;
- cout<<"6.逆转"<<endl;
- cout<<"7.销毁"<<endl;
- cout<<"8.置空表"<<endl;
- cout<<"9.求表长"<<endl;
- cout<<"10.查找元素"<<endl;
- cout<<"11.判断顺序表是否为空"<<endl;
- cout<<"12.实现顺序表元素逆转"<<endl;
- cout<<"13.退出"<<endl;
- cout<<"请输入将要进行的操作编号:"<<endl;
- int option;
- cin>>option;
- while(option!=13){
- switch(option){
- case 1:
- InitList(List);
- ListOutput(List);
- break;
- case 2:
- int add;
- cin>>add;
- ListAdd(List,add);
- ListOutput(List);
- break;
- case 3:
- int pi,n;
- cout<<"输入你要插入的值位置和数值:";
- cin>>pi>>n;
- ListInsert(List,pi,n);
- ListOutput(List);
- break;
- case 4:
- int pd;
- cout<<"输入你要删除的元素位置:";
- cin>>pd;
- ListDelete(List,pd);
- ListOutput(List);
- break;
- case 5:
- int va;
- cout<<"输入你要删除的元素值:";
- cin>>va;
- ListDeleteValue(List,va);
- ListOutput(List);
- break;
- case 6:
- ListReverse(List);
- ListOutput(List);
- break;
- case 7:
- ListDestory(List);
- ListOutput(List);
- break;
- case 8:
- ListEmpty(List);
- ListOutput(List);
- break;
- case 9:
- ListOutput(List);
- ListLength(List);
- break;
- case 10:
- int u;
- cout<<"输入你要找的元素:";
- cin>>u;
- ListOutput(List);
- ListSearch(List,u);
- break;
- case 11:
- ListOutput(List);
- ListIsEmpty(List);
- break;
- case 12:
- int p1,p2;
- cout<<"请输入你要逆转的两个元素位置:";
- cin>>p1>>p2;
- ListReverseElem(List,p1-1,p2-1);
- cout<<"逆转后的顺序为:";
- ListOutput(List);
- break;
- }
- cin>>option;
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment