This commit is contained in:
e2hang
2025-07-30 14:33:09 +08:00
parent c77f685f1f
commit ecb484a9e8
36 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,7 @@
#include <iostream>
#include <stack>
using namespace std;
int main(){
return 0;
}

View File

@@ -0,0 +1,35 @@
#include <iostream>
#include <stdexcept>
#include <string>
#include <stack>
using namespace std;
void isMatch(const string& s){
stack<int> t;
//bool error = false;
for(int i = 0; i < s.length(); i++){
if(s[i] == '('){
t.push(i);
}
if(s[i] == ')'){
try{
if(t.empty()) throw std::runtime_error("Error: Braket \" ) \" No Match");
t.pop();
}
catch(const runtime_error& e){
cout << e.what() << endl;
//error = true;
return;
}
}
}
if(t.empty()) cout << "Match" << endl;
else cout << "Error: Braket \" ( \" No Match" << endl;
}
int main(){
string s;
cin >> s;
isMatch(s);
return 0;
}

Binary file not shown.

View File

@@ -0,0 +1,92 @@
#include <iostream>
#include <stack>
using namespace std;
void hanoi(int n, int a, int b, int c){
if(n == 1){
cout << "Move 1 from stack " << a << " to " << b << "(n == 1)" << endl;
}
if(n > 1){
hanoi(n - 1, a, b, c);
cout << "Move " << n << " from stack " << a << " to " << c << endl;
hanoi(n - 1, b, c, a);
}
}
int main(){
hanoi(1, 1, 2, 3);
cout << endl;
hanoi(2, 1, 2, 3);
cout << endl;
hanoi(3, 1, 2, 3);
cout << endl;
hanoi(4, 1, 2, 3);
cout << endl;
hanoi(5, 1, 2, 3);
cout << endl;
return 0;
}
/*
Move 1 from stack 1 to 2(n == 1)
Move 1 from stack 1 to 2(n == 1)
Move 2 from stack 1 to 3
Move 1 from stack 2 to 3(n == 1)
Move 1 from stack 1 to 2(n == 1)
Move 2 from stack 1 to 3
Move 1 from stack 2 to 3(n == 1)
Move 3 from stack 1 to 3
Move 1 from stack 2 to 3(n == 1)
Move 2 from stack 2 to 1
Move 1 from stack 3 to 1(n == 1)
Move 1 from stack 1 to 2(n == 1)
Move 2 from stack 1 to 3
Move 1 from stack 2 to 3(n == 1)
Move 3 from stack 1 to 3
Move 1 from stack 2 to 3(n == 1)
Move 2 from stack 2 to 1
Move 1 from stack 3 to 1(n == 1)
Move 4 from stack 1 to 3
Move 1 from stack 2 to 3(n == 1)
Move 2 from stack 2 to 1
Move 1 from stack 3 to 1(n == 1)
Move 3 from stack 2 to 1
Move 1 from stack 3 to 1(n == 1)
Move 2 from stack 3 to 2
Move 1 from stack 1 to 2(n == 1)
Move 1 from stack 1 to 2(n == 1)
Move 2 from stack 1 to 3
Move 1 from stack 2 to 3(n == 1)
Move 3 from stack 1 to 3
Move 1 from stack 2 to 3(n == 1)
Move 2 from stack 2 to 1
Move 1 from stack 3 to 1(n == 1)
Move 4 from stack 1 to 3
Move 1 from stack 2 to 3(n == 1)
Move 2 from stack 2 to 1
Move 1 from stack 3 to 1(n == 1)
Move 3 from stack 2 to 1
Move 1 from stack 3 to 1(n == 1)
Move 2 from stack 3 to 2
Move 1 from stack 1 to 2(n == 1)
Move 5 from stack 1 to 3
Move 1 from stack 2 to 3(n == 1)
Move 2 from stack 2 to 1
Move 1 from stack 3 to 1(n == 1)
Move 3 from stack 2 to 1
Move 1 from stack 3 to 1(n == 1)
Move 2 from stack 3 to 2
Move 1 from stack 1 to 2(n == 1)
Move 4 from stack 2 to 1
Move 1 from stack 3 to 1(n == 1)
Move 2 from stack 3 to 2
Move 1 from stack 1 to 2(n == 1)
Move 3 from stack 3 to 2
Move 1 from stack 1 to 2(n == 1)
Move 2 from stack 1 to 3
Move 1 from stack 2 to 3(n == 1)
*/

Binary file not shown.

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

View File

@@ -0,0 +1,43 @@
#include <iostream>
#include "stack.h"
#include "arraystack.h"
using namespace std;
/*
TO TEST:
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;
arrayStack& operator=(const arrayStack& x);
arrayStack(const arrayStack& x);
*/
int main(){
arrayStack<char> s(10);
s.push('a');
s.show();
s.push('b');
s.show();
s.push('c');
s.show();
s.pop();
s.show();
return 0;
}
/*
a
a b
a b c
a b
*/

View File

@@ -0,0 +1,13 @@
#pragma once
template <class T>
class Stack {
public:
virtual ~Stack(){}
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;
};

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

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
*/

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){}

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