设为首页 加入收藏

TOP

利用C++中采用面向对象的思想顺序表(一)
2018-10-21 22:08:42 】 浏览:60
Tags:利用 采用 面向 对象 思想 顺序

  最近在复习数据结构,我用面向对象的思想实现了顺序表,采用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
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇洛谷P3808 【模板】AC自动机(简.. 下一篇C++标准库之右值引用与交付语义

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目