线性表的实现

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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值