顺序栈的实现例程
2006-11-13 14:00
267 查看
说明:一直对栈似是而非的理解,因此写出其实现,但是就这个简单的程序中,我发现存在着很多缺陷,谁有兴趣帮我更正,本人非常感谢。另外我发现仅就顺序栈而言,都存在着多个变种:)
////////-----------Stack_Seq_SelfDefine.h-----------///////
#include <iostream>
using namespace std;
//-----------------------------------------//
// Stack -- Sequence, SelfDefine
// SuperXu 2006.11
// Version: Final
//-----------------------------------------//
class SSS {
private:
static const int INITSIZE;
static const int INCREAMENT;
int* m_base;
int* m_top;
int m_stack_size;
public:
SSS(int sz = INITSIZE) : m_stack_size(sz) {
m_base = new int[sz];
m_top = m_base;
}
SSS(const SSS& sss) {
int* new_base = new int[sss.m_stack_size];
int* p = sss.m_base;
m_base = new_base;
while (p < sss.m_top)
{
*new_base = *p;
++new_base;
++p;
}
m_top = new_base;
}
SSS& operator =(const SSS& sss) {
if (this == &sss)
return *this;
delete m_base;
int* new_base = new int[sss.m_stack_size];
int* p = sss.m_base;
m_base = new_base;
while (p < sss.m_top)
{
*new_base = *p;
new_base++;
p++;
}
m_top = new_base;
return *this;
}
~SSS() {
delete[] m_base;
}
bool isEmpty() const {
return (m_base == m_top);
}
int getTop() const
{
if (m_top == m_base)
return -1;
return *(m_top-1);
}
void push(const int& value);
int pop();
int Number() const {
return (m_top - m_base);
}
void display() const
{
int* p = m_top;
while (p != m_base)
{
p--;
cout << *p << " ";
}
cout << endl;
}
};
///////--------Stack_Seq_SelfDefine.cpp--------////////
#include "Stack_Seq_SelfDefine.h"
const int SSS::INITSIZE = 10;
const int SSS::INCREAMENT = 5;
void SSS::push(const int& value)
{
if (m_top - m_base >= m_stack_size) //increase memory
{
int* new_base = new int[m_stack_size + INCREAMENT];
int* p = m_base;
m_base = new_base;
while (p < m_top)
{
*new_base = *p;
new_base++;
p++;
}
m_top = new_base;
m_stack_size += INCREAMENT;
}
*m_top = value;
m_top++;
}
int SSS::pop()
{
if (m_base == m_top)
return -1;
return *(--m_top);
}
////////-----------Stack_Seq_SelfDefine.h-----------///////
#include <iostream>
using namespace std;
//-----------------------------------------//
// Stack -- Sequence, SelfDefine
// SuperXu 2006.11
// Version: Final
//-----------------------------------------//
class SSS {
private:
static const int INITSIZE;
static const int INCREAMENT;
int* m_base;
int* m_top;
int m_stack_size;
public:
SSS(int sz = INITSIZE) : m_stack_size(sz) {
m_base = new int[sz];
m_top = m_base;
}
SSS(const SSS& sss) {
int* new_base = new int[sss.m_stack_size];
int* p = sss.m_base;
m_base = new_base;
while (p < sss.m_top)
{
*new_base = *p;
++new_base;
++p;
}
m_top = new_base;
}
SSS& operator =(const SSS& sss) {
if (this == &sss)
return *this;
delete m_base;
int* new_base = new int[sss.m_stack_size];
int* p = sss.m_base;
m_base = new_base;
while (p < sss.m_top)
{
*new_base = *p;
new_base++;
p++;
}
m_top = new_base;
return *this;
}
~SSS() {
delete[] m_base;
}
bool isEmpty() const {
return (m_base == m_top);
}
int getTop() const
{
if (m_top == m_base)
return -1;
return *(m_top-1);
}
void push(const int& value);
int pop();
int Number() const {
return (m_top - m_base);
}
void display() const
{
int* p = m_top;
while (p != m_base)
{
p--;
cout << *p << " ";
}
cout << endl;
}
};
///////--------Stack_Seq_SelfDefine.cpp--------////////
#include "Stack_Seq_SelfDefine.h"
const int SSS::INITSIZE = 10;
const int SSS::INCREAMENT = 5;
void SSS::push(const int& value)
{
if (m_top - m_base >= m_stack_size) //increase memory
{
int* new_base = new int[m_stack_size + INCREAMENT];
int* p = m_base;
m_base = new_base;
while (p < m_top)
{
*new_base = *p;
new_base++;
p++;
}
m_top = new_base;
m_stack_size += INCREAMENT;
}
*m_top = value;
m_top++;
}
int SSS::pop()
{
if (m_base == m_top)
return -1;
return *(--m_top);
}
相关文章推荐
- 单链表的顺序实现例程[动态分配存储空间]
- 单链表的顺序实现例程[静态分配存储空间]
- 顺序表(线性表的顺序存储结构)及C语言实现
- DS之顺序栈和链队实现回文判断
- java实现顺序栈
- 顺序栈的实现
- 顺序表的动态实现
- 5.基于顺序栈 实现 检测括号匹配程序
- 线性表的顺序实现
- mysql 实现按 where in () 中的顺序排序,用find_in_set() 函数
- (学习java)顺序循环队列的简易实现
- 大量 vcproj 构建顺序管理工具的实现(一)
- 数据结构第一天、线性表的顺序表示与实现
- C++实现最小堆及插入,调整顺序,删除堆顶元素的操作
- 实现LoadRunner多个场景的顺序执行
- 数据结构-线性表的顺序存储实现及各种操作代码
- 顺序线性表的实现及操作(C语言实现)
- 数字转字母顺序 JS实现
- 指针变量作为函数参数实现对输入的两个整数按大小顺序输出
- 数据机构(10)--顺序循环队列的实现及相关操作