chainStack

This commit is contained in:
e2hang
2025-07-21 17:08:57 +08:00
parent 047e634d93
commit a18c6ba5d4
4 changed files with 206 additions and 0 deletions

View File

@@ -0,0 +1,127 @@
#ifndef CHAIN_STACKH
#define CHAIN_STACKH
#include <iostream>
#include "stack.h"
#include "node.h"
template <class T>
class chainStack : public Stack<T>{
private:
Node<T>* first;
Node<T>* last;
int stackTop;
public:
chainStack() = delete;
chainStack(Node<T>* f = nullptr);
chainStack(const chainStack<T>& x);
virtual bool empty() const;
virtual int size() const;
virtual T& top();
virtual void pop();
virtual void push(const T& theElement);
virtual void show() const;
~chainStack();
};
template <class T>
inline chainStack<T>::chainStack(Node<T>* f) :first(f), stackTop(0), last(nullptr) {
Node<T>* p = first;
while (p) {
++stackTop;
p = p->next;
}
}
template <typename T>
inline chainStack<T>::chainStack(const chainStack<T>& x)
{
Node<T>* run = x.first;
Node<T>* tnext = nullptr;
Node<T>* tprev = nullptr;
while (run) {
Node<T>* tmp = new Node<T>(run->element, tprev, tnext);
if(tprev) tprev->next = tmp;
if (!tprev) first = tmp;
tprev = tmp;
run = run->next;
}
last = tprev;
}
template<class T>
inline bool chainStack<T>::empty() const
{
return (stackTop == 0);
}
template<class T>
inline int chainStack<T>::size() const
{
return stackTop;
}
template<class T>
inline T& chainStack<T>::top()
{
return last->element;
}
template<class T>
inline void chainStack<T>::pop()
{
if (stackTop == 0)
throw std::invalid_argument("Pop Error: No Element in Stack");
if (last) {
Node<T>* tmp = last;
last = last->prev;
if (last) {
last->next = nullptr;
}
else {
first = nullptr;
}
delete tmp;
stackTop--;
}
}
template<class T>
inline void chainStack<T>::push(const T& theElement) {
Node<T>* tmp = new Node<T>(theElement, last, nullptr);
if (last) last->next = tmp;
else first = tmp;
last = tmp;
stackTop++;
}
template<class T>
inline void chainStack<T>::show() const {
Node<T>* p = first;
while (p) {
std::cout << p->element << " ";
p = p->next;
}
std::cout << std::endl;
}
template<class T>
inline chainStack<T>::~chainStack()
{
Node<T>* p = first;
Node<T>* that;
while (p) {
that = p;
p = that->next;
delete that;
}
first = nullptr;
last = nullptr;
stackTop = 0;
}
#endif // !CHAIN_STACKH

42
Stack/chainStack/main.cpp Normal file
View File

@@ -0,0 +1,42 @@
#include <iostream>
#include "node.h"
#include "stack.h"
#include "chainStack.h"
using namespace std;
/*
TO TEST:
private:
Node<T>* first;
Node<T>* last;
int stackTop;
public:
chainStack() = delete;
chainStack(Node<T>* f = nullptr);
chainStack(const chainStack<T>& x);
virtual bool empty() const;
virtual int size() const;
virtual T& top();
virtual void pop();
virtual void push(const T& theElement);
virtual void show() const;
~chainStack()
*/
int main() {
chainStack<char> s(nullptr);
s.push('a');
s.show();
s.push('b');
s.show();
s.push('z');
s.show();
s.pop();
s.show();
return 0;
}
/*
a
a b
a b z
a b
*/

24
Stack/chainStack/node.h Normal file
View File

@@ -0,0 +1,24 @@
#pragma once
template <class T>
class Node {
public:
T element;
Node<T>* next;
Node<T>* prev;
public:
Node(); // Ĭ<>Ϲ<EFBFBD><CFB9><EFBFBD>
Node(const T& e, Node<T>* n = nullptr, Node<T>* p = nullptr); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
Node(const Node<T>& x); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>죨dz<ECA3A8><C7B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
~Node() = default;
};
template<class T>
Node<T>::Node() : element(), next(nullptr) , prev(nullptr){}
template<class T>
Node<T>::Node(const T& e, Node<T>* n, Node<T>* p) : element(e), next(p), prev(n){}
template<class T>
Node<T>::Node(const Node<T>& x) : element(x.element), next(nullptr), prev(nullptr){}

13
Stack/chainStack/stack.h Normal file
View File

@@ -0,0 +1,13 @@
#pragma once
template <class T>
class Stack {
public:
virtual ~Stack() = default;
virtual bool empty() const = 0;
virtual int size() const = 0;
virtual T& top() = 0;
virtual void pop() = 0;
virtual void push(const T& theElement) = 0;
virtual void show() const = 0;
};