STL中已经有<stack>和<queue>了,写在这里是帮助理解这两种数据结构。
栈:只能在表的一段插入(push)或者删除(pop),且满足先进先出的顺序。很形象的称之为“栈”。
连续栈(数组实现):
#include <iostream>
using namespace std;
const int stackSize = 10000;
template <class Stack_entry>
class Stack{ // Contiguous Stack
public:
Stack();
void pop(); /*pre: make sure the stack is not empty */
Stack_entry top() const;/*pre: make sure the stack is not empty */
bool push(const Stack_entry&);
bool empty() const;
private:
Stack_entry entry[stackSize];
unsigned int count;
};
template <class Stack_entry>
Stack<Stack_entry> :: Stack(){
count = 0;
}
template <class Stack_entry>
void Stack<Stack_entry> :: pop(){
if(count > 0){
count--;
}
/*else underflow*/
return;
}
template <class Stack_entry>
Stack_entry Stack<Stack_entry> :: top() const{
if(count > 0){
return entry[count-1];
}
/* return underflow */
}
template <class Stack_entry>
bool Stack<Stack_entry> :: push(const Stack_entry& newEntry){
if(count < stackSize){
entry[count++] = newEntry;
return true;
}
return false/*overflow*/;
}
template <class Stack_entry>
bool Stack<Stack_entry> :: empty() const{
return count == 0;
}
int main()
{
Stack<int> s;
s.push(1);
s.push(2);
s.push(3);
while(!s.empty()){
cout << s.top() << endl;
s.pop();
}
return 0;
}
链式栈(链表):
#include <iostream>
using namespace std;
template <typename Node_entry>
struct Node{
Node_entry entry;
Node* next;
Node(){
next = NULL;
}
Node(const Node_entry& newEntry, Node* add_on = NULL){
entry = newEntry;
next = add_on;
}
};
template <class Stack_entry>
class Stack{ // Linked Stack
public:
Stack();
Stack(const Stack& original);
~Stack();
void pop(); /*pre: make sure the stack is not empty */
Stack_entry top() const;/*pre: make sure the stack is not empty */
void push(const Stack_entry&);
bool empty() const;
//
Stack& operator=(const Stack& original);
//
private:
Node<Stack_entry>* top_node;
};
template <class Stack_entry>
Stack<Stack_entry> :: Stack(){
top_node = NULL;
}
template <class Stack_entry>
Stack<Stack_entry> :: Stack(const Stack& original){
Node<Stack_entry> *new_copy, *original_node = original.top_node;
if(original_node == NULL) top_node = NULL;
else{
top_node = new_copy = new Node<Stack_entry>(original_node->entry);
while(original_node->next != NULL){
original_node = original_node->next;
new_copy->next = new Node<Stack_entry>(original_node->entry);
new_copy = new_copy->next;
}
}
}
template <class Stack_entry>
Stack<Stack_entry> :: ~Stack(){
while(top_node != NULL){
Node<Stack_entry>* old_node = top_node;
top_node = top_node -> next;
delete old_node;
}
}
template <class Stack_entry>
void Stack<Stack_entry> :: pop(){
if(top_node != NULL){
Node<Stack_entry>* old_node = top_node;
top_node = top_node -> next;
delete old_node;
}
/*else underflow*/
return;
}
template <class Stack_entry>
Stack_entry Stack<Stack_entry> :: top() const{
if(top_node != NULL){
return top_node -> entry;
}
/* return underflow */
}
template <class Stack_entry>
void Stack<Stack_entry> :: push(const Stack_entry& newEntry){
Node<Stack_entry>* new_top = new Node<Stack_entry>(newEntry, top_node);
//if(new_top == NULL) return/*overflow*/; // the dynamic memory is exhausted
top_node = new_top;
return;
}
template <class Stack_entry>
bool Stack<Stack_entry> :: empty() const{
return top_node == NULL;
}
template <class Stack_entry>
Stack<Stack_entry>& Stack<Stack_entry> :: operator=(const Stack<Stack_entry>& original){
/*Node *new_top, *new_copy, *original_node = original.top_node;
if(original_node == NULL) new_top = NULL;
else{
new_top = new_copy = new Node(original_node->entry);
while(original_node->next != NULL){
original_node = original_node->next;
new_copy->next = new Node(original_node->entry);
new_copy = new_copy->next;
}
}
while(top_node != NULL) pop();
top_node = new_top;
return *this;*/
while(top_node != NULL) pop();
Node<Stack_entry> *new_copy, *original_node = original.top_node;
if(original_node == NULL) top_node = NULL;
else{
top_node = new_copy = new Node<Stack_entry>(original_node->entry);
while(original_node->next != NULL){
original_node = original_node->next;
new_copy->next = new Node<Stack_entry>(original_node->entry);
new_copy = new_copy->next;
}
}
return *this;
}
int main()
{
Stack<int> s;
s.push(1);
s.push(2);
s.push(3);
s.pop();
cout << "s " << s.top() << endl;
Stack<int> t(s);
while(!t.empty()){
cout << t.top() << endl;
t.pop();
}
return 0;
}