当前位置:网站首页>线性表的实现
线性表的实现
2022-07-21 15:11:00 【小飞将】
BaseList.h
#pragma once
template <class T>
class BaseList {
public:
typedef size_t pos;
virtual void clear() = 0; // empty the list
virtual bool isEmpty() = 0; // Is empty list?
virtual void append(const T&) = 0; // add element in the end
virtual void insert(const pos, const T&) = 0; // insert element in the middle
virtual void del(const pos) = 0; // delete element from list
virtual T getValue(const pos) = 0; // return the value in the position
virtual void setValue(const pos, const T&) = 0; // modify the value at position
virtual pos getPosition(const T&) = 0; // get the position of value T
};
ArrList.h
#pragma once
#include <iostream>
#include <string>
#include <exception>
#include "BaseList.h"
template <class T>
class ArrList : public BaseList<T> {
private:
int maxSize;
int currentLen;
int position;
T* arrList;
void checkRangeOverflow() {
if (currentLen + 1 > maxSize) {
std::cout << "Will exceed the max size of list" << std::endl;
throw std::runtime_error("List overflow");
}
}
void checkPosition(const BaseList<T>::pos p) {
if (p < 0 || p > maxSize) {
std::cout << "Not a valid position" << std::endl;
throw std::runtime_error("Invalid position");
}
}
public:
ArrList(const int size)
: maxSize(size), currentLen(0), position(0), arrList(new T[maxSize]) {
}
~ArrList() {
delete[] arrList;
}
virtual void clear() override {
delete[] arrList;
currentLen = position = 0;
arrList = nullptr;
}
virtual bool isEmpty() override {
return currentLen == 0;
}
virtual void append(const T &val) override {
checkRangeOverflow();
arrList[position] = val;
currentLen++;
position++;
return;
}
virtual void insert(const BaseList<T>::pos p, const T &val) override {
checkRangeOverflow();
checkPosition(p);
for (int cp = currentLen; cp != p; cp--) {
arrList[cp] = arrList[cp - 1];
}
arrList[p] = val;
currentLen++;
position = p;
return;
}
virtual void del(const BaseList<T>::pos p) override {
checkPosition(p);
if (isEmpty()) {
std::cout << "Empty list, no element to delete" << std::endl;
return;
}
for (int cp = p; cp != currentLen; cp++) {
arrList[cp] = arrList[cp + 1];
}
currentLen--;
return;
}
virtual T getValue(const BaseList<T>::pos p) override {
checkPosition(p);
if (isEmpty()) {
std::cout << "Empty list" << std::endl;
throw std::runtime_error("Cannot get value from empty list");
}
return arrList[p];
}
virtual void setValue(const BaseList<T>::pos p, const T& val) override {
checkPosition(p);
if (isEmpty()) {
std::cout << "Empty list" << std::endl;
throw std::runtime_error("Cannot get value in empty list");
}
arrList[p] = val;
return;
}
virtual BaseList<T>::pos getPosition(const T& val) override {
if (isEmpty()) {
std::cout << "Empty list" << std::endl;
throw std::runtime_error("Empty List");
}
for (int p = 0; p != currentLen; p++) {
if (arrList[p] == val) {
return p;
}
}
std::cout << "Not found" << std::endl;
return -1;
}
void print() const {
for (int p = 0; p != currentLen; p++) {
std::cout << arrList[p] << std::endl;
}
return;
}
};
main.cpp
// ArrList.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <cstdlib>
#include <iostream>
#include "ArrList.h"
using namespace std;
int main(int argc, char* argv[])
{
int val, p, n;
ArrList<int> a(10);
cout << "Enter the count you want to append to arrlist" << endl;
cin >> n;
for (int i = 0; i < n; i++) {
cout << "Enter the value you want to append" << endl;
cin >> val;
a.append(val);
}
cout << "Show the arrlist" << endl;
a.print();
cout << "Enter the value you want to search" << endl;
cin >> n;
cout << "The value you want to search is in position: " << a.getPosition(n) << endl;
cout << "Enter the position you want to delete" << endl;
cin >> p;
a.del(p);
cout << "Show the arrlist" << endl;
a.print();
cout << "Enter the position you want to insert" << endl;
cin >> p;
cout << "Enter the value you want to insert" << endl;
cin >> val;
a.insert(p, val);
cout << "Show the arrlist" << endl;
a.print();
return 0;
}
边栏推荐
猜你喜欢
随机推荐
自定义 Handler 时如何有效地避免内存泄漏问题?
What does it mean that the profit and loss of financial products are negative?
Educational Codeforces Round 100 (Rated for Div. 2) B. Find The Array 题解
东方广益深夜辟谣,称入股锤子被立案调查消息不实!
side effect of intrinsics
请问开户对年龄有限制么?请问,手机开户股票开户安全吗?
Arcgis图层标注显示
String description
application.yml 和 application.propertise 配置不生效
ENVI波段合成逆运算——波段拆分
Custom class loader implementation
ArcGIS创建矢量
flask - { “message“: “Failed to decode JSON object: Expecting value: line 1 column 1 (char 0)“ }
微信小程序列表(数据渲染之列表渲染)
[MySQL and database] MySQL & database Chapter 8: variables in MySQL
uniapp设置tabBar跳转后,其他页面跳转到主页(整理)
Sword finger offer - print binary tree from top to bottom - (queue structure)
面试题 01.02. 判定是否互为字符重排
缓存穿透、雪崩、一致性问题
Vscode - no PIP installer is available in the selected environment