ADD file via upload

main
mi84kabv9 6 months ago
parent 4f9a590b03
commit 4ab346a607

@ -0,0 +1,63 @@
template<typename T>
class Stack;
// ½Úµã¶¨Òå
template<typename T>
class Node {
private:
T info;
Node* next;
public:
friend class Stack<T>;
Node() : next(nullptr) {};
Node(T item, Node<T>* nextnode = NULL) {
info = item;
next = nextnode;
}
Node(Node<T>& node) {
info = node.info;
next = node.next;
}
};
// Õ»¶¨Òå
template<typename T>
class Stack {
private:
Node<T>* top;
public:
Stack() {
top = NULL;
}
bool push(const T& item) {
Node<T>* p = top;
top = new Node<T>;
top->info = item;
top->next = p;
return true;
}
bool pop() {
if (top == NULL) return false;
else {
Node<T>* p = top;
top = top->next;
delete p;
}
return true;
}
T peek() {
return top->info;
}
~Stack() {
Node<T>* p = top;
while (top) {
top = top->next;
delete p;
p = top;
}
top = p = NULL;
}
bool empty() {
return top == NULL;
}
};
Loading…
Cancel
Save