const int StackSize = 10;
template <class T>
class SeqStack
{
public:
SeqStack(){top = -1;};
~SeqStack(){}
void Push(T x);
T Pop();
T GetTop();
bool Empty();
private:
T data[StackSize];
int top;
};
|
template <class T>
void SeqStack<T>::Push(T x)
{
if (top == StackSize-1) throw "上溢";
top ++;
data[top] = x;
}
|
template <class T>
T SeqStack<T>::Pop()
{
if (top == -1) throw "下溢";
T x = data[top--];
return x;
}
|
template <class T>
T SeqStack<T>::GetTop()
{
if (top != -1) return data[top];
}
|
template <class T>
bool SeqStack<T>::Empty()
{
if (top == -1) return 1;
else return 0;
}
|
const StackSize = 100;
template <class T>
class BothStack
{
public:
BothStack(){top1 = -1;top2 = StackSize;};
~BothStack(){}
void Push(int i, T x);
T Pop(int i);
T GetTop(int i);
bool Empty(int i);
private:
T data[StackSize];
int top1,top2;
};
|
template <class T>
void BothStack<T>::Push(int i, T x)
{
if (top1 == top2-1) throw "上溢";
if (i == 1) data[++top1] = x;
if (i == 2) data[--top2] = x;
}
|
template <class T>
T BothStack<T>::Pop(int i)
{
if (i == 1)
{
if (top1 == -1) throw "下溢";
return data[top1--];
}
if (i == 2)
{
if (top2 == StackSize) throw "下溢";
return data[top2++];
}
}
|
template <class T>
struct Node
{
T data;
Node<T> *next;
};
template <class T>
class LinkStack
{
public:
LinkStack(){top = NULL};
~LinkStack();
void Push(T x);
T Pop();
T GetTop();
bool Empty();
private:
Node<T> * top;
};
|
template <class T>
void LinkStack<T>::Push(T x)
{
Node<T> *s = new Node<T>;
s->data = x;
s->next = top;
top = s;
}
|
template <class T>
T LinkStack<T>::Pop()
{
if (top == NULL) throw "下溢";
T x = top->data;
Node<T> *p = top;
top = top->next;
delete p;
return x;
}
|
template <class T>
LinkStack<T>::~LinkStack()
{
while (top)
{
Node<T> *p = top->next;
delete top;
top = p;
}
}
|
#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;
const int capacity = 30;
template <class T>
class Stack
{
public:
Stack(){pool.reserve(capacity);}
void clear(){pool.clear();}
bool isEmpty() const{return pool.empty();}
T& topEL(){return pool.back();}
T pop()
{
T el = pool.back();
pool.pop_back();
return el;
}
void push(const T& el)
{pool.push_back(el);}
private:
vector<T> pool;
};
|
#include "stdafx.h"
#include <iostream>
#include <list>
using namespace std;
template <class T>
class LLStack
{
public:
LLStack(){}
void clear(){lst.clear();}
bool isEmpty() const{return lst.empty();}
T& topEL(){return lst.back();}
T pop()
{
T el = lst.back();
lst.pop_back();
return el;
}
void push(const T& el)
{lst.push_back(el);}
private:
list<T> lst;
};
|
标准模板库中的栈
stack<int> stack1;//默认为双端队列
stack<int,vector<int>> stack2;//向量/*在VS2005环境下*/
stack<int,list<int>> stack3;//链表/*在VS2005环境下*/
#include <iostream> #include <stack> using namespace std; int main() { stack<int> stack1;//stack()创建一个空栈 stack1.push(1);//void push(const T& el)将el插入到栈的顶端
stack1.push(3); while (!stack1.empty())//bool empty()const 如果栈为空则返回true,否则返回false { cout << "栈顶元素:" << stack1.top() << endl;//T& top()/const T& top()const 返回栈的栈顶元素 cout << "栈中元素数目:" << stack1.size() << endl;//size_type size()const 返回栈中的元素数目 stack1.pop(); }
return 0; } |
#include "stdafx.h" #include <iostream> #include <stack> #include <vector> #include <list>
using namespace std; int main() { stack<int> stack1; stack<int,vector<int>> stack2; stack<int,list<int>> stack3; stack1.push(1); stack1.push(2); stack1.push(3); stack2.push(4); stack2.push(5); stack2.push(6); stack3.push(7); stack3.push(8); stack3.push(9); stack1.push(stack2.top()); stack2.push(stack3.top()); while (!stack1.empty()) { cout << "stack1栈顶元素:" << stack1.top() << " "; stack1.pop(); cout << endl; } while (!stack2.empty()) { cout << "stack2栈顶元素:" << stack2.top() << " "; stack2.pop(); cout << endl; } while (!stack3.empty()) { cout << "stack3栈顶元素:" << stack3.top() << " "; stack3.pop(); cout << endl; } return 0; } |