线性表基本操作
#include<bits/stdc++.h>
#define rep(i, aa, bb) for(int i = aa; i <= bb; i ++)
#define per(i, aaa, bbb) for(int i = aaa; i >= bbb; i --)
#define long long int
const int maxn = 1e6 + 10;
using namespace std;
int n;
typedef struct {
int *arr;
int length;
}SqList;
int InitList_Sq(SqList * l)
{
printf("请输入你要构造数组的长度:");
scanf("%d", &n);
l->arr = (int * ) malloc (maxn * sizeof(int));
if(!l->arr){
printf("数组开辟失败/n");
return -1;
}
l->length = n;
return 1;
}
void ScannerList_Sq(SqList * l)
{
printf("数组长度为%d,请输入数组元素:", n);
rep(i, 1, n){
cin >> l -> arr[i];
}
}
void QuickSort_Sq(int l, int r, SqList * lis)
{
if(l >= r) return;
int i = l - 1, j = r + 1;
int x = lis -> arr[l + r >> 1];
while(i < j){
do i ++; while(lis -> arr[i] < x);
do j --; while(lis -> arr[j] > x);
if(i < j) swap(lis -> arr[i], lis -> arr[j]);
}
QuickSort_Sq(l, j, lis);
QuickSort_Sq(j + 1, r, lis);
}
int FindList_Sq(SqList * l)
{
printf("请输入要查询的元素的位置:");
int pos;
cin >> pos;
if(pos < 1 || pos > l -> length){
cout << "元素不存在";
return -1;
} else{
cout << "该元素为:" << l -> arr[pos] << endl;
}
return 1;
}
int Delete_Sq(SqList * l)
{
printf("请输入要删除的元素:");
int tmp;
cin >> tmp;
bool f1 = false;
rep(i, 1, l -> length){
if(tmp == l -> arr[i]){
f1 = true;
rep(j, i, l -> length - 1){
l -> arr[j] = l -> arr[j + 1];
}
l -> length --;
i --;
}
}
if(!f1){
return -1;
} else {
rep(i, 1, l -> length){
cout << l -> arr[i] << " ";
}
cout << endl;
return 1;
}
}
int Insert_Sq(SqList * l)
{
printf("请输入要插入元素的数值和位置");
int pos, val;
cin >> val >> pos;
if(pos <= 0 || pos > l -> length + 2){
return -1;
}
l -> length ++;
l -> arr[l -> length] = 0;
// cout << pos << endl;
per(i, l -> length, pos){
// cout << l -> arr[i + 1] << " " << l -> arr[i] << endl;
l -> arr[i + 1] = l -> arr[i];
}
l -> arr[pos] = val;
rep(i, 1, l -> length){
cout << l -> arr[i] << " ";
}
cout << endl;
}
void InsertNewNode_Sq(SqList * l)
{
printf("请输入你要插入数组的元素数量:");
int newn;
cin >> newn;
printf("请输入数组元素:");
rep(i, 1, newn){
int num;
cin >> num;
l -> arr[++l -> length] = num;
// l -> length ++;
}
cout << endl;
QuickSort_Sq(1, l -> length, l);
rep(i, 1, l -> length){
cout << l -> arr[i] << " ";
}
cout << endl;
}
int TotalLength_Sq(SqList * l)
{
return l -> length;
}
signed main()
{
bool f = true;
int op;
SqList L;
while(f){
printf("操作菜单如下:\n");
printf("0:对数组初始化\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("请输入你的操作:");
cin >> op;
if(op == 0) {
int k = InitList_Sq(& L);
if(k == -1){
cout << "数组初始化失败\n";
} else {
cout << "赋值成功\n";
}
} else if(op == 2) {
QuickSort_Sq(1, n, & L);
rep(i, 1, n){
cout << L.arr[i] << " ";
}
printf("\n");
} else if(op == 3) {
int k = FindList_Sq(& L);
if(k == -1){
cout << "查询位置不合法\n";
}
} else if(op == 4) {
int k = Delete_Sq(& L);
if(k == -1){
cout << "删除元素不存在\n";
}
} else if(op == 5) {
int k = Insert_Sq(& L);
if(k == -1){
cout << "插入元素不合法\n";
}
} else if(op == 6) {
free(L.arr);
} else if(op == 1) {
ScannerList_Sq(& L);
} else if(op == 7) {
InsertNewNode_Sq(& L);
} else if(op == 8) {
int k = TotalLength_Sq(& L);
printf("该数组长度为:%d\n", k);
} else {
return 0;
}
}
}