// LinedStack
#include <iostream>
#include <cassert>
template<typename T>
struct StackNode {
T data;
StackNode<T> *link;
StackNode(T d = 0, StackNode<T> *next = NULL)
: link(next), data(d) {}
};
template<typename T>
class LinkedStack {
private:
StackNode<T> *top;
public:
LinkedStack() : top(NULL) { }
~LinkedStack() {
make_empty();
}
void push(const T& x);
bool pop(T& x);
bool get_top(T& x) const;
int get_size() const;
bool is_empty() const {
return top == NULL;
}
void make_empty();
friend std::ostream& operator<<(std::ostream& os, LinkedStack<T>& s) {
os << "StackSize: " << s.get_size() << std::endl;
StackNode<T> *p = s.top;
int i = 0;
while (p != NULL) {
os << ++i << ": " << p->data << std::endl;
p = p->link;
}
return os;
}
};
template<typename T>
void LinkedStack<T>::make_empty() {
StackNode<T> *p;
while (top != NULL) {
p = top;
top = top->link;
delete p;
}
}
template<typename T>
void LinkedStack<T>::push(const T& x) {
top = new StackNode<T>(x, top);
assert(top != NULL);
}
template<typename T>
bool LinkedStack<T>::pop(T& x) {
if (is_empty())
return false;
StackNode<T> *p = top;
top = top->link;
x = p->data;
delete p;
return true;
}
template<typename T>
bool LinkedStack<T>::get_top(T& x) const {
if (is_empty())
return false;
x = top->data;
return true;
}
template<typename T>
int LinkedStack<T>::get_size() const {
StackNode<T> *p = top;
int k = 0;
while (p != NULL) {
p = p->link;
k++;
}
return k;
}
// LinkedQueue
#include <iostream>
#include <cassert>
template<typename T>
struct LinkedNode {
T data;
LinkedNode<T> *link;
LinkedNode(LinkedNode<T> *ptr = NULL) {
link = ptr;
}
LinkedNode(const T& item, LinkedNode<T> *ptr = NULL) {
data = item;
link = ptr;
}
};
template<typename T>
class LinkedQueue {
public:
LinkedQueue() {
rear = NULL;
front = NULL;
}
~LinkedQueue() {
make_empty();
}
bool enqueue(const T& x);
bool dequeue(T& x);
bool get_front(T& x) const;
void make_empty();
bool is_empty() const {
return front == NULL;
}
int get_size() const;
friend std::ostream& operator<<(std::ostream& os, LinkedQueue<T>& que) {
LinkedNode<T> *p = que.front;
int i = 0;
while (p != NULL) {
os << "#" << ++i << ": " << p->data << std::endl;
p = p->link;
}
os << "QueueSize: " << que.get_size() << std::endl;
return os;
}
void output();
protected:
LinkedNode<T> *front;
LinkedNode<T> *rear;
};
template<typename T>
void LinkedQueue<T>::make_empty() {
LinkedQueue<T> *p ;
while (p != NULL) {
p = front;
front = front->link;
delete p;
}
}
template<typename T>
bool LinkedQueue<T>::enqueue(const T& x) {
if (front == NULL) {
front = rear = new LinkedNode<T>(x);
if (front == NULL)
return false;
} else {
rear->link = new LinkedNode<T>(x);
if (rear->link == NULL)
return false;
rear = rear->link;
}
return true;
}
template<typename T>
bool LinkedQueue<T>::dequeue(T& x) {
if (is_empty())
return false;
LinkedNode<T> *p = front;
x = front->data;
front = front->link;
delete p;
return true;
}
template<typename T>
bool LinkedQueue<T>::get_front(T& x) const {
if (is_empty())
return false;
x = front->data;
return true;
}
template<typename T>
int LinkedQueue<T>::get_size() const {
LinkedNode<T> *p = front;
int k = 0;
while (p != NULL) {
p = p->link;
k++;
}
return k;
}
template<typename T>
void LinkedQueue<T>::output() {
LinkedNode<T> *p = front;
int i = 1;
while (i <= get_size()) {
std::cout << p->data << " " << std::endl;
p = p->link;
i++;
}
}