2023年3月10日
#C++抽象类栈
类似于接口,其他类通过实现该接口来完成自身类的定义
template<class T>
class stack
{
public:
virtual ~stack() {}
virtual bool empty() const = 0;
// return true iff stack is empty
virtual int size() const = 0;
// return number of elements in stack
virtual T& top() = 0;
// return reference to the top element
virtual void pop() = 0;
// remove the top element
virtual void push(const T& theElement) = 0;
// insert theElement at the top of the stack
};
#数组描述栈
template<class T>
class arrayStack : public stack<T>
{
public:
arrayStack(int initialCapacity = 10);
~arrayStack() {delete [] stack;}
bool empty() const {return stackTop == -1;}
int size() const
{return stackTop + 1;}
T& top()
{
if (stackTop == -1)
throw stackEmpty();
return stack[stackTop];
}
void pop()
{
if (stackTop == -1)
throw stackEmpty();
stack[stackTop--].~T(); // destructor for T
}
void push(const T& theElement);
private:
int stackTop; // current top of stack
int arrayLength; // stack capacity
T *stack; // element array
};
template<class T>
arrayStack<T>::arrayStack(int initialCapacity)
{// Constructor.
if (initialCapacity < 1)
{ostringstream s;
s << "Initial capacity = " << initialCapacity << " Must be > 0";
throw illegalParameterValue(s.str());
}
arrayLength = initialCapacity;
stack = new T[arrayLength];
stackTop = -1;
}
template<class T>
void arrayStack<T>::push(const T& theElement)
{// Add theElement to stack.
if (stackTop == arrayLength - 1)
{// no space, double capacity
changeLength1D(stack, arrayLength, 2 * arrayLength);
arrayLength *= 2;
}
// add at stack top
stack[++stackTop] = theElement;
}
其中,stackTop表示当前栈顶,arrayLength表示栈容量,T* stack表示元素数组
#用链表描述栈
template<class T>
class linkedStack : public stack<T>
{
public:
linkedStack(int initialCapacity = 10)
{stackTop = NULL; stackSize = 0;}
~linkedStack();
bool empty() const
{return stackSize == 0;}
int size() const
{return stackSize;}
T& top()
{
if (stackSize == 0)
throw stackEmpty();
return stackTop->element;
}
void pop();
void push(const T& theElement)
{
stackTop = new chainNode<T>(theElement, stackTop);
stackSize++;
}
private:
chainNode<T>* stackTop; // pointer to stack top
int stackSize; // number of elements in stack
};
template<class T>
linkedStack<T>::~linkedStack()
{// Destructor.
while (stackTop != NULL)
{// delete top node
chainNode<T>* nextNode = stackTop->next;
delete stackTop;
stackTop = nextNode;
}
}
template<class T>
void linkedStack<T>::pop()
{// Delete top element.
if (stackSize == 0)
throw stackEmpty();
chainNode<T>* nextNode = stackTop->next;
delete stackTop;
stackTop = nextNode;
stackSize--;
}
其中,chainNode<T>* stackTop表示栈顶指针,stackSize表示栈中元素个数
#栈和队列的理论基础
这里放链接方便学习,不再打字赘述
#用栈表示队列
可以用两个栈,一个专门用来入栈,一个专门用来出栈,代码如下
class MyQueue {
public:
stack<int> stIn;
stack<int> stOut;
/** Initialize your data structure here. */
MyQueue() {
}
/** Push element x to the back of queue. */
void push(int x) {
stIn.push(x);
}
/** Removes the element from in front of queue and returns that element. */
int pop() {
// 只有当stOut为空的时候,再从stIn里导入数据(导入stIn全部数据)
if (stOut.empty()) {
// 从stIn导入数据直到stIn为空
while(!stIn.empty()) {
stOut.push(stIn.top());
stIn.pop();
}
}
int result = stOut.top();
stOut.pop();
return result;
}
/** Get the front element. */
int peek() {
int res = this->pop(); // 直接使用已有的pop函数
stOut.push(res); // 因为pop函数弹出了元素res,所以再添加回去
return res;
}
/** Returns whether the queue is empty. */
bool empty() {
return stIn.empty() && stOut.empty();
}
};
#用队列实现栈
同理,用两个队列
class MyStack {
public:
queue<int> que1;
queue<int> que2; // 辅助队列,用来备份
/** Initialize your data structure here. */
MyStack() {
}
/** Push element x onto stack. */
void push(int x) {
que1.push(x);
}
/** Removes the element on top of the stack and returns that element. */
int pop() {
int size = que1.size();
size--;
while (size--) { // 将que1 导入que2,但要留下最后一个元素
que2.push(que1.front());
que1.pop();
}
int result = que1.front(); // 留下的最后一个元素就是要返回的值
que1.pop();
que1 = que2; // 再将que2赋值给que1
while (!que2.empty()) { // 清空que2
que2.pop();
}
return result;
}
/** Get the top element. */
int top() {
return que1.back();
}
/** Returns whether the stack is empty. */
bool empty() {
return que1.empty();
}
};
当我只用一个队列的时候,一个队列在模拟栈弹出元素的时候只要将队列头部的元素(除了最后一个元素外) 重新添加到队列尾部,此时再去弹出元素就是栈的顺序了。
class MyStack {
public:
queue<int> que;
/** Initialize your data structure here. */
MyStack() {
}
/** Push element x onto stack. */
void push(int x) {
que.push(x);
}
/** Removes the element on top of the stack and returns that element. */
int pop() {
int size = que.size();
size--;
while (size--) { // 将队列头部的元素(除了最后一个元素外) 重新添加到队列尾部
que.push(que.front());
que.pop();
}
int result = que.front(); // 此时弹出的元素顺序就是栈的顺序了
que.pop();
return result;
}
/** Get the top element. */
int top() {
return que.back();
}
/** Returns whether the stack is empty. */
bool empty() {
return que.empty();
}
};
这两天有点迷了,希望自己能赶紧调整状态回归学习状态,明天见!