m0_74539947 2022-11-06 03:18 采纳率: 100%
浏览 57
已结题

顺序栈的出栈与入栈操作

#include 
using namespace std;
#define MaxSize 100
//顺序栈的初始分配空间大小
typedef int ElemType;
//假设顺序栈中所有元素为int类型
typedef struct
{
    ElemType data[MaxSize];
    //保存栈中元素
    int top;
    //栈顶指针
}SqStack;
//顺序栈类型

int Push(SqStack&st,ElemType x)//进栈元
{
    if (st.top==MaxSize-1)

        return 0;
    else
    {
        st.top++;
        st.data[st.top]=x;
        return 1;

    }
}

int StackEmpty(SqStack st)
    //判断栈是否为空
{
    
if(st.top==-1)
return 1;
else return 0;
}

    int Pop(SqStack &st,SqStack&tmpst,ElemType &x) ///st,15
    //出栈元素x
    {
        if(st.top==-1)
        //栈空
        return 0;
    
    else if(st.data[st.top]==15) //等于15的时候出栈
        
    {     
        x=st.data[st.top];
        st.top--;
        return 1;
        
    }
    
    else                //不是值为15的 出栈并且存到临时栈tmpst中
    {
        
    tmpst.data[tmpst.top]=st.data[st.top];
    st.top--;
    tmpst.top++;
    return 1;

    }

int lop(SqStack &st,ElemType &x){
if(tmpst.top==-1)
return 0;//栈空
else
x=st.data[st.top]
st.top--;
rerturn 1;
}

void expand_action()
    
    
    {    
    
        printf("初始化栈st\n");    
        SqStack st,tmpst;
        ElemType e,t;
        InitStack(st);
        InitStack(tmpst);
        printf("栈%s\n",(StackEmpty(st)==1?"空":"不空"));
        Push(st,10);
        Push(st,15);
        Push(st,20);
        Push(st,15);
        Push(st,15);
        Push(st,60);
        printf("元素10 15 20 15 15 60依次进栈st\n");
        printf("退栈所有值为15的元素\n");
        int t=15;//
        Pop(st,t);//t=15
        printf("栈%s\n",(StackEmpty(st)==1?"空":"不空"));
        printf("出栈序列为");

         while(!StackEmpty(st))
         {
             
             Pop(st,e);
             printf("%c",e);
             
         }

         
    
    }

    int main()

    {
       // 
        expand_action;
        return 0;
    }

img

不太明白怎么写 有没有老哥看看怎么弄

  • 写回答

1条回答 默认 最新

  • qzjhjxj 2022-11-06 15:22
    关注

    修改如下,供参考:

    #include <stdio.h>
    #define MaxSize 100
    //顺序栈的初始分配空间大小
    typedef int ElemType;
    //假设顺序栈中所有元素为int类型
    typedef struct
    {
        ElemType data[MaxSize];//保存栈中元素
        int top; //栈顶指针
       
    }SqStack;
    //顺序栈类型
    
    void InitStack(SqStack& st)
    {
        st.top = -1;
    }
    void DestroyStack(SqStack& st)
    {
        st.top = -1;
        printf("销毁栈\n");
    }
    int Push(SqStack& st,ElemType x)//进栈元
    {
        if (st.top==MaxSize-1)
            return 0;
        else{
            st.top++;
            st.data[st.top]=x;
            return 1;
        }
    }
    int Pop(SqStack& st,ElemType &x) //出栈
    {
        if(st.top==-1)
             return 0;//栈空
        x = st.data[st.top];
        st.top--;
        return 1;
    }
    int StackEmpty(SqStack st)  //判断栈是否为空
    {
        if(st.top==-1)
            return 1;
        else
            return 0;
    }
    int lop(SqStack& st, ElemType x) //删除所有为 x 的元素
    {
        SqStack tmpst;//临时栈 tmpst
        ElemType e;
        InitStack(tmpst);
        if(st.top==-1)//栈空
            return 0;
        else{
            while(!StackEmpty(st))
            {
                Pop(st,e);
                if (e != x)
                    Push(tmpst,e);
            }
            while(!StackEmpty(tmpst))
            {
                Pop(tmpst,e);
                Push(st,e);
            }
        }
        DestroyStack(tmpst);//销毁临时栈
        return 1;
    }
    
    void expand_action()
    {
        SqStack st;        //,tmpst;
        ElemType e,t;      //InitStack(tmpst);
        printf("初始化栈st\n");
        InitStack(st);
        printf("栈%s\n",(StackEmpty(st)==1?"空":"不空"));
        Push(st,1);
        Push(st,2);
        Push(st,2);
        Push(st,1);
        Push(st,2);
        Push(st,3);
        printf("元素1,2,2,1,2,3依次进栈\n");
    
        printf("删除所有元素2\n");
        lop(st,2);
    
        printf("出栈序列:");
        while(!StackEmpty(st))
        {
            Pop(st,e);
            printf("%d",e);
        }
    }
    int main()
    {
        expand_action();  //expand_action
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
    1人已打赏

报告相同问题?

问题事件

  • 系统已结题 11月14日
  • 已采纳回答 11月6日
  • 创建了问题 11月6日

悬赏问题

  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型