顺序表 ~ 程序实现二
本篇博客本篇博客基于 温故而知新 -> 数据结构 -> 线性表 -> 顺序表 中的理论知识,并利用 C++
中的 类 对数据结构中的 顺序表 进行代码实现!
其中涉及了顺序表的 增(头插、尾插、任插)删(头删、尾删、任删)查(节点寻找)改(没写(~ ̄▽ ̄)~),判空,销毁,打印等操作!并附带了实例以及对应的运行结果!
具体内容如下
(1)SeqListHpp.h
#pragma once
#include<iostream>
using namespace std;
#include<assert.h>
#define SLDataType int
class SeqList
{
public:
SeqList() :_nums(nullptr)
, _size(0)
, _capa(0)
{}
~SeqList()
{
if (_nums != nullptr)
{
free(_nums);
_nums = NULL;
_size = _capa = 0;
}
}
void CheckCapacity();//检查容量,为空则为其开辟空间
void PushBack(SLDataType val);//尾插数据
void PopBack();//尾删函数
void PushFront(SLDataType val);//头插
void PopFront();//头删
void Insert(int pos, SLDataType val);//在特定位置插入数据
SLDataType ListBack();//获得表尾元素
void Erase(int pos);//删除在特定位置上的数据
void Destory();//销毁这个数组内容
bool Empty();//是否为空
int Size();//返回数组中元素个数
void Find(SLDataType val);//寻找数据位置
void ListPrint();//打印数组内容
void ListElement(int pos);//打印位置POS上的数据
private:
SLDataType *_nums; //指向动态开辟的数组
size_t _size; //有效数据个数
size_t _capa; //空间大小
};
(2)main.cpp
#include"SeqListHpp.h"
void SeqList::CheckCapacity()//检查容量,为空则为其开辟空间
{
if (_size == _capa)
{
int newCapa = _capa == 0 ? 1 : 2 * _capa;//更改空间大小
_nums = (SLDataType*)realloc(_nums, newCapa*sizeof(SLDataType));
_capa = newCapa;
}
}
void SeqList::PushBack(SLDataType val)//尾插数据
{
/*assert(_nums != nullptr);
SeqList::CheckCapacity();
_nums[_size] = val;
_size++;*/
SeqList::Insert(_size+1, val);
}
void SeqList::PopBack()//尾删函数
{
/*assert(_nums != nullptr);
if (_size > 0)
_size--;*/
SeqList::Erase(_size);
}
void SeqList::PushFront(SLDataType val)//头插
{
/*assert(_nums != nullptr);
SeqList::CheckCapacity();
_size++;
for (int i = _size-1; i > 0; i--)
_nums[i] = _nums[i - 1];
_nums[0] = val;*/
//上述程序可以用 Insert() 进行替换
SeqList::Insert(1, val);
}
void SeqList::PopFront()//头删
{
/*assert(_nums != nullptr);
for (int i = 0; i < _size; i++)
_nums[i] = _nums[i + 1];
_size--;*/
SeqList::Erase(1);
}
void SeqList::Insert(int pos, SLDataType val)//在特定位置插入数据
{
//注:该程序我实现时,当 pos > _size 时,程序不会退出,而是将数据进行尾插,
// 正常情况下,此时应程序退出
assert(_nums != nullptr);
if (pos <= 0)
{
cout << "越界" << endl;
return;
}
SeqList::CheckCapacity();
_size++;
if (pos <= _size - 1)
{
for (int i = _size-1; i > pos-1; i--)
_nums[i] = _nums[i - 1];
_nums[pos - 1] = val;
}
else if (pos > _size - 1)
{
_nums[_size - 1] = val;
}
}
SLDataType SeqList::ListBack()
{
return _nums[_size-1];
}
void SeqList::Erase(int pos)//删除在特定位置上的数据
{
assert(_nums != nullptr);
if (pos > _size || pos <= 0)
{
cout << "数组没有该位置的数据!!" << endl;
return;
}
int start = pos - 1;
while (start < _size-1)
{
_nums[start] = _nums[start + 1];
start++;
}
_size--;
}
void SeqList::Destory()//销毁这个数组内容
{
if (_nums != nullptr)
{
free(_nums);
_nums = NULL;
_size = _capa = 0;
}
//SeqList::~SeqList();
}
bool SeqList::Empty()//是否为空
{
if (_nums != nullptr)
{
cout << " !NULL" << endl;
return true;
}
else
{
cout << " NULL" << endl;
return false;
}
}
int SeqList::Size()//返回数组中元素个数
{
return _size;
}
void SeqList::Find(SLDataType val)//寻找数据位置
{
assert(_nums != nullptr);
for (int i = 0; i < _size; i++)
{
if (val == _nums[i])
{
cout << "数组中 " << val << " 的pos为 " << i + 1 << endl;
return;
}
}
cout << "数组中没有这个数" << endl;
}
void SeqList::ListPrint()//打印数组内容
{
if (_nums == nullptr)
{
cout << "顺序表中没有数据" << endl;
return;
}
cout << "顺序表内容:" << endl;
if (_size <= 0)
cout << "NULL" << endl;
for (int i = 0; i < _size; i++)
cout << _nums[i] << " ";
cout << endl;
}
void SeqList::ListElement(int pos)//打印位置POS上的数据
{
assert(_nums != nullptr);
if (pos <= 0 || pos>_size)
cout << "数组没有这个位置" << endl;
cout <<"数组中 " << pos << " 位置的数据为 " << _nums[pos - 1] << endl;
}
void test()
{
SeqList s1;
s1.Empty();
s1.CheckCapacity();
s1.Empty();
cout << endl;
/* 实验 尾插 与 尾删 */
s1.PushBack(1);
s1.PushBack(2);
s1.PushBack(3);
s1.PushBack(4);
s1.ListPrint();//1 2 3 4
s1.PopBack();
s1.PopBack();
s1.ListPrint();//1 2
cout << endl;
/* 实验 头插 与 头删 */
s1.PushFront(50);
s1.PushFront(40);
s1.PushFront(30);
s1.ListPrint();//30 40 50 1 2
s1.PopFront();
s1.PopFront();
s1.PopFront();
s1.ListPrint();//1 2
cout << endl;
/* 实验 指定位置插入 */
s1.Insert(1, 50);//50 1 2 相当于头插
s1.Insert(2, 60);//50 60 1 2
s1.Insert(5, 100);//50 60 1 2 100 相当于尾插
s1.Insert(5, 150);//50 60 1 2 150 100
s1.ListPrint();
s1.Insert(-1, 50);// 越界
cout << endl;
/* 实验 指定位置删除 */
s1.Erase(5);
s1.ListPrint();//50 60 1 2 100
s1.Erase(5);
s1.ListPrint();//50 60 1 2 相当于尾删
s1.Erase(1);
s1.ListPrint();//60 1 2 相当于头删
cout << endl;
cout << "元素个数:" << s1.Size() << endl;
cout << endl;
s1.Find(60);
s1.Find(2);
s1.Find(555);
cout << endl;
s1.ListElement(2);
cout << endl;
s1.Destory();
s1.ListPrint();
}
int main()
{
test();
system("pause");
return 0;
}
(3)运行结果