zoukankan      html  css  js  c++  java
  • 数组的基本操作

    #include <iostream>
    #include <stdlib.h>
    using namespace std;
    struct SqList {
        int *elem;
        int length;
    };
    
    int InitList(SqList &L,int num)
    {
        L.elem = new int[num];
        if (!L.elem) {
            cout << "Creation failed!" << endl; 
            exit(1);
        }
        L.length = num;
        cout << "Created successfully." << endl;
        cout << "Please enter the elements." << endl;
        for (int i = 0; i < num;i++) {
            cin >> L.elem[i];
            if (i==num-1)
                cout << "Input complete."<<endl;
        }
        return 0;
    }
    
    void DestroyList(SqList &L)
    {
        if (L.elem) {
            delete L.elem;
            cout << "Destruction succeeded." << endl;
        }
        else
            cout << "Destroy failed." << endl;
    }
    
    void ClearList(SqList &L)
    {
        if (L.elem) {
            L.length = 0;
            cout << "Clear success." << endl;
        }
        else
            cout << "Purge failed." << endl;
    }
    
    int ListEmpty(SqList &L)
    {
        if (L.elem) {
            if (!L.length)
                return 1;    
        }
        return 0;
    }
    
    int ListLength(SqList &L)
    {
        if (L.elem) {
            cout << L.length << endl;
        }
        else
            cout << "Find failed." << endl;
        return 0;
    }
    
    int GetElem(SqList &L,int num,int &e)
    {
        if (!L.elem) {
            cout << "Find failed." << endl;
            return 0;
        }
        if (num>=L.length||num<0){
            cout << "Find failed." << endl;
            return 0;
        }
        e = L.elem[num];
        return 1;
    }
    
    int LocateElem(SqList &L,int e)
    {
        if (!L.elem) {
            cout << "Find failed." << endl;
            return 0;
        }
        int flag = 0;
        for (int i = 0; i < L.length;i++) {
            if (e<L.elem[i]) {
                cout << i << endl;
                flag = 1;
            }
        }
        if (flag==0)
            cout << 0 << endl;
        return 0;
    }
    
    int PriorElem(SqList &L,int &cur_e,int &pre_e)
    {
        if (!L.elem||cur_e==L.elem[0])
            cout << "Find failed." << endl;
        for (int i = 1; i < L.length;i++) {
            if (cur_e==L.elem[i]) {
                pre_e = L.elem[i - 1];
            }
        }
        return 0;
    }
    
    int NextElem(SqList &L,int &cur_e,int &next_e)
    {
        if (!L.elem||cur_e==L.elem[L.length-1])
            cout << "Find failed." << endl;
        for (int i = 0; i < L.length-1;i++) {
            if (cur_e==L.elem[i]){
                next_e = L.elem[i + 1];
            }
        }
        return 0;
    }
    
    int ListInsert(SqList &L,int num,int e)
    {
        if (!L.elem||num<0||num>L.length) {
            cout << "Insert failed." << endl;
            return 0;
        }
        for (int i = L.length - 1; i >= num;i--) {
            L.elem[i + 1] = L.elem[i];
        }
        L.elem[num] = e;
        L.length++;
        cout << "Insert Successfully." << endl;
        return 0;
    }
    
    int ListDelete(SqList &L,int num,int &e)
    {
        if (!L.elem||num<0||num>=L.length) {
            cout << "Delete failed." << endl;
            return 0;
        }
        e = L.elem[num];
        for (int i = num; i < L.length-1;i++) {
            L.elem[i] = L.elem[i+1];
        }
        L.length--;
        cout << "Delete succeeded." << endl;
        return 0;
    }
    int ListTraverse(SqList &L)
    {
        if (!L.elem) {
            cout << "Traversal failure." << endl;
            return 0;
        }
        for (int i = 0; i < L.length;i++)
            cout << L.elem[i] << " ";
        cout << endl;
        return 0;
    }
    int main()
    {
        int n,num,cnt,pre_e,cur_e,next_e,e;
        SqList sq;
        cout << "Please enter what you want to do." << endl
             << "1:Create a linear table." << endl
             << "2:Destroying a linear table." << endl
             << "3:Empty the linear table." << endl
             << "4:Empty table." << endl
             << "5:To find the length of a linear table." << endl
             << "6:To find the value of the element I of a linear table." << endl
             << "7:Position the first number greater than num." << endl
             << "8:Looking for a precursor." << endl
             << "9:Looking for a successor." << endl
             << "10:Inserting elements." << endl
             << "11:Delete elements." << endl
             << "12:Traversing a linear table." << endl
             << "Input 0 end." << endl;
        while (~scanf("%d",&n)&&n) {
            cnt = 0;
            switch(n) {
                case 1:
                    cout << "Please enter the number of elements." << endl;
                    cin >> num;
                    InitList(sq,num);
                    break;
                case 2:
                    DestroyList(sq);
                    break;
                case 3:
                    ClearList(sq);
                    break;
                case 4:
                    if (ListEmpty(sq))
                        cout << "TRUE" << endl;
                    else
                        cout << "FLASE" << endl;
                    break;
                case 5:
                    ListLength(sq);
                    break;
                case 6:
                    int num, e;
                    cout << "Please enter the ordinal number you want to find." << endl;
                    cin >> num;
                    if (GetElem(sq, num, e))
                        cout << e << endl;
                    break;
                case 7:
                    cout << "Please enter the number you want to compare." << endl;
                    cin >> num;
                    LocateElem(sq,num);
                    break;
                case 8:
                    pre_e = NULL;
                    cout << "Please enter the current element." << endl;
                    cin >> cur_e;
                    PriorElem(sq, cur_e, pre_e);
                    cout << pre_e<<endl;
                    break;
                case 9:
                    next_e = NULL;
                    cout << "Please enter the current element." << endl;
                    cin >> cur_e;
                    NextElem(sq, cur_e, next_e);
                    cout << next_e << endl;
                    break;
                case 10:
                    cout << "Please enter the number where you want to insert." << endl
                         << "Input num and where." << endl;
                    cin >> e >> num;
                    ListInsert(sq, num, e);
                    break;
                case 11:
                    cout << "Please enter number you want to delete." << endl;
                    cin >> num;
                    ListDelete(sq, num, e);
                    cout << e << endl;
                    break;
                case 12:
                    ListTraverse(sq);
            }
        }
        system("pause");
        return 0;
    }
  • 相关阅读:
    Git常用命令集合
    kubeadm搭建高可用集群-版本1.18.2
    springboot实现事务管理
    定时任务突然中止,告警:Thread starvation or clock leap detected
    Class版本号和Java版本对应关系
    vue开发环境配置
    CentOS配置jar应用程序开机启动的方法
    jvm运行时数据区之程序计数器
    JVM常见面试题及答案
    MYSQL的修改表结构SQL语句
  • 原文地址:https://www.cnblogs.com/xyqxyq/p/10211370.html
Copyright © 2011-2022 走看看