模拟一个数字序列压栈出栈操作,输出所有出栈数字序列

本文采用递归方式实现数字序列的压栈和出栈操作,并通过输出序列展示不同出栈顺序,包括从序列 {1, 2, 3}

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <iostream>
#include <stack>
#include <vector>
using namespace std;
static int count=0;

/**
mystack:代表堆栈
input:代表要压入堆栈的数字序列
output:代表弹出堆栈的数字序列
*/
void countSequeue(stack<int> mystack,vector<int> input,vector<int> output){	
	
	/**
	当input为空时,说明等待进栈的队列为空,展示结果:将前面出栈数字和栈中的数字打印出来
	*/
	if(input.empty()){
		for(vector<int>::iterator b=output.begin();b<output.end();b++){

			cout<<*b<<" ";
		}
		while(!mystack.empty()){
			int value=mystack.top();
			cout<<value<<" ";
			mystack.pop();
		}
		cout<<endl<<"执行次数"<<++count<<endl;
		return;
	}

	/**
	将原来的栈复制到两个栈中,给下面两个递归函数使用
    */	
	stack<int> tmp,newstack,newstack2;
	
	while(!mystack.empty()){
		tmp.push(mystack.top());
		mystack.pop();
	}
    while(!tmp.empty()){
		newstack.push(tmp.top());
		newstack2.push(tmp.top());
		tmp.pop();
	}
	/**
	将input复制一份留给下一个递归函数使用
	*/
    vector<int> tmpinput=input;
	//首先将下一个数压入堆栈中,进行迭代
	newstack.push(input.front());
	input.erase(input.begin());
	countSequeue(newstack,input,output);

	//进行下一种情况,就是先将堆栈中的数弹出,进行迭代
	if(!newstack2.empty()){
		
		output.push_back(newstack2.top());
		newstack2.pop();
		countSequeue(newstack2,tmpinput,output);
	}
	
}

int main(){
	stack<int> mystack;
	vector<int> output;
	vector<int> input;
	for(int i=0;i<3;i++)
		input.push_back(i+1);

	countSequeue(mystack,input,output);
	return 0;
}

解释:采用递归的方式模拟数字序列压栈出栈操作,能够输出搜索的出栈序列,对一个{1,2,3}的序列,出栈结果如下

3 2 1
执行次数1
2 3 1
执行次数2
2 1 3
执行次数3
1 3 2
执行次数4
1 2 3
执行次数5
Press any key to continue


在C语言中,要生成所有合法的出栈序列,通常会涉及到动态规划或回溯算法。我们先假设有一个整数数组作为的内容,你需要找出所有可能的出栈顺序,使得这些顺序可以由这个得到。 一个基本思路是递归,从最后一个元素开始遍历,尝试将每个元素弹出并添加到结果序列中。然后对剩余的继续做同样的操作。但为了防止重复,需要记录已经访问过的节点,并避免在回溯过程中再次访问它们。 下面是一个简单的伪代码示例: ```cpp #include <stdio.h> #include <vector> void printStackSequences(int arr[], int n, std::vector<int>& curr_seq, int index) { // Base case: If the stack is empty, add the current sequence to result. if (index == n) { for (int i : curr_seq) { printf("%d ", i); } puts(""); return; } // Try pushing current element into sequence and move forward curr_seq.push_back(arr[index]); printStackSequences(arr, n, curr_seq, index + 1); // Backtrack: Try next element without pushing it curr_seq.pop_back(); printStackSequences(arr, n, curr_seq, index + 1); // Index remains same because we didn't push } // Function to generate all valid stack sequences void generateAllStackSequences(int arr[], int n) { std::vector<int> seq; printStackSequences(arr, n, seq, 0); } int main() { int arr[] = {1, 2, 3}; int n = sizeof(arr) / sizeof(arr[0]); generateAllStackSequences(arr, n); return 0; } ``` 当你运行上述代码时,它会打印出所有从给定数组`arr`中合法产生的出栈序列。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WitsMakeMen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值