用两种方法实现栈---顺序表和链表
2016-08-06 11:37
423 查看
栈是一种先进后出的数据结构,栈中的数据是先进后出的(First In Last Out, FILO)。栈只有一个出口,允许新增元素(只能在栈顶上增加)、移出元素(只能移出栈顶元素)、取得栈顶元素等操作。在STL中,栈是以别的容器作为底部结构,再将接口改变,使之符合栈的特性就可以了。栈里面常用的函数有push()(增),pop()(删),size()(求大小),top()(顶部)和empty()(空),
下面是用顺序表实现栈的各种函数
顺序表代码实现:#include <iostream>
#include <assert.h>
using namespace std;
template<typename T>
class Stack
{
public:
Stack()
:_a(NULL)
, _top(0)
, _capacity(0)
{}
~Stack()
{
if (_a)
{
delete[] _a;
}
}
public:
void _CheckCapacity()
{
if (_a == NULL)
{
_capacity = 3;
_a = new T[_capacity];
return;
}
if (_top == _capacity)
{
_capacity *= 2;
T* tmp = new T[_capacity];
for (size_t i = 0; i < _top; i++)
{
tmp[i] = _a[i];
}
delete[] _a;
_a = tmp;
}
}
void Push(const T& x)
{
_CheckCapacity();
_a[_top++] = x;
}
void Pop()
{
assert(_top > 0);
--_top;
}
size_t Size()
{
return _top;
}
bool Empty()
{
return _top == 0;
}
T& Top()
{
return _a[_top];
}
private:
T* _a;
size_t _top;
size_t _capacity;
};
链表实现栈:
链表实现代码:
测试代码:
test.cppvoid Test()
{
Stack<int> s1;
s1.Push(0);
s1.Push(1);
s1.Push(2);
s1.Push(3);
s1.Push(4);
s1.Push(5);
s1.Pop();
s1.Pop();
s1.Pop();
int top = s1.Top();
size_t size = s1.Size();
cout << s1.Empty() << endl;
}
int main()
{
Test();
system("pause");
return 0;
}
下面是用顺序表实现栈的各种函数
顺序表代码实现:#include <iostream>
#include <assert.h>
using namespace std;
template<typename T>
class Stack
{
public:
Stack()
:_a(NULL)
, _top(0)
, _capacity(0)
{}
~Stack()
{
if (_a)
{
delete[] _a;
}
}
public:
void _CheckCapacity()
{
if (_a == NULL)
{
_capacity = 3;
_a = new T[_capacity];
return;
}
if (_top == _capacity)
{
_capacity *= 2;
T* tmp = new T[_capacity];
for (size_t i = 0; i < _top; i++)
{
tmp[i] = _a[i];
}
delete[] _a;
_a = tmp;
}
}
void Push(const T& x)
{
_CheckCapacity();
_a[_top++] = x;
}
void Pop()
{
assert(_top > 0);
--_top;
}
size_t Size()
{
return _top;
}
bool Empty()
{
return _top == 0;
}
T& Top()
{
return _a[_top];
}
private:
T* _a;
size_t _top;
size_t _capacity;
};
链表实现栈:
链表实现代码:
#include <iostream> #include <assert.h> using namespace std; template<typename T> struct Node { Node(const T& x) :_data(x) ,_next(NULL) {} T _data; Node<T>* _next; }; template<typename T> class Stack { public: Stack() :_top(NULL) ,_base(NULL) ,_size(0) {} ~Stack() {} void Push(const T& x) { if (_top == NULL) { _top = new Node<T>(x); _base = _top; } else { _top->_next = new Node<T>(x); _top = _top->_next; } ++_size; } void Pop() { assert(_top); Node<T>* cur = _base; while (cur->_next != _top) { if (cur == NULL) { printf("Stack is empty!\n"); return; } cur = cur->_next; } delete _top; _top = cur; --_size; } T& Top() { return _top->_data; } bool Empty() { return _top == NULL; } size_t Size() { return _size; } private: Node<T>* _top; Node<T>* _base; size_t _size; };
测试代码:
test.cppvoid Test()
{
Stack<int> s1;
s1.Push(0);
s1.Push(1);
s1.Push(2);
s1.Push(3);
s1.Push(4);
s1.Push(5);
s1.Pop();
s1.Pop();
s1.Pop();
int top = s1.Top();
size_t size = s1.Size();
cout << s1.Empty() << endl;
}
int main()
{
Test();
system("pause");
return 0;
}
相关文章推荐
- ByteBuffer的心得
- caffe——cifar10模型训练自己的数据
- ubuntu下安装ros出现“无法下载-package.ros.org中某个包-校验和不符”的解决方法
- Groovy
- 理解一般指针和指向指针的指针
- Linux下的C语言学习笔记(3)
- 量化分析师的Python日记【第4天:一大波金融Library来袭之scipy篇】
- Hdu 3694 Fermat Point in Quadrangle(三分)
- Unity触摸操作
- 关于 loopj/android-async-http java.net.SocketTimeoutException 错误
- 将博客搬至CSDN
- 如何使用 SQL Developer 导出数据
- 20145202马超 《Java程序设计》第九周学习总结
- Javascript算法练习(七)
- UGUI之Text对齐至格子
- 【DRP】——servlet
- 【代码积累】xml文件读取并输出
- MySQL笔记-数据库约束条件 与 主键重排序
- poj2367Genealogical tree
- iOS UI篇16- 图片压缩方法