栈顺序存储结构的C++模板类程序源代码
2016-05-31 21:38
375 查看
栈顺序存储结构利用一个大数组,相当于简化了的线性表,线性表具有查找、插入、删除等功能,而栈则简化为只包括压入、弹出这种更有针对性的功能。下面是自己写的栈的顺序存储C++模板类源代码:
//linearstack.h
#ifndef LINEARSTACK
#define LINEARSTACK
#include <IOSTREAM>
const int ROOMSIZE=100;
template<class Type>
class LinearStack
{
private:
Type Data[ROOMSIZE];
int top;
public:
LinearStack():top(0){};
~LinearStack(){};
bool Push(Type temp);
Type Pop();
bool IsFull();
bool IsEmpty();
void Print();
};
template<class Type>
bool LinearStack<Type>::Push(Type temp)
{
if(IsFull())
return false;
Data[top++]=temp;
return true;
}
template<class Type>
Type LinearStack<Type>::Pop()
{
if(IsEmpty())
return Type(-111);
top--;
return Data[top];
}
template<class Type>
bool LinearStack<Type>::IsFull()
{
if(top==ROOMSIZE)
return true;
else
return false;
}
template<class Type>
bool LinearStack<Type>::IsEmpty()
{
if(top==0)
{
return true;
}
else
return false;
}
template<class Type>
void LinearStack<Type>::Print()
{
for(int i=0;i<top;i++)
{
std::cout<<Data[i]<<" ";
}
std::cout<<std::endl;
}
#endif
//linearstack.h
#ifndef LINEARSTACK
#define LINEARSTACK
#include <IOSTREAM>
const int ROOMSIZE=100;
template<class Type>
class LinearStack
{
private:
Type Data[ROOMSIZE];
int top;
public:
LinearStack():top(0){};
~LinearStack(){};
bool Push(Type temp);
Type Pop();
bool IsFull();
bool IsEmpty();
void Print();
};
template<class Type>
bool LinearStack<Type>::Push(Type temp)
{
if(IsFull())
return false;
Data[top++]=temp;
return true;
}
template<class Type>
Type LinearStack<Type>::Pop()
{
if(IsEmpty())
return Type(-111);
top--;
return Data[top];
}
template<class Type>
bool LinearStack<Type>::IsFull()
{
if(top==ROOMSIZE)
return true;
else
return false;
}
template<class Type>
bool LinearStack<Type>::IsEmpty()
{
if(top==0)
{
return true;
}
else
return false;
}
template<class Type>
void LinearStack<Type>::Print()
{
for(int i=0;i<top;i++)
{
std::cout<<Data[i]<<" ";
}
std::cout<<std::endl;
}
#endif
相关文章推荐
- C++基于栈实现铁轨问题
- C语言栈的表示与实现实例详解
- C语言实现颠倒栈的方法
- 算法系列15天速成 第十天 栈
- 一看就懂:图解C#中的值类型、引用类型、栈、堆、ref、out
- Array栈方法和队列方法的特点说明
- java数据结构之java实现栈
- 浅析栈区和堆区内存分配的区别
- 用Java代码实现栈数据结构的基本方法归纳
- 用PHP解决的一个栈的面试题
- 浅谈C#中堆和栈的区别(附上图解)
- JavaScript数据结构与算法之栈详解
- 浅析顺序结构存储的栈
- C语言之栈和堆(Stack && Heap)的优缺点及其使用区别
- Python实现栈的方法
- JavaScript数组的栈方法与队列方法详解
- JavaScript数据结构与算法之栈与队列
- JavaScipt中栈的实现方法
- Java中堆和栈的区别详解
- 详解Java的堆内存与栈内存的存储机制