You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
940 B

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