栈用于反转字符串,链表

//应用 反转字符串 先全部入栈,再逐个Top Push
#include<iostream>
#include<stack>//stack form standard template library
using namespace std;
void Reverse(char *C, int n)//n是字符的数量
{
	stack<char> S;//拥有了一个字符栈
	//loop(循环) for push
	for (int i = 0; i < n; i++)
	{
		S.push(C[i]);//调用push函数把字符压入栈
	}
	//loop for pop
	for (int i = 0; i < n; i++)
	{
		C[i] = S.top();
		S.pop();
	}
}
int main(void)
{
	char C[5] = "hell";
	Reverse(C, 4);
	cout << C;//lleh
	return 0;

 - [ ] List item

}
//应用 反转链表 Reverse a Linked List
stack<struct Node*> S;
Node* head = NULL;
void Reverse_linked_list()
{
	Node* temp = head;
	while (temp != NULL)
	{
		S.push(temp);
		temp = temp->link;
	}
	Node* temp1 = S.top();
	head = temp1;
	S.pop();
	while (! S.empty())
	{
		temp1->link = S.top();
		S.pop();
		temp1 = temp1->link;
	}
	temp1->link = NULL;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值