Matrix & Stack
This commit is contained in:
98
Stack/arrayStack/arraystack.h
Normal file
98
Stack/arrayStack/arraystack.h
Normal file
@@ -0,0 +1,98 @@
|
||||
#pragma once
|
||||
#include <iostream>
|
||||
#include "stack.h"
|
||||
|
||||
template <class T>
|
||||
class arrayStack : public Stack<T> {
|
||||
private:
|
||||
T* stack;
|
||||
int stackTop;
|
||||
int length;
|
||||
public:
|
||||
arrayStack(int initialCapacity = 10);
|
||||
virtual ~arrayStack() { delete[] stack; }
|
||||
|
||||
virtual bool empty() const override { return stackTop == -1; }
|
||||
virtual int size() const override { return stackTop + 1; }
|
||||
virtual T& top() override;
|
||||
virtual void pop() override;
|
||||
virtual void push(const T& theElement) override;
|
||||
virtual void show() const;
|
||||
arrayStack& operator=(const arrayStack& x);
|
||||
arrayStack(const arrayStack& x);
|
||||
|
||||
};
|
||||
|
||||
template <class T>
|
||||
arrayStack<T>::arrayStack(int initialCapacity) {
|
||||
length = initialCapacity;
|
||||
if(length < 0) {
|
||||
throw std::invalid_argument("Initial Error: Length < 0");
|
||||
length = 1;
|
||||
}
|
||||
stack = new T[length];
|
||||
stackTop = -1;
|
||||
}
|
||||
|
||||
|
||||
template <class T>
|
||||
T& arrayStack<T>::top() {
|
||||
if (stackTop == -1) throw std::invalid_argument("Top Error: No Element in Stack");
|
||||
return stack[stackTop];
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void arrayStack<T>::pop() {
|
||||
if (stackTop == -1) throw std::invalid_argument("Pop Error: Empty Stack");
|
||||
if constexpr (!std::is_trivially_destructible<T>::value) {
|
||||
stack[stackTop].~T(); // ֻ<><D6BB><EFBFBD><EFBFBD> T <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>ŵ<EFBFBD><C5B5><EFBFBD>
|
||||
}
|
||||
stackTop--;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void arrayStack<T>::push(const T& theElement) {
|
||||
if(stackTop == length - 1){
|
||||
T* tmp = new T[length * 2];
|
||||
for(int i = 0; i < length; i++){
|
||||
tmp[i] = stack[i];
|
||||
}
|
||||
delete[] stack;
|
||||
stack = tmp;
|
||||
length *= 2;
|
||||
}
|
||||
stackTop++;
|
||||
stack[stackTop] = theElement;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
arrayStack<T>::arrayStack(const arrayStack& x) {
|
||||
length = x.length;
|
||||
stackTop = x.stackTop;
|
||||
stack = new T[length];
|
||||
for (int i = 0; i <= stackTop; ++i) {
|
||||
stack[i] = x.stack[i];
|
||||
}
|
||||
}
|
||||
|
||||
template <class T>
|
||||
arrayStack<T>& arrayStack<T>::operator=(const arrayStack<T>& x) {
|
||||
if (this != &x) {
|
||||
delete[] stack;
|
||||
length = x.length;
|
||||
stackTop = x.stackTop;
|
||||
stack = new T[length];
|
||||
for (int i = 0; i <= stackTop; ++i) {
|
||||
stack[i] = x.stack[i];
|
||||
}
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void arrayStack<T>::show() const {
|
||||
for (int i = 0; i <= stackTop; i++) {
|
||||
std::cout << stack[i] << " ";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
Reference in New Issue
Block a user