c++day6(2024/1/3)

 #include 
#include 
using namespace std;


#include 

using namespace std;

template
class List{
public:
    T1 len;
    T2 arr[20];
    List(){}
//    List &operator=(const List &other)
//    {
//        len=other.len;
//        strcpy(arr,other.arr);
//        return *this;
//    }
};

template
List init(List &list)
{
    list.len=0;
    memset(list.arr,0,sizeof(list.arr));
    return list;
};

//尾插
template
List insertRear(List &list,T2 data)
{
    if(list.len>=20)
        throw new int(1);
    list.arr[list.len++]=data;
    return list;
};

//指定位置插入元素
template
void inserLocation(List &list,T1 index,T2 data)
{
    if(index<0 || index >list.len)
        throw new int(2);
    for (int i=list.len;i>index;i--) {
           list.arr[i]=list.arr[i-1];
    }
    list.len++;
    list.arr[index]=data;
}

//指定位置删除元素
template
void deleteLocation(List &list,T1 index)
{
    if(index<0 || index >=list.len || list.len<=0)
        throw new int(3);
    for (int i=index;i
int searchLocation(List &list,T2 element)
{
    for (int i=0;i
void deleteElement(List &list,T2 element)
{
    int index=searchLocation(list,element);
    deleteLocation(list,index);
}

//打印顺序表
template
void show(List &list)
{
    cout << "列表数据为:" << endl;
    for (int i=0;i list;
    init(list);

    try {
        //尾插赋值列表
        cout << "请输入您要插入的元素,以字符'#'结束";
        int a;
        int count=1;
        char element;
        while(1)
        {
            cout << "请输入第" << count << "个字符" << endl;
            cin>>element;
            if(element == '#')
                break;

            count++;
            insertRear(list,element);
        }
        show(list);

        cout <<"-----------------------------"<>index;
        cout << "请输入您要插入字符值" << endl;
        cin >>element;
        inserLocation(list,index-1,element);
        show(list);

        cout <<"-----------------------------"<>index;
        deleteLocation(list,index-1);
        show(list);

        cout <<"-----------------------------"<> element;
        index=searchLocation(list,element);
        cout << "位置在第" << index+1 << "个" << endl;

        //按照值删除元素
        cout << "请输入您要删除的值" << endl;
        cin >>element;
        deleteElement(list,element);
        show(list);
    } catch (int i) {
        if(i==1){
            cout << "列表已满,无法插入" <

c++day6(2024/1/3)_第1张图片

你可能感兴趣的:(c++,开发语言)