顺序栈和链栈的部分功能完整代码

一、顺序栈代码

#include<iostream>
#include<stdlib.h>
using namespace std;
#define OK 1
#define ERROR 0
#define MAXSIZE 100
typedef int ElemType;
typedef int Status;
typedef struct 
{
    ElemType *elem;
    int top;
}Sqstack;
Status InitStack(Sqstack &S);
Status Push(Sqstack &S,ElemType e);
Status Pop(Sqstack &S,ElemType &e);
Status GetPop(Sqstack S,ElemType &e);
Status StackEmpty(Sqstack S);
void Show(Sqstack S);
//初始化
Status InitStack(Sqstack &S)
{
    S.elem=new ElemType[MAXSIZE];
    if(!S.elem) return ERROR;
    S.top =-1;
    return OK;
 } 
//入栈
Status Push(Sqstack &S,ElemType e)
{
    if(S.top ==MAXSIZE-1)  return ERROR;
    S.top++;
    S.elem[S.top]=e;
    return OK; 
 } 
//出栈
Status Pop(Sqstack &S,ElemType &e)
{
    if(S.top==-1) return ERROR;
    e=S.elem[S.top];
    S.top--;
    return OK;

//取栈顶元素
Status GetPop(Sqstack S,ElemType &e)
{
    if(S.top==-1) return ERROR;
    e=S.elem [S.top];
    return OK;
 } 
//判断栈是否为空 
Status StackEmpty(Sqstack S)
{
    if(S.top==-1)
    return OK;
    else
    return ERROR;
}
//输出元素
void Show(Sqstack S)
{
    if(S.top==-1)
    {
        cout<<"没有数据"<<endl;
        return ;
    }
    while(S.top!=-1)
    {
        cout<<S.elem[S.top]<<" ";
        S.top--;
    }
    cout<<endl;
 } 

int main()
{
    Sqstack S;
    ElemType e;
    int choice;
    while(1)
    {
        cout<<"1、初始化  2、入栈  3、出栈  4、显示"<<endl;
        cout<<"5、判断栈空  6、取栈顶元素  0、退出"<<endl;
        cout<<"输入选项=";
        cin>>choice;
        switch(choice)
        {
        case 1:
            InitStack(S);
            cout<<"初始化成功!"<<endl;
            break;
        case 2:
            cout<<"入栈数据=";
            cin>>e;
            if(Push(S,e)==OK)
            cout<<"输入成功!"<<endl;
            else 
            cout<<"输入失败!"<<endl;
            break;
        case 3:
               if(Pop(S,e)==OK)
               {
                   cout<<"出栈成功!"<<endl;
                   cout<<"出栈数据="<<e<<endl;
               }
            else
            cout<<"出栈失败!"<<endl;
            break;
        case 4:
            Show(S);
            break;
        case 5:
             if(StackEmpty(S)==OK)
                 cout<<"栈空!"<<endl;
            else 
                cout<<"栈非空!"<<endl;
            Show(S);
            break;
        case 6:
            if(GetPop(S,e)==OK)
            {
                cout<<"栈顶元素="<<e<<endl; 
            }
            else
            cout<<"栈空!"<<endl;
            break;
        case 0:
            return 0;
        default:
            cout<<"选项输入错误!"<<endl;
        } 
        system("pause");
        system("cls");
    }
    return 0;
}

二、链栈代码

#include<iostream>
#include<stdlib.h>
using namespace std;
#define OK 1
#define ERROR 0
typedef int ElemType;
typedef int Status;
//定义存储结构 
typedef struct Node
{
    ElemType data;
    struct Node *next;
}StackNode,*LinkStack;
Status Initstack(LinkStack &S);
Status Push(LinkStack &S,ElemType e);
Status Pop(LinkStack &S,ElemType &e);
Status GetPop(LinkStack S,ElemType &e);
Status StackEmpty(LinkStack S);
void Show(LinkStack S);
//初始化
Status Initstack(LinkStack &S)
{
    S=NULL;
    return OK;
 } 
//入栈(头插法) 
Status Push(LinkStack &S,ElemType e)
{
    StackNode *p;
    p=new StackNode ;
    if(!p) return ERROR;
    p->data=e;
    p->next=S;
    S=p;
    return OK;
 } 
//出栈 
Status Pop(LinkStack &S,ElemType &e)
{
    StackNode *p;
    if(S==NULL) return ERROR;
    e=S->data;
    p=S;
    S=S->next;
    delete p;
    return OK;
}
//取栈顶元素 
Status GetPop(LinkStack S,ElemType &e)
{
    if(S==NULL) return ERROR;
    e=S->data;
    return OK;
}
//判断栈是否为空 
Status StackEmpty(LinkStack S)
{
    if(S==NULL)
    return OK;
    else 
    return ERROR;
}
//输出函数 
void Show(LinkStack S)
{
    StackNode *p;
    p=S;
    if(S==NULL) 
    {
        cout<<"没有数据!"<<endl;
        return ;
    }
    while(p)
    {
    cout<<p->data<<" ";
    p=p->next;
    }
    cout<<endl;
}
int main()
{
    LinkStack S;
    ElemType e;
    int choice;
    while(1)
    {
        cout<<"1、初始化  2、入栈  3、出栈  4、显示"<<endl;
        cout<<"5、判断栈空  6、取栈顶元素  0、退出"<<endl;
        cout<<"输入选项=";
        cin>>choice;
        switch(choice)
        {
        case 1:
            Initstack(S);
            cout<<"初始化成功!"<<endl;
            break;
        case 2:
            cout<<"入栈数据=";
            cin>>e;
            if(Push(S,e)==OK)
            cout<<"输入成功!"<<endl;
            else 
            cout<<"输入失败!"<<endl;
            break;
        case 3:
               if(Pop(S,e)==OK)
               {
                   cout<<"出栈成功!"<<endl;
                   cout<<"出栈数据="<<e<<endl;
               }
            else
            cout<<"出栈失败!"<<endl;
            break;
        case 4:
            Show(S);
            break;
        case 5:
             if(StackEmpty(S)==OK)
                 cout<<"栈空!"<<endl;
            else 
                cout<<"栈非空!"<<endl;
            Show(S);
            break;
        case 6:
            if(GetPop(S,e)==OK)
            {
                cout<<"栈顶元素="<<e<<endl; 
            }
            else
            cout<<"栈空!"<<endl;
            break;
        case 0:
            return 0;
        default:
            cout<<"选项输入错误!"<<endl;
        } 
        system("pause");
        system("cls");
    }
    return 0;
}
 

  • 24
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值