zoukankan      html  css  js  c++  java
  • c++线性结构基础

    一:线性表的顺序存储结构:

    #include <iostream>
    using namespace std;
    /* run this program using the console pauser or add your own getch, system("pause") or input loop */
    #define MAXSIZE 100
    typedef int DataType; //DataType代替int类型. 
    class SequenList
    {
    public:
        void Initiate(); //初始化线性表
        int Length();     //查看线性表的长度. 
        int Insert(DataType x,int i);
        int Deleted(int i);  //删除i号元素,注意i表示的是第几个元素,而不是数组中的下标. 
        DataType Locate(DataType x);
        DataType Get(int i);
        void show();
    private:
        DataType data[MAXSIZE];
        int len;  //用于记录线性表的长度. 
    };
    void SequenList::Initiate()
    {
        len = 0; //私有成员只有在类内部可以使用(除了友元类,友元函数) 
    }
    int SequenList::Insert(DataType x,int i)
    {
        int j;
        if(i>=MAXSIZE)
        {
            cout<<"overflow"<<endl; //超出线性表的空间。
            return 0; 
        }
        else if((i<1)||(i>len+1))
        {
            cout<<"插入位置不符合"<<endl;
            return 0; 
        }
        else
        {
            for(j = len;j>=i;j--)
                data[j] = data[j-1]; //把最后一项数据向后移动一位. 
            data[i-1] = x; //插入元素
            len++;
            cout<<"插入成功"<<endl; 
            return 1;
        }
    }
    int SequenList::Deleted(int i)
    {
        int j; //j作为一个媒介,可以以冒泡排序来理解. 
        if((i<1)||(i>len))  //i不能小于1是由i代表的不是下标所决定.
        {
            cout<<"删除位置不合法"<<endl;
         } 
         else
         {
             for(j=i;j<len;j++)
                 data[j-1] = data[j];
             len--; //len永远指向最后一个元素. 
            return 1;
         }
    }
    DataType SequenList::Locate(DataType x) //按值查找数据元素的位置. 
    {
        int j = 0;
        while((j<len)&&(data[j]!=x))
            j++;
        if(j<len)
            return j+1;
        else
            return 0;
    }
    DataType SequenList::Get(int i) //读取i号元素. 
    {
        if((i<1)||(i>len))
        {
            cout<<"不存在该元素"<<endl;
            return 0; //这里的0代表NULL,这里不可以使用NULL,不然会有警告,这是因为char和int类型不同, 
        }
        else
            return data[i-1];
     } 
     int SequenList::Length() //获取表的长度. 
     {
         return len;
     }
     void SequenList::show()
     {
         int *p;
         p = &this->data[0];
         for(int i=0;i<this->len;i++)
             cout<<*p++;
        cout<<endl;
     }
    int main(int argc, char** argv)
    {
        SequenList L; 
        L.Initiate(); 
        for(int i;i<10;i++) //插入值 
        {
            L.Insert(i,i);
        }
        L.show(); //查询插入的值
        int y = L.Length(); //获取其长度;
        cout<<y<<endl;
        L.Deleted(4); //删除下标为3的元素,
        L.show(); //查询
        y = L.Length();
        cout<<y<<endl; 
        return 0;
    }

    //输出:

    插入位置不符合
    插入成功
    插入成功
    插入成功
    插入成功
    插入成功
    插入成功
    插入成功
    插入成功
    插入成功
    123456789
    9
    12356789
    8

    
    

    --------------------------------
    Process exited after 0.09883 seconds with return value 0
    请按任意键继续.

     
  • 相关阅读:
    java中的工厂模式(简单工厂模式+工厂方法模式)
    代码集合
    java读取文件的路径问题
    使用ZXing库生成二维码
    java设计模式-装饰者模式
    android文件流缓存
    java8 新特性
    Excel导出
    常用的在线工具
    Java加密简介
  • 原文地址:https://www.cnblogs.com/1314bjwg/p/12465395.html
Copyright © 2011-2022 走看看