数据结构算法--堆栈

/**
//    //
//   堆栈数据结构   stack.h      
// 
/**
#include<iostream.h>
template<class Type>class Stack;

template<class Type>
class StackNode
{
    friend class Stack<Type>;
private:
    Type data;
    StackNode<Type> *link;
    StackNode(Type D=0,StackNode<Type> *L=NULL):link(L),data(D)......{}
};

template<class Type>
class Stack
{
public:
    Stack():top(NULL),NumItem(0)......{}
    void Push(Type item);
    Type Pop();
    Type GetTop();
    void MakeEmpty();
    bool ISEmpty();
    int GetNum();
private:
    int NumItem;
    StackNode<Type> *top;
};

template<class Type>
void Stack<Type>::Push(Type item)
{
    top=new StackNode<Type>(item,top);
    NumItem++;
}

template<class Type>
Type Stack<Type>::Pop()
...{
    StackNode<Type> *p;
    Type temp;
    temp=top->data;
    p=top;
    top=top->link;
    delete p;
    NumItem--;
    return temp;
}

template<class Type>
Type Stack<Type>::GetTop()
{
    return top->data;
}

template<class Type>
bool Stack<Type>::ISEmpty()
...{
    return top==NULL;
}

template<class Type>
void Stack<Type>::MakeEmpty()
{
    delete top;
}

template<class Type>
int Stack<Type>::GetNum()
{
    return NumItem;
}

/**//
//    //
//   堆栈功能函数   Stack.cpp/ /
//    //
/**/
#include"Stack.h"
#include"iostream.h"

const int INT =13;
const double FLOAT= 13.33;
const char CHAR ='a';

template<class Type>
void Stack_Push(Stack<Type> &StackOPP)
{
    cout<<"请输入要插入的数据项: ";
    Type item;
    cin>>item;
    StackOPP.Push(item);
}

template<class Type>
void Stack_Pop(Stack<Type> &StackOPP)
{
    if(!StackOPP.ISEmpty())
    {
        cout<<"出栈数据项: ";
        cout<<StackOPP.Pop()<<endl;
    }
    else
    {
        cout<<"堆栈已经为空!"<<endl;
    }
}

template<class Type>
void Stack_ISEmpty(Stack<Type> &StackOPP)
{
    if(!StackOPP.ISEmpty())
        cout<<"堆栈不空,还有"<<StackOPP.GetNum()<<"数据项!"<<endl;
    else
        cout<<"堆栈为空!"<<endl;
}

template<class Type>
void Stack_GetTop(Stack<Type> &StackOPP)
{
    if(!StackOPP.ISEmpty())
        cout<<"栈顶元素为:"<<StackOPP.GetTop()<<endl;
    else
        cout<<"堆栈为空!"<<endl;
}

template<class Type>
void Stack_MakeEmpty(Stack<Type> &StackOPP)
{
    if(!StackOPP.ISEmpty())
    {
        StackOPP.MakeEmpty();
        cout<<"堆栈已经销毁!"<<endl;
    }
    else
    {
        cout<<"销毁失败!"<<endl;
    }
}

template<class Type>
void StackINI(Type temp)
{
    Stack<Type> StackOPP;
    do
    {
        cout<<"堆栈的操作: "<<endl
            <<" 1) 插入堆栈"<<endl
            <<" 2) 出栈"<<endl
            <<" 3) 堆栈是否为空"<<endl
            <<" 4) 得栈顶数据项"<<endl
            <<" 5) 销毁堆栈"<<endl
            <<" X) 退出堆栈操作"<<endl;
        int item;
        cin>>item;
        switch(item)
        {
        case 1: Stack_Push(StackOPP); break;
        case 2: Stack_Pop(StackOPP);  break;
        case 3: Stack_ISEmpty(StackOPP);  break;
        case 4: Stack_GetTop(StackOPP); break;
        case 5: Stack_MakeEmpty(StackOPP); break;
        default: return ;
        }
    }while(true);
}

void STACK()
{
    int item;
    cout<<"清选择数据类型: 1) 整型 2) 浮点型 3) 字符型 X) 退出: ";

    cin>>item;
    switch(item)
   {
        case 1: StackINI(INT); break; //根据不同的用户需要选择数据类型
        case 2: StackINI(FLOAT); break;
        case 3: StackINI(CHAR); break;
        default: return ; break;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值