#include <bits/stdc++.h>
using namespace std;
typedef struct
{
    int *elem, len;
}Array;
const int maxn = 1e5;
int       j,choose,n,e,gn,fn,ip,in,choose2,Insert,dn;
Array a;
// typedef int Status
int InitList(Array &L)
{
    L.elem = new int[maxn];
    if (!L.elem)
        return -2;
    L.len = 0;
    return 1;
}
int GetElem(Array L, int i, int &e)
{
    if ((i < 1) || (i > L.len))
        return 0;
    e = L.elem[i - 1];
    return 1;
}
int LocateElem(Array L, int e)
{
    for (int i = 0; i < L.len; i++)
        if (L.elem[i] == e)
            return i + 1;
    return 0;
}
int ListInsert(Array &L, int i, int e)
{
    if ((i < 1) || (i > L.len + 1))
        return -2;
    if (L.len == maxn)
        return 0;
    for (j = L.len - 1; j >= i - 1; j--)
    {
        L.elem[j + 1] = L.elem[j];
    }
    L.elem[i - 1] = e;
    L.len++;
    return 1;
}
int ListDelete(Array &L, int i)
{
    if ((i < 1) || (i > L.len + 1))
        return 0;
    for (j = i; j <= L.len - 1; j++)
        L.elem[j - 1] = L.elem[j];
    L.len--;
    return 1;
}
int main()
{
    // Array a;
    // InitList(a);
    // for (int xx = 1; xx <= 10; xx++)
    //     cin >> a.elem[xx], a.len++;
    // cout << LocateElem(a, 5);
    cout<<"--------若要正常使用本功能,请在最开始的时候选择初始化--------\n\n\n";
    cout << "1.初始化\n"; cout << "2.取值\n"; cout << "3.查找\n"; cout << "4.插入\n"; cout << "5.删除\n";cout << "0.退出\n";
    choose=-1;
    while(choose!=0)
    {
        cout<<"请选择:";
        cin>>choose;
        switch(choose)
        {
            case 1:
                if(InitList(a)==1)
                {
                    puts("初始化成功");
                    cout<<"请输入你要输入的数据个数:";cin>>n;
                    cout<<"请输入"<<n<<"个数:\n";
                    for(j=0;j<n;j++)
                        cin>>a.elem[j],a.len++;
                    cout<<"存储成功\n";
                }
                else    puts("存储分配失败");
                break;
            case 2:
                cout<<"请输入你想取出来的数的位置:";cin>>gn;
                if(GetElem(a,gn,e))    cout<<"取出来的数为:"<<e<<endl;
                else    cout<<"位置不存在\n";
                break;
            case 3:
                cout<<"请输入你想查找的数值:";cin>>fn;
                if(LocateElem(a,fn))    cout<<"查找成功\n这个数是数列中的第"<<LocateElem(a,fn)<<"个元素\n";
                else    cout<<"查找失败,这个数不存在\n";
                break;
            case 4:
                cout<<"请输入你想插入的元素:";cin>>in;
                cout<<"请输入你想插入到的位置:";cin>>ip;
                Insert=ListInsert(a,ip,in);
                if(Insert==0)    cout<<"插入失败,存储空间已满\n";
                if(Insert==-2)    cout<<"插入失败,插入位置不存在\n";
                if(Insert==1)
                {
                    cout<<"插入成功\n";
                    choose2=-1;
                    cout<<"是否要查看插入之后的数组?1=是,0=否:";cin>>choose2;
                    if(choose=1)
                    for(int i=0;i<a.len;i++)
                        cout<<a.elem[i]<<" ";
                    cout<<endl;
                }
                break;
            case 5:
                cout<<"请输入你想删除的数的位置:";cin>>dn;
                if(ListDelete(a,dn))    cout<<"删除成功\n";
                else    cout<<"删除失败\n";
                break;
        }
    }
    return 0;
}