stack逆序和排序

// 两个斩实现一个队列
// 两个队列实现一个栈

// 用递归颠倒一个栈
//void reverse(stack<int>&s, int *p)
//{
//	int t = s.top();
//	s.pop();
//	reverse(s, t);
//}

#include <iostream>
using namespace std;
#include <stack>

// 递归 栈元素的排序.最小值在top
void order_stack2(stack<int> &st)
{
	if(st.empty())
		return;
	int tmp = st.top();
	st.pop();
	if(!st.empty())
	{
		order_stack2(st);
		int tmp2 = st.top();
		if (tmp > tmp2)
		{
			st.pop();
			st.push(tmp);
			st.push(tmp2);
			return;
		}
	}
	st.push(tmp);
}
void order_(stack<int> &st)
{
	if (st.empty())
		return;
	order_stack2(st);
	int tmp = st.top();
	st.pop();
	order_(st);
	st.push(tmp);
}

// 非递归 栈元素的排序.
// 利用两个辅助栈.以下排序结果,top()是最小元素。
// p1永远指向第一个辅助栈,p2用来倒腾
void order_stack(stack<int> &st)
{
	if (st.empty())
		return;
	stack<int> st1;
	
	while(! st.empty())
	{
		int t1 = st.top();
		st.pop();
		if (st1.empty() || t1 > st1.top())
		{
			st1.push(t1);
		}
		else
		{
			stack<int> st2;
			while (!st1.empty() && t1 <= st1.top())
			{
				st2.push(st1.top());
				st1.pop();
			}
			st1.push(t1);
			while (!st2.empty())
			{
				st1.push(st2.top());
				st2.pop();
			}
		}
	}

	// 
	while (!st1.empty())
	{
		st.push(st1.top());
		st1.pop();
	}

}

void print_stack(stack<int> s)
{
	while(! s.empty())
	{
		cout<<s.top()<<" ";
		s.pop();
	}
}

// 递归 栈元素的排序.最小值在top
void zhuanzhi_stack(stack<int> &st)
{
	if(st.empty())
		return;
	int tmp = st.top();
	st.pop();
	if(!st.empty())
	{
		zhuanzhi_stack(st);
		int tmp2 = st.top();
		//if (tmp > tmp2)
		//{
			st.pop();
			st.push(tmp);
			st.push(tmp2);
			return;
		//}
	}
	st.push(tmp);
}
void zhuanzhi_(stack<int> &st)
{
	if (st.empty())
		return;
	zhuanzhi_stack(st);
	int tmp = st.top();
	st.pop();
	zhuanzhi_(st);
	st.push(tmp);
}

int main()
{
	stack<int> st;
	st.push(10);
	st.push(9);
	st.push(20);
	st.push(0);
	st.push(7);

	print_stack(st);

	order_(st);
	cout<<endl;
	print_stack(st);

	zhuanzhi_(st);
	cout<<endl;
	print_stack(st);	

	order_stack(st);
	cout<<endl;
	print_stack(st);

	cout<<endl;
	return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 通常来说,单链表的逆序输出可以通过遍历链表,并对链表中的每个元素进行重新排序实现。首先,从链表的头结点开始遍历,然后将每个节点依次插入到新链表的头部,最后将新链表的头结点赋值给原链表的头结点,从而实现逆序输出。 ### 回答2: 单链表的逆序输出可以通过遍历链表并使用递归或栈来实现。 1. 使用递归实现单链表的逆序输出: 递归函数可以在链表访问到最后一个节点后开始返回并输出节点的值,然后再一层一层返回并输出之前的节点的值。 例如,假设链表的节点如下: ``` class ListNode { int val; ListNode next; ListNode(int val) { this.val = val; this.next = null; } } ``` 递归函数如下: ``` public void reversePrint(ListNode head) { if (head == null) { return; } reversePrint(head.next); System.out.print(head.val + " "); } ``` 通过调用 `reversePrint(head)` 函数即可逆序输出单链表的节点值。 2. 使用栈实现单链表的逆序输出: 可以使用栈来保存链表节点的值,并在遍历链表时将节点的值依次压入栈中,最后再逐个弹出栈中的元素并输出。 例如,假设链表的节点如下: ``` class ListNode { int val; ListNode next; ListNode(int val) { this.val = val; this.next = null; } } ``` 使用栈实现逆序输出的代码如下: ``` public void reversePrint(ListNode head) { if (head == null) { return; } Stack<Integer> stack = new Stack<>(); ListNode curr = head; while (curr != null) { stack.push(curr.val); curr = curr.next; } while (!stack.isEmpty()) { System.out.print(stack.pop() + " "); } } ``` 通过调用 `reversePrint(head)` 函数即可逆序输出单链表的节点值。 以上是两种实现单链表逆序输出的方法。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值