堆栈(栈stack)的实现和基本用法(一)

个人网站http://www.ravedonut.com/

栈 (stack)又称堆栈,是一种受限制的线性表,其限制是只允许在表的一端进行插入和删除。

允许操作的一端称为栈顶(top),不允许 操作的称为栈底(bottom),每每次删除的数据元素总是最后插入的数据元素,所以栈又称为“后入先出表”。

栈的储存结构有2种:一种顺序储存结构(顺序栈),一种链式储存结构(链式栈)。

今天主要来看看如何实现一个栈的功能

首先,栈的基本功能有:

1.       empty 判断堆栈是否为空

2.       pop   向堆栈里面压入一个数据

3.       push        向堆栈压入一个数据

4.       size          返回当前堆栈长度(即内部数据个数)

5.       top           得到堆栈栈顶数据

实现:

1.利用之前的vector来实现:

stack1.h文件

#pragma once
#include <vector>
using namespace std;

template <class Object>
class Stack
{
public:
    Stack(void);
    bool isEmpty() const;
    const Object & top() const;
    void makeEmpty();
    void pop();
    void push(const Object & x);
    Object topAndPop();
    ~Stack();

private:
    vector<Object> theArray;
    int topOfStack;
};

stack1.cpp

#include "stack1.h"
//利用vector

template <class Object>
Stack<Object>::Stack():theArray(1)
{
    topOfStack=-1;
}


template <class Object>
bool Stack<Object>::isEmpty() const
{
    return topOfStack==-1;
}


template <class Object>
void Stack<Object>::makeEmpty()
{
    this->topOfStack=-1;
}


template <class Object>
void Stack<Object>::push(const Object & x)
{
    if(topOfStack==theArray.size()-1)
        theArray.resize(theArray.size()*21);
    theArray[++topOfStack]=x;
}


template <class Object>
const Object & Stack<Object>::top() const
{
    if(isEmpty())
        throw underflow_error( "堆栈已经为空" );
    return theArray[topOfStack];
}


template <class Object>
void Stack<Object>::pop()
{
    if(isEmpty())
        throw underflow_error( "堆栈已经为空" );
    topOfStack--;
}


template <class Object>
Object Stack<Object>::topAndPop()
{
    if(isEmpty())
        throw underflow_error( "堆栈已经为空" );
    return theArray[topOfStack--];
}


template <class Object>
Stack<Object>::~Stack()
{
}

2.利用数组实现:

stack2.h

#pragma once 
typedef int DataType; 
const int MaxStatckSize = 50; //栈大小  
class StackDemo 
{ 
private: 
    DataType stacklist[MaxStatckSize]; 
    int top;//栈顶 
public: 
    //构造函数 
    StackDemo(void); 
    ~StackDemo(void); 
public: 
    //压栈出栈操作 
    void Push(const DataType &item); 
    DataType Pop(void); 
    void ClearStack(void); 

    //访问栈顶 
    DataType Peek(void)const; 

    //检测椎栈 
    bool isEmpty(void)const; 
    bool isFull(void)const; 
};

stack2.cpp

#include "stack2.h" 
#include <iostream> 

StackDemo::StackDemo(void) 
{ 
    this->top = -1; 
} 


StackDemo::~StackDemo(void) 
{ 
    this->top = -1; 
} 

void StackDemo::Push(const DataType &item) 
{ 
    //栈是否已满 
    if(!isFull()) 
    {   top += 1; 
    this->stacklist[top] = item; 

    } 
    else 
        std::cout << "Out of the Stack!" << std::endl; 
} 

DataType StackDemo::Pop(void) 
{ 
    if(!isEmpty()) 
    {      
        int ebp = top; 
        top -= 1; 
        return stacklist[ebp]; 
    } 
    else 
        return -1; 
} 

DataType StackDemo::Peek(void)const
{ 
    return top; 
} 

void StackDemo::ClearStack() 
{ 
    for(int i = top;i >=0 ;i--) 
        stacklist[i] = 0; 
    top = -1; 
    std::cout << "Clear stack done!" << std::endl; 
} 

bool StackDemo::isFull(void)const
{ 
    return top > MaxStatckSize?true:false; 
} 

bool StackDemo::isEmpty(void)const
{ 
    return top < 0 ?true:false; 
}

 

posted @ 2012-06-20 16:10  狂吼的面包圈  阅读(12375)  评论(1编辑  收藏  举报