栈的递归调用,输出所有可能序列(82)页

/************************************************************************/
/* 李春葆数据结构习题解析b版本三版82页,参考书上代码,有改动           */
/************************************************************************/
#include <stdio.h>
#define maxsize 30
/*********************************/
/* 这里定义了一个栈以及上的操作  */
/*********************************/
struct 
{
    char data[maxsize];
    int top;
}st;
void push(char a)
{
    st.top++;
    st.data[st.top]=a;
}
char pop()
{
    char temp;
    temp=st.data[st.top];
    st.top--;
    return temp;
}
void init()
{
    st.top=-1;
}
bool empty()
{
    if (st.top==-1)
        return true;
    else
        return false;
}
int total=4;//一共四个元素
char str[]="ABCD";
int sum=0;
/*************************************************************************************************/
/* m表示未进入栈的序列的标号,a[]是从栈中输出的序列的标号,curp是a[]当前的位置即使要添加的位标号 */
/*************************************************************************************************/
void process(int m,char a[],int curp)
{
    int i;
    char x;
    if (m>total&&empty())
    {
        
        for (i=0;i<curp;i++)
        {
            printf("%c",a[i]);                
        }
        printf("\n");
        sum++;
    }
    if (m<=total)
    {
        push(str[m-1]);
        process(m+1,a,curp);
        pop();
    }
    if (!empty())
    {
        x=pop();
        a[curp]=x;
        process(m,a,curp+1);
        push(x);
    }
}
/*****************************************************************************/
/* 主函数,传递了1(进栈的序列标号),a[]出栈的字符数组,curp,输出的下一位置*/
/*****************************************************************************/
void main()
{
    char a[maxsize];
    init();
    printf("所有出栈序列\n");
    process(1,a,0);
    printf("总共有%d个可能\n",sum);
}

 

转载于:https://www.cnblogs.com/lisongfeng9213/p/3372539.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值