最近在复习数据结构,我用面向对象的思想实现了顺序表,采用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[];
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 = ;
} SqList::~SqList()
{
this->length = ;
} // 求取表的长度
int SqList::Length()
{
return this->length;
} // 判断表是否为空
bool SqList::IsEmpty()
{
if (this->Length() == )
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< || pos>LISTSIZE) {
cerr << "插入位置不合法!其取值范围应为[1,100]" << endl;
return false;
} // 3.元素后移
for (int i = this->Length() - ; i >= pos - ; i--) {
this->items[i + ] = this->items[i];
} // 4.将待插入的元素放在它该在的位置
this->items[pos-] = item; // 5.修改表长 不要忘记!
this->length++;
return true;
} // 展示元素
void SqList::Display()
{
if (this->length) {// 如果表不空,则展示
for (int i = ; 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< || pos>this->Length()) {
cerr << "删除位置不合法!其取值范围应为[1,"<<this->length<<"]!" << endl;
return false;
} // 3.元素往前覆盖
for (int i = pos; i < this->length; i++) {
this->items[i - ] = this->items[i];
} // 4. 修改表长 不要忘记
this->length--; return true;
} // 在顺序表中查找元素data 返回data在顺序表中的位置
int SqList::Find(DataType data)
{
// 1.判断表是否为空
if (this->length == ) {
cerr << "顺序表为空表!无法进行查找操作!" << endl;
return -;
}
// 2.再寻找data
int i = ;
while (i < this->length&&this->items[i] != data)i++;
if (i < this->length)return i+;
else return -;
} bool SqList::GetData(int pos,DataType* data )//用data指针将数据传输出去
{
if (this->length == ) {
//data = NULL;
return false;
}
if (pos< || pos>this->length) {
cerr << "输入位置不合法!" << endl;
//data = NULL;
return false;
}
*data = this->items[pos - ];
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 = ; i < ; i++) {
list1.Insert(i + , i+);
}
cout << "插入1-99后的顺序表为:" << endl;
list1.Display(); cout << "当前表长为:" <<list1.Length() << endl;
cout << "表为空吗?" << list1.IsEmpty() << endl;
cout << "---------------------------------------------------------------------------------" << endl; cout << endl;
cout << "---------------------------------------------------------------------------------" << endl;
// 然后在第8个位置插入1000
list1.Insert(, );
cout << "再在第8个位置插入1000后的顺序表为:" << endl;
list1.Display();
cout << "当前表长为:" << list1.Length() << endl;
cout << "表为空吗?" << list1.IsEmpty() << endl;
cout << "当前1000在表中的位置是" << list1.Find() << endl;
cout << "---------------------------------------------------------------------------------" << endl; cout << endl;
cout << "---------------------------------------------------------------------------------" << endl;
list1.Delete();
list1.Delete();
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(,pData) << endl;
cout << "取到第0个位置的元素了吗?" << list1.GetData(, pData) << endl;
cout << "取到第5个位置的元素了吗?" << list1.GetData(, pData) << endl;
cout << "第5个位置的元素值为:" << *pData << endl;
cout << "---------------------------------------------------------------------------------" << endl; cout << endl;
cout << "---------------------------------------------------------------------------------" << endl;
cout << "在表中查找1000的位置是:" << list1.Find() << endl;
cout << "在表中查找100的位置是:" << list1.Find() << endl;
cout << "在表中查找39的位置是:" << list1.Find() << endl;
cout << "---------------------------------------------------------------------------------" << endl; return ;
}

   编译并运行得下图:

  

利用C++中采用面向对象的思想顺序表的更多相关文章

  1. 利用 Python 尝试采用面向对象的设计方法计算图形面积及周长

    利用 Python 尝试采用面向对象的设计方法.(1)设计一个基类 Shape:包含两个成员函数:def cal_area(): 计算并返回该图形的面积,保留两位小数:def cal_perimete ...

  2. C++的标准模板库STL中实现的数据结构之顺序表vector的分析与使用

    摘要 本文主要借助对C++的标准模板库STL中实现的数据结构的学习和使用来加深对数据结构的理解.即联系数据结构的理论分析和详细的应用实现(STL),本文是系列总结的第一篇,主要针对线性表中的顺序表(动 ...

  3. JavaScript设计模式学习之路——面向对象的思想

    今天,我拿到了张容铭写的这本<JavaScript设计模式>这本书,开始了关于JavaScript更深一点的学习. 看到这本书开始的时候,虽然之前通过看书.一些比较好的视频的讲解,对Jav ...

  4. 顺序表的原理与python中的list类型

    数据是如何在内存中存储的? 在32位的计算机上,1个字节有8位,内存寻址的最小单位就是字节.假设我们有一个int类型的值,它从0x10开始,一个int占据4个字节,则其结束于0x13. 那么数据类型有 ...

  5. 3、顺序表、内存、类型、python中的list

    1.内存.类型本质.连续存储 1.内存本质 2.C 语言实例-计算 int, float, double 和 char 字节大小 使用 sizeof 操作符计算int, float, double 和 ...

  6. Java实现顺序表

    利用顺序存储结构表示的顺序表称为顺序表. 它用一组连续的地址存储单元一次存放线性表中的数据元素. 顺序表的实现是数据结构中最简单的一种. 由于代码中已经有详细注释,代码外不再阐述. 下次再陈上关于顺序 ...

  7. K:线性表的实现—顺序表

    所谓顺序表,就是顺序存储的线性表.顺序存储就是用一组地址连续的存储单元依次存放线性表中各个数据元素的存储结构. 线性表中所有数据元素的类型是相同的,所以每一个数据元素在存储器中占用相同的大小的空间.假 ...

  8. python算法与数据结构-顺序表(37)

    1.顺序表介绍 顺序表是最简单的一种线性结构,逻辑上相邻的数据在计算机内的存储位置也是相邻的,可以快速定位第几个元素,中间不允许有空,所以插入.删除时需要移动大量元素.顺序表可以分配一段连续的存储空间 ...

  9. 【数据结构】之顺序表(Java语言描述)

    之前总结过使用C语言描述的顺序表数据结构.在C语言类库中没有为我们提供顺序表的数据结构,因此我们需要自己手写,详细的有关顺序表的数据结构描述和C语言代码请见[我的这篇文章]. 在Java语言的JDK中 ...

随机推荐

  1. PAT 1036 Boys vs Girls

    #include <cstdio> #include <iostream> #include <cstdlib> #include <algorithm> ...

  2. Java中Date()类 日期转字符串、字符串转日期的问题(已解决)

    Java中Date()类 日期转字符串.字符串转日期的问题 今天在写东西的时候突然发现一个问题,就是先new 一个Date()然后将生成的值转为字符串, 然后再将转换后的字符串再次用new Date( ...

  3. 【代码笔记】XML深入学习:DTD约束与DTD语法(2)

    DTD语法之定义实体(了解即可) 实体分为一般实体和参数实体. 定义实体:定义变量 引用实体:使用变量 一般实体:定义实体在DTD中,实体引用在xml中. 参数实体:定义实体在DTD中,实体引用在DT ...

  4. CSS中font-family:中文字体对应的英文名称

    中文字体 对应英文字体 宋体 SimSun 黑体 SimHei 微软雅黑 Microsoft YaHei 微软正黑体 Microsoft JhengHei 新宋体 NSimSun 新细明体 PMing ...

  5. oracle查询所有用户表的表名、主键名称、索引、外键等

    1.查找表的所有索引(包括索引名,类型,构成列): select t.*,i.index_type from user_ind_columns t,user_indexes i where t.ind ...

  6. day008-File文件

    1. File 文件和目录路径名的抽象表示形式. 一个File类对象就代表了一个文件或文件夹. 1.1 File类的作用 用来操作硬盘上的文件或文件夹 绝对路径:一般是以盘符开始的,比如:C:/Jav ...

  7. 文件读取方法(FileHelpers) z

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using FileHelp ...

  8. (四)WebDriver常用方法

    点击和输入 前面我们已经学习了定位元素, 定位只是第一步, 定位之后需要对这个元素进行操作, 或单击(按钮) 或输入(输入框) , 下面就来认识 WebDriver 中最常用的几个方法: clear( ...

  9. MovieReview—The Foreigner (英伦对决)

    The Foreigner's theme is revenge.The whole story is carried out in two dimensions:political struggle ...

  10. Android Studio常用快捷键、Android Studio快捷键大全

    Android Studio 是谷歌基于IntelliJ IDEA开发的安卓开发工具,有点类似 Eclipse ADT,Android Studio 提供了集成的 Android 开发工具用于开发和调 ...