zoukankan      html  css  js  c++  java
  • [原创]c++线性表的插入 Virus

    超简单的线性表的插入

    List.h

    class List
    {
    public:
        List(int MaxListSize=10)
        {
            maxSize=MaxListSize;
            element=new int[maxSize];
            length=0;
        }
        ~List()
        {
            delete []element;
        }
        bool IsEmpty() const//const:表示在函数中不可以修改对象的数据
        {
            return length==0;
        }
        int Length()
        {
            return length;
        }
      //  bool Find(int k, int &x) const;
    //    int Search(const int &x) const;//第一个const表示在函数中不可以修改传递的参数,第二个const表示在
    //                                 //函数中不可以修改对象的数据

        List &Insert(int k, const int &x);
    private:
        int length;
        int maxSize;
        int *element;
    };

    List.cpp

    #include "List.h"
    #include <iostream>

    using namespace std;


    List &List::Insert(int k, const int &x)
    {
        if(k<0 || k>length)
        {
            exit(1);
        }
        if(length==maxSize)
        {
            exit(1);
        }
        for(int i=length;i>=k;i--)
        {
            element[i+1]=element[i];
        }
        element[k]=x;
        length++;
        return *this;
    }


    main.cpp

    #include <cstdlib>
    #include <iostream>
    #include "List.h"

    using namespace std;

    int main(int argc, char *argv[])
    {
        List l;
        cout<<"Length="<<l.Length()<<endl;
        cout<<"IsEmpty="<<l.IsEmpty()<<endl;
        l.Insert(0,0);
        l.Insert(1,1);
        l.Insert(2,2);
        l.Insert(3,3);
        cout<<"current Length is "<<l.Length()<<endl;
        //l.OutPut();
        //l.Insert(
       
        system("PAUSE");
        return EXIT_SUCCESS;
    }

    【Blog】http://virusswb.cnblogs.com/

    【MSN】jorden008@hotmail.com

    【说明】转载请标明出处,谢谢

    反馈文章质量,你可以通过快速通道评论:

  • 相关阅读:
    团体程序设计天梯赛-练习集L1-002. 打印沙漏
    团体程序设计天梯赛-练习集L1-001. Hello World
    腾讯的一笔画游戏
    Educational Codeforces Round 11
    POJ 1149 PIGS
    POJ 3422 Kaka's Matrix Travels
    POJ 2914 Minimum Cut
    POJ 1815 Friendship
    POJ 1966 Cable TV Network
    BZOJ 1797: [Ahoi2009]Mincut 最小割
  • 原文地址:https://www.cnblogs.com/virusswb/p/838704.html
Copyright © 2011-2022 走看看