建民哥哥的AV
1 #include<bits/stdc++.h> 2 #include<vector> 3 #include<algorithm> 4 using namespace std; 5 #define MAXSIZE 100 6 typedef struct{ 7 int *elem; //指针 8 int length; //顺序表长度 9 }SqList; //图书表中的顺序储存结构SqList 10 11 12 void InitList(SqList &L)//顺序表的初始化 13 { 14 L.elem=new int[MAXSIZE]; //为链表分配一个大小为MAXSIZE的数组空间 15 if(!L.elem) exit(OVERFLOW); //存储分配失败退出 16 L.length=0; //空表长度为0; 17 18 } 19 void CreatList(SqList &L) 20 { 21 cout<<"请输入顺序表长度"<<endl; 22 int num; 23 cin>>num; 24 cout<<"请传入数值"<<endl; 25 for(int i=0;i<num;i++) 26 { 27 28 cin>>L.elem[i]; 29 L.length++; 30 } 31 } 32 int ShowList(SqList &L) 33 { 34 cout<<"目前顺序表为:"<<endl; 35 if (L.length == 0) 36 { 37 return 0; 38 } 39 for(int i=0;i<L.length;i++) 40 { 41 cout<<L.elem[i]<<"\t"; 42 } 43 cout<<endl; 44 } 45 bool GetElem(SqList L)//顺序表的取值 46 { 47 48 int p; 49 int e; 50 cout<<"请输入要查找的位置"<<endl; 51 cin>>p; 52 if(p<1||p>L.length) return false ; 53 e=L.elem[p-1]; 54 cout<<"您要查找的值是:"<<e<<endl; 55 } 56 57 int LocateElem(SqList L)//顺序表的查找 58 { 59 int e; 60 cout<<"请输入要查找的值"<<endl; 61 cin>>e; 62 for(int i=0;i<L.length;i++) 63 if(L.elem[i]==e) 64 cout<<"查询成功,查询的位置在"<<i+1<<endl; 65 return 0; 66 } 67 68 bool ListInsert(SqList &L)//在第i个位置插入新元素e,i的取值合法范围是1<=i<=L.le==length+1 69 { 70 int i; 71 cout<<"请输入要在第几个位置插入数字"<<endl; 72 cin>>i; 73 int e; 74 cout<<"请输入要插入的数字"<<endl; 75 cin>>e; 76 if((i<1)||(i>L.length+1))return false; 77 if(L.length==MAXSIZE)return false; 78 for(int j=L.length-1;j>=i-1;j--) 79 L.elem[j+1]=L.elem[j]; 80 L.elem[i-1]=e; 81 ++L.length; 82 return true; 83 84 } 85 bool ListDelete(SqList &L) 86 { 87 int i; 88 cout<<"请输入要在第几个位置删除数字"<<endl; 89 cin>>i; 90 if((i<1)||(i>L.length))return false; 91 if(L.length==MAXSIZE)return false; 92 for(int j=i;j<=L.length-1;j++) 93 L.elem[j-1]=L.elem[j]; 94 --L.length; 95 return true; 96 97 98 } 99 int main(){ 100 SqList L; 101 InitList(L);//顺序表的初始化 102 CreatList(L);//赋值 103 ShowList(L);//打印 104 GetElem(L);//顺序表的取值 105 ShowList(L);//打印 106 LocateElem(L);//顺序表的查找 107 ShowList(L);//打印 108 ListInsert(L);//顺序表的插入 109 ShowList(L);//打印 110 ListDelete(L);//顺序表的删除 111 ShowList(L);//打印 112 return 0; 113 }
这是我学的顺序表,牛逼1

浙公网安备 33010602011771号