/*
* This class implements a stack by directly using a linked list.
*/
#ifndef _lnkstack_h_
#define _lnkstack_h_
template <typename T>
class Stack {
public:
// Initialize the stack empty.
Stack() {
m_head = nullptr;
m_size = 0;
}
// Push the argument item.
void push(const T &itm) {
m_head = new node(itm, m_head);
++m_size;
}
// Pop the argument item, and return true, or return false if the
// the stack is empty.
bool pop(T &itm) {
if(m_head == nullptr) return false;
itm = m_head->data();
node *zombie = m_head;
m_head = m_head->next();
delete zombie;
--m_size;
return true;
}
// Convenience to pop and return the top object. Only works
// for types where T() is a valid expression.
T pop() {
T ret;
if(pop(ret)) return ret;
return T();
}
// Tell if empty.
bool empty() {
return m_head == nullptr;
}
int size() {
return m_size;
}
private:
// Nodes for the linked list.
class node {
public:
node(const T &d, node *n = nullptr) {
m_data = d;
m_next = n;
}
const T& data() { return m_data; }
node *next() { return m_next; }
private:
T m_data;
node *m_next;
};
node * m_head; // Head of the linked list.
int m_size; // Size of the stack.
};
#endif
To make a linked structure, we must be able to allocate nodes. This is
done using new, just as in Java. Two important differences:
- The new operation returns a pointer to the allocated object.
(This pointer should not be used like an array iterator, since there was only
one object allocated; never increment it.)
- The system does automatically get rid of the object when you are done.
You must explicitly delete it.