zoukankan      html  css  js  c++  java
  • 顺序表

    #include <iostream>
    using namespace std;

    const int INITLENGTH = 10;
    const int INCREMENT = 10;
    const int INVALIDDATA = 100;

    struct Node
    {
        int* data;
        int length;
        int size;
    };

    struct Equal
    {
        bool operator()(const int& left, const int& right)
        {
            return (left == right) ? true : false;
        }
    };

    bool InitList(Node* &list);
    void Destory(Node* list);
    bool ListEmpty(Node* &list);
    int ListLength(Node* &list);
    bool GetElem(Node* list,int i,int& e);
    int LocateElem(Node* list,int e,Equal equ);
    bool ListInsert(Node* &list,int i,int e);
    bool ListDelete(Node* list,int i,int& e);
    void BiggerSpace(Node* &NewList);
    void PrintList(Node* list);
    void InvalidOrNot(Node* &L);

    int main()
    {
        Node* MyList;

        InitList(MyList);

    //    ListInsert(MyList,1,11);
        ListInsert(MyList,1,10);
        ListInsert(MyList,1,9);
        ListInsert(MyList,1,8);
        ListInsert(MyList,1,7);
        ListInsert(MyList,1,6);
        ListInsert(MyList,1,5);
        ListInsert(MyList,1,4);
        ListInsert(MyList,1,3);
        ListInsert(MyList,1,2);
        ListInsert(MyList,1,1);

    //     if (true == ListEmpty(MyList))
    //     {
    //         cout<<"Empty"<<endl;
    //     }
    //     else
    //     {
    //         cout<<"Not Empty"<<endl;
    //     }
    //
    //     cout<<ListLength(MyList)<<endl;

    //    PrintList(MyList);

    //    cout<<LocateElem(MyList,3,Equal())<<endl;

        Destory(MyList);

        return 0;
    }

    bool InitList(Node* &list)
    {

        list = new Node;
        list->data = new int[INITLENGTH];

        for (int i = 0; i < INITLENGTH; ++i)
        {
            list->data[i] = INVALIDDATA;
        }

        list->length = 0;
        list->size = INITLENGTH * sizeof(int);

        return true;
    }

    void Destory(Node* list)
    {
        InvalidOrNot(list);

        delete [] list->data;
        delete [] list;
        list = NULL;
    }

    bool ListEmpty(Node* &list)
    {
        InvalidOrNot(list);

         return (list->length == 0) ? true : false;
    }

    int ListLength(Node* &list)
    {
        InvalidOrNot(list);

        return list->length;
    }

    bool GetElem(Node* list,int i,int& e)
    {
        InvalidOrNot(list);

        if (i > 0 && i <= ListLength(list))
        {
            e = list->data[i - 1];
            return true;
        }

        return false;
    }

    int LocateElem(Node* list,int e,Equal equ)
    {
        int iLoc = -1;

        for (int i = 0; i < ListLength(list); ++i)
        {
            if (equ(list->data[i],e))
            {
                iLoc = ++i;
                return iLoc;
            }
        }

        return iLoc;
    }

    bool ListInsert(Node* &list,int i,int e)
    {
        InvalidOrNot(list);

        if (i < 1 || i > ListLength(list) + 1)
            return false;

        if (list->length * sizeof(int) == list->size)
        {
            BiggerSpace(list);
        }

        for (int j = ListLength(list) ; j >= i; j--)
        {
            list->data[j] = list->data[j - 1];
        }

        list->data[i - 1] = e;

        ++list->length;

        return true;
    }

    bool ListDelete(Node* list,int i,int& e)
    {
        InvalidOrNot(list);

        if (true == ListEmpty(list))
            return false;

        if (i < 1 || i > ListLength(list))
            return false;

        e = list->data[i - 1];

        for (int j = i - 1; j < ListLength(list) - 1; j++)
            list->data[j] = list->data[j + 1];

        list->data[ListLength(list) - 1] = INVALIDDATA;

        --list->length;

        return true;
    }

    void BiggerSpace(Node* &NewList)
    {
     
        int* temp = new int[NewList->length];

        for (int i = 0; i < NewList->length; ++i)
        {
            temp[i] = NewList->data[i];
        }

        delete [] NewList->data;

        NewList->data = new int[NewList->length + INCREMENT];
        NewList->size = NewList->size + INCREMENT * sizeof(int);

        for (int i = 0; i < NewList->length; ++i)
        {
            NewList->data[i] = temp[i];
        }

        delete [] temp;
        temp = NULL;

        for (int i = NewList->length; i < NewList->length + INCREMENT; i++)
        {
            NewList->data[i] = INVALIDDATA;
        }
    }

    void PrintList(Node* list)
    {
        InvalidOrNot(list);

        if (0 == ListLength(list))
            return ;

        for (int i = 0; i < ListLength(list); ++i)
            cout<<list->data[i]<<"\t";
        cout<<endl;
    }

    void InvalidOrNot(Node* &L)
    {
        if (NULL == L)
        {
            cout<<"ERROR"<<endl;
            exit(1);
        }
    }

  • 相关阅读:
    quick-cocos2d-x + Lua 开发
    设置 Quick-Cocos2d-x 在 Windows 下的编译环境
    luaIDE选择
    接管radiobutton onclick 事件
    AlphaControls的使用方法
    Delphi程序调用C#.Net编译的DLL并打开窗体(详解)
    delphi 正则表达式
    Windows server 2003 伪静态配置方法
    windows 2003 iis php
    Linux Shell系列教程之(十一)Shell while循环
  • 原文地址:https://www.cnblogs.com/BeyondTechnology/p/1857888.html
Copyright © 2011-2022 走看看