当前位置: 移动技术网 > IT编程>开发语言>C/C++ > 利用C++中采用面向对象的思想顺序表

利用C++中采用面向对象的思想顺序表

2018年06月29日  | 移动技术网IT编程  | 我要评论

步步惊心丽19,2014年医改最新消息,中华人民共和国国歌

  最近在复习数据结构,我用面向对象的思想实现了顺序表,采用C++语言。

  首先建立在Visual Studio 2017中建立一个工程,然后新建一个类SqList。然后会生成SqList.h和SqList.cpp文件。分别编写这两个文件。

  SqList.h文件如下:

#pragma once


typedef int DataType;

// 自定义表的大小为100
#define LISTSIZE 100


class SqList
{
private:
    DataType items[100];
    int length;

public:
    SqList();
    ~SqList();
    int Length();
    bool IsEmpty();
    bool Insert(int pos, DataType item);
    void Display();
    bool Delete(int pos);
    int Find(DataType data);
    bool GetData(int pos, DataType* data);
};

  SqList.cpp文件如下:

#include "stdafx.h"
#include "SqList.h"
#include<iostream>
using namespace std;
SqList::SqList()
{
    this->length = 0;
}


SqList::~SqList()
{
    this->length = 0;
}

// 求取表的长度
int SqList::Length()
{
    return this->length;
}

// 判断表是否为空
bool SqList::IsEmpty()
{
    if (this->Length() == 0)
        return true;
    else return false;
}

// 在pos位置插入一个数据元素item
bool SqList::Insert(int pos, DataType item)
{
    // 1.判断表是否已满
    if (this->Length() >= LISTSIZE) {
        cerr << "顺序表最大长度为100,已满!请删除一些元素后重新插入" << endl;
        return false;
    }
    
    // 2.判断插入位置是否合法
    if (pos<1 || pos>LISTSIZE) {
        cerr << "插入位置不合法!其取值范围应为[1,100]" << endl;
        return false;
    }

    // 3.元素后移
    for (int i = this->Length() - 1; i >= pos - 1; i--) {
        this->items[i + 1] = this->items[i];
    }

    // 4.将待插入的元素放在它该在的位置
    this->items[pos-1] = item;

    // 5.修改表长      不要忘记!
    this->length++;
    return true;
}


// 展示元素
void SqList::Display()
{
    if (this->length) {// 如果表不空,则展示
        for (int i = 0; i < this->length; i++) {
            cout << this->items[i] << " ";
        }
        cout << endl;
    }
    else {
        cerr << "顺序表为空,无法展示!!!" << endl;
    }
    
}


// 删除pos位置上的元素
bool SqList::Delete(int pos)
{
    // 1.判断表是否为空
    if (this->IsEmpty()) {
        cerr << "顺序表是空表,无法执行删除操作!" << endl;
        return false;
    }

    // 2.判断删除位置是否合法
    if (pos<1 || pos>this->Length()) {
        cerr << "删除位置不合法!其取值范围应为[1,"<<this->length<<"]!" << endl;
        return false;
    }
    
    // 3.元素往前覆盖
    for (int i = pos; i < this->length; i++) {
        this->items[i - 1] = this->items[i];
    }

    // 4. 修改表长   不要忘记
    this->length--;

    return true;
}


// 在顺序表中查找元素data 返回data在顺序表中的位置
int SqList::Find(DataType data)
{
    // 1.判断表是否为空
    if (this->length == 0) {
        cerr << "顺序表为空表!无法进行查找操作!" << endl;
        return -1;
    }
    // 2.再寻找data
    int i = 0;
    while (i < this->length&&this->items[i] != data)i++;
    if (i < this->length)return i+1;
    else return -1;
}

bool SqList::GetData(int pos,DataType* data )//用data指针将数据传输出去
{
    if (this->length == 0) {
        //data = NULL;
        return false;
    }
    if (pos<1 || pos>this->length) {
        cerr << "输入位置不合法!" << endl;
        //data = NULL;
        return false;
    }
    *data = this->items[pos - 1];
    return true;
}

   在源文件中添加并编写Main.cpp文件如下:

#include"CholenSort.h"
#include"SqList.h"
#include<iostream>
using namespace std;

int main()
{
    SqList list1;

    cout << "初始化后表长为:" << list1.Length() << endl;
    cout << "表为空吗?"<<list1.IsEmpty() << endl;

    cout << "---------------------------------------------------------------------------------" << endl;
    // 循环插入1-99
    for (int i = 0; i < 99; i++) {
        list1.Insert(i + 1, i+1);
    }
    cout << "插入1-99后的顺序表为:" << endl;
    list1.Display();

    cout << "当前表长为:" <<list1.Length() << endl;
    cout << "表为空吗?" << list1.IsEmpty() << endl;
    cout << "---------------------------------------------------------------------------------" << endl;

    cout << endl;
    cout << "---------------------------------------------------------------------------------" << endl;
    // 然后在第8个位置插入1000
    list1.Insert(8, 1000);
    cout << "再在第8个位置插入1000后的顺序表为:" << endl;
    list1.Display();
    cout << "当前表长为:" << list1.Length() << endl;
    cout << "表为空吗?" << list1.IsEmpty() << endl;
    cout << "当前1000在表中的位置是" << list1.Find(1000) << endl;
    cout << "---------------------------------------------------------------------------------" << endl;


    cout << endl;
    cout << "---------------------------------------------------------------------------------" << endl;
    list1.Delete(0);
    list1.Delete(8);
    cout << "删除表中的第0个位置和和第8个位置后表为:" << endl;
    list1.Display();
    cout << "当前表长为:" << list1.Length() << endl;
    cout << "表为空吗?" << list1.IsEmpty() << endl;
    cout << "---------------------------------------------------------------------------------" << endl;

    cout << endl;
    cout << "---------------------------------------------------------------------------------" << endl;
    DataType data, *pData;
    pData = &data;
    cout << "取到第1000个位置的元素了吗?" << list1.GetData(1000,pData) << endl;
    cout << "取到第0个位置的元素了吗?" << list1.GetData(0, pData) << endl;
    cout << "取到第5个位置的元素了吗?" << list1.GetData(5, pData) << endl;
    cout << "第5个位置的元素值为:" << *pData << endl;
    cout << "---------------------------------------------------------------------------------" << endl;

    cout << endl;
    cout << "---------------------------------------------------------------------------------" << endl;
    cout << "在表中查找1000的位置是:" << list1.Find(1000) << endl;
    cout << "在表中查找100的位置是:" << list1.Find(100) << endl;
    cout << "在表中查找39的位置是:" << list1.Find(39) << endl;
    cout << "---------------------------------------------------------------------------------" << endl;

    return 0;
}

   编译并运行得下图:

  

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网