/*
设以整数序列1,2,3,4作为栈S的输入,利用push(进栈)和pop(出栈)操作,写出所有可能的输出编程实现算法。
*/
#include <stdio.h>
#define MaxSize 10
using namespace std;
struct stacknode
{
int data[MaxSize];
int top;
} st;
int total=4;
void initstack()
{
st.top=-1;
}
void push(int n)
{
st.top++;
st.data[st.top]=n;
}
int pop()
{
int temp;
temp=st.data[st.top];
st.top--;
return temp;
}
bool empty()
{
if(st.top==-1)
return true;
else
return false;
}
void process(int pos,int path[],int curp)
{
int m,i;
if(pos<=total)
{
push(pos);
process(pos+1,path,curp);
pop();
}
if(!empty())
{
m=pop();
path[curp]=m;
curp++;
process(pos,path,curp);
push(m);
}
if(pos>total&&empty())
{
printf(" ");
for(i=0; i<curp; i++)
printf("%d",path[i]);
printf("\n");
}
}
int main()
{
int path[MaxSize];
initstack();
printf("所有输出序列:\n");
process(1,path,0);
return 0;
}
顺序栈算法
最新推荐文章于 2022-04-11 20:25:14 发布