zoukankan      html  css  js  c++  java
  • 1.C++顺序表

    SeqList:

    #ifndef SEQLIST_H_
    #define SEQLIST_H_
    
    const int Size = 100;
    
    template<typename Type>
    class SeqList
    {
    private:
        Type *m_elements;
        const int m_maxsize;
        int m_currentsize;
    public:
        SeqList(int sz=Size):m_maxsize(sz),m_currentsize(-1)
        {
            if(sz>0)
                m_elements = new Type[m_maxsize];
        }
        ~SeqList() {delete[] m_elements;}
        int Length() const {return m_currentsize+1;}     //get the length
        int Find(Type x) const;                          //find the position of x 
        int IsElement(Type x) const;                     //is x in the list
        int Insert(Type x,int i);                        //insert x
        int Remove(Type x);                              //delete x
        int IsEmpty() {return m_currentsize==-1;}
        int Isfull() {return m_currentsize == m_maxsize-1;}
        Type get(int i) 
        {
            return i<0 || i>m_maxsize?(cout<<"can't find the element"<<endl,0):m_element[i];
        }
        void Print();
    };
    
    template<typename Type> int SeqList<Type>::Find(Type x) const
    {
        for(int i=0;i<m_currentsize;i++)
        {
            if(m_elements[i] == x)
                return i;
        }
        cout<<"can't find the element you want to find!"<<endl;
        return -1;
    }
    
    template<typename Type> int SeqList<Type>::IsElement(Type x) const
    {
        if(Find(x)==-1)
            return 0;
        return 1;
    }
    
    template<typename Type> int SeqList<Type>::Insert(Type x,int i)
    {
        if(i<0 || i>m_currentsize+1 || m_currentsize == m_maxsize -1)
        {
            cout<<"the operate is illegal!"<<endl;
            return 0;
        }
        m_currentsize++;
        for(int j=m_currentsize;j>i;j--)
            m_elements[j] = m_elements[j-1];
        m_elements[i] = x;
        return 1;
    }
    
    template<typename Type> int SeqList<Type>::Remove(Type x)
    {
        int count = m_currentsize;
        for(int i=0;i<m_currentsize;)
        {
            if(m_elements[i]==x)
            {
                for(int j=i;j<m_currentsize;j++)
                    m_elements[j] = m_elements[j+1];
                m_currentsize--;
                continue;
            }
            i++;
        }
        if(count == m_currentsize)
        {
            cout<<"can't find the element you want to remove!"<<endl;
            return 0;
        }
        return 1;
    }
    
    template<typename Type> void SeqList<Type>::Print()
    {
        for(int i=0;i<m_currentsize+1;i++)
            cout<<i+1<<":	"<<m_elements[i]<<endl;
        cout<<endl;
    }
    
    #endif

    Test:

    #include<iostream>
    #include"Seqlist.h"
    
    using namespace std;
    
    int main()
    {
        SeqList<int> test(15);
        int array[15] = {2,5,8,1,9,9,7,6,4,3,2,9,7,7,9};
        for(int i=0;i<15;i++)
            test.Insert(array[i],0);
        test.Print();
        test.Insert(1,0);
        cout<<(test.Find(0)?"can't be found ":"Be found ")<< 0 << endl<<endl;
        test.Remove(7);
        test.Print();
        test.Remove(9);
        test.Print();
        test.Remove(0);
        test.Print();
    
        return 0;
    }

     C风格:

    #include<iostream>
    using namespace std;
    typedef int elemType;//元素类型
    #define MAXSIZE 20
    struct SeqList
    {
        elemType data[MAXSIZE];
        int length;
    };
    
    bool InitList(SeqList *L)//初始化线性表
    {
        for(int i=0;i<MAXSIZE;i++)
            L->data[i] = 0;
        L->length = 0;
        return true;
    }
    
    bool IsEmpty(SeqList *L)//判断是否为空
    {
        if(L->data==0)
            return true;
        else
            return false;
    }
    
    bool ClearList(SeqList *L)//清空
    {
        for(int i=0;i<MAXSIZE;i++)
            L->data[i] = 0;
        L->length = 0;
        return true;
    }
    
    //用pd返回L中第pos个数据元素,注意pos指位置,第一个位置从0开始
    bool GetElem(SeqList *L,int pos,elemType *pd)
    {
        if(L->length==0 || pos<1 || pos>L->length)
            return false;
        *pd = L->data[pos-1];
        return true;
    }
    
    //返回L中第一个与x满足关系的数据元素,若不存在返回0
    int Locate(SeqList *L,elemType x)
    {
        for(int i=0;i<L->length;i++)
        {
            if(L->data[i]==x)
                return i+1;
        }
        return 0;
    }
    
    //在L中的第pos个位置之前插入新的数据元素y,长度加1
    bool InsertList(SeqList *L, int pos, elemType y)
    {
        if(L->length ==MAXSIZE)
            return false;
        if(pos<1 || pos>L->length+1)
            return false;
        if(pos <= L->length)
        {
            for(int i=L->length-1;i>=pos-1;i--)
           {
            L->data[i+1] = L->data[i];
           }
        }
        L->data[pos-1] = y;
        L->length++;
        return true;
    }
    
    //删除L的第pos个数据元素,并用pe返回其值,L的长度减1
    bool DeleteList(SeqList *L,int pos,elemType *pe)
    {
        if(pos<1 || pos>L->length)
            return false;
        *pe = L->data[pos-1];
        for(int i=pos;i<L->length;i++)
            L->data[i-1] = L->data[i];
        L->length--;
        return true;
    }
    
    int LengthList(SeqList *L)//返回长度
    {
        return L->length;
    }
    
    //将所有在线性表pb中但不在pa中的元素都插入pa中
    void UnionList(SeqList *pa,SeqList*pb)
    {
        int lena = pa->length;
        int lenb = pb->length;
        int item;
        for(int i=0;i<lenb;i++)
        {
            if(GetElem(pb,i+1,&item))
            {
                if(Locate(pa,item)==0)
                    InsertList(pa,++lena,item);
            }
        }
    }
    
    int main()
    {
        SeqList Sq;
        InitList(&Sq);
        for(int i=1;i<5;i++)
        {
            InsertList(&Sq ,i,i);
        }
        cout << LengthList(&Sq) <<endl;
        if (!IsEmpty(&Sq))
        {
            cout << "Sq: " << endl;
            for (int i = 0;i<LengthList(&Sq);i++)
                cout << Sq.data[i] << ' ';
        }
        cout << endl;
        
        int pos = Locate(&Sq, 2);
        if (pos != 0)
        {
            int result;
            DeleteList(&Sq, pos, &result);
            cout << "delete: " << result << endl;
        }
        
        if (!IsEmpty(&Sq))
        {
            cout << "Sq: " << endl;
            for (int i = 0 ; i < LengthList(&Sq); i++)
                cout << Sq.data[i] << ' ';
        }
        cout << endl;
        
        SeqList Sq2;
        InitList(&Sq2);
        for (int i = 1 ; i < 4; i++)
            InsertList(&Sq2, i, 6);
        InsertList(&Sq2, 4, 7);
        if (!IsEmpty(&Sq2))
        {
            cout << "Sq2: " << endl;
            for (int i = 0 ; i < LengthList(&Sq2); i++)
                cout << Sq2.data[i] << ' ';
        }
        cout << endl;
        
        UnionList(&Sq, &Sq2);
        
        if (!IsEmpty(&Sq))
        {
            cout << "Sq: " << endl;
            for (int i = 0 ; i < LengthList(&Sq); i++)
                cout << Sq.data[i] << ' ';
        }
        cout << endl;
    
    
        return 0;
    }
  • 相关阅读:
    #454. 【UER #8】打雪仗
    6496. 【GDOI2020模拟03.08】圣痕
    6495. 【GDOI2020模拟03.08】死星
    6494. 【GDOI2020模拟03.08】勘探
    NOI Online划水记
    6482. 【GDOI2020模拟02.22】代数几何(algebraic)
    6493. 【GDOI2020模拟03.04】迷宫
    6492. 【GDOI2020模拟03.04】多项式
    6491. 【GDOI2020模拟03.04】铺路
    #76. 【UR #6】懒癌
  • 原文地址:https://www.cnblogs.com/jx-yangbo/p/4839366.html
Copyright © 2011-2022 走看看