Linked list reversal using stack【1月19日学习笔记】

点击查看代码
//Linked list reversal using stack
#include<iostream>
#include<stack>//stack from standard template library(STL)
using namespace std;
struct node {
	char data;
	node* next;
};
node* A;//全局头指针

void reverse() {
	if (A == NULL) return;//空列表直接退出
	stack<node*> S;//node*指针型的stack

	node* run = A;
	while (run != NULL) {
		S.push(run);
		run = run->next;
	}//遍历打印模式push
	run = S.top();//run退回到末节点
	A = run;//头指针指向末节点
	S.pop();//pop掉末节点地址

	while (!S.empty()) {//stack不为空时
		run->next = S.top();//run指向节点尾巴指向上一节点
		S.pop();//pop掉已用节点地址
		run = run->next;//run继续退回(run指向节点尾巴已指向上一节点)
	}
	run->next = NULL;//1节点尾巴赋空
}

int main() {
	
}
posted @   bituion  阅读(7)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示