当前位置:网站首页>Print linked list from end to end (6)
Print linked list from end to end (6)
2022-06-10 16:32:00 【Burn slowly】
// 6: Print linked list from end to end
// subject : Enter the head node of a linked list , Print the value of each node from the end to the end .
#include<iostream>
#include<stack>
using namespace std;
struct ListNode{
int m_nValue;
ListNode * m_pNext;
};
// Create an empty single linked list of leading nodes
void InitList(ListNode **L) {
*L = (ListNode*)malloc(sizeof(ListNode));// Create a header node
(*L)->m_pNext = NULL;// Create an empty single linked list
}
// Use stack : First in, then out ; Put each traversed node from the top of the stack to the bottom of the stack , After traversing the linked list , Output one by one from the top of the stack , Because the top of the stack comes out first , Out of stack after stack bottom .
void print_stack(ListNode * pHead){
stack<ListNode *> nodes;
ListNode * pNode = pHead;
while (pNode != nullptr){
nodes.push(pNode);
pNode = pNode->m_pNext;
}
while (!nodes.empty()){
pNode = nodes.top();
printf("%d\n", pNode->m_nValue);
nodes.pop();
}
}
// Using recursion ( Too many layers can overflow the stack )
void print_recurve(ListNode * pHead){
if (pHead != nullptr){
if (pHead->m_pNext != nullptr){
print_recurve(pHead->m_pNext);
}
printf("%d\n", pHead->m_nValue);
}
}
// Add the single linked list of the leading node
void add_head(ListNode * pHead,int value){
ListNode * pNew = new ListNode();
pNew->m_nValue = value;
pNew->m_pNext = nullptr;
ListNode * pNode = pHead;
while (pNode->m_pNext != nullptr){
pNode = pNode->m_pNext;
}
pNode->m_pNext = pNew;
}
// Add a single linked list without leading nodes
void add_no_head(ListNode ** pHead, int value){
ListNode * pNew = new ListNode();
pNew->m_nValue = value;
pNew->m_pNext = nullptr;
if (*pHead == nullptr){
*pHead = pNew;
}else{
ListNode * pNode = *pHead;
while (pNode->m_pNext != nullptr){
pNode = pNode->m_pNext;
}
pNode->m_pNext = pNew;
}
}
#if 1
int main(){
ListNode * L;
printf("line = %d\n",__LINE__);
InitList(&L);
add_head(L, 1);
add_head(L, 2);
add_head(L, 3);
add_head(L, 4);
add_head(L, 5);
add_head(L, 6);
//print_stack(L->m_pNext);
print_recurve(L->m_pNext);
return 0;
}
#else
int main(){
ListNode * L=nullptr;
printf("line = %d\n",__LINE__);
add_no_head(&L, 1);
add_no_head(&L, 2);
add_no_head(&L, 3);
add_no_head(&L, 4);
add_no_head(&L, 5);
//print_stack(L);
print_recurve(L);
return 0;
}
#endif
边栏推荐
- 我用 MATLAB 复刻了抖音爆火小游戏 苹果蛇
- Devops-2- from the Phoenix Project
- Join operation cases in the reduce phase of MapReduce
- [object].
- Google x open source grabbing manipulator can find the target part at a glance without manual marking [turn]
- 纽约金融监管机构发布正式的稳定币指南
- Is it safe to open an account in qiniu? How to open an account online when buying stocks
- 硬件仪器的使用
- Effect comparison and code implementation of three time series hybrid modeling methods
- Analysis of different dimensions of enterprise reviewers: enterprise growth of Hunan Great Wall Science and Technology Information Co., Ltd
猜你喜欢

RGB颜色空间、色调、饱和度、亮度、HSV颜色空间详解

sm59远程连接,如果提示没有host,则在服务器上加上host,然后重启sap_SAP刘梦

Rk3308--8 channels changed to dual channels + recording gain

Meetup回顾|DevOps&MLOps如何在企业中解决机器学习困境?

Rk3308-- firmware compilation
![leetcode:730. Statistics of different palindrome subsequences [traversed by point and surface interval DP + 3D DP + diagonal]](/img/90/79c51f944709ef230f394e874aa9ac.png)
leetcode:730. Statistics of different palindrome subsequences [traversed by point and surface interval DP + 3D DP + diagonal]

I used Matlab to reproduce the trembling sonic boom Fire Games Apple snake

Scope and closure

Troubleshooting of cl210openstack operation -- troubleshooting of common core problems

Opencv 4 handwriting recognition: perfect self built training set
随机推荐
Android 13 re upgrade for intent filters security
2022 underground coal mine electrical examination question bank and online simulation examination
Rk3308-- firmware compilation
[section 7 function]
Application scenario introduction of nixie tube driver chip + voice chip, wt588e02b-24ss
Middle office: Data middle office, business middle office, technology middle office, application middle office, AI middle office
The command set has reached strategic cooperation with Yingmin technology, and the domestic original Internet of things operating system has helped to make power detection "intelligent"
Devops-2- from the Phoenix Project
Two methods of modifying PIP download source
Rk3308--8 channels changed to dual channels + recording gain
Zhangxiaobai teaches you how to use Ogg to synchronize Oracle 19C data with MySQL 5.7 (1)
2D human pose estimation for pose estimation - simdr: is 2D Heatmap representation even necessity for human pose estimation?
torch. nn. utils. rnn. pad_ Detailed explanation of sequence() [pytoch getting started manual]
STM32 printf garbled
China coal machinery industry development research and investment prospect analysis report 2022-2028 Edition
Why__ Inline usually needs static
PV operation daily question - black and white chess question (variant)
Many MapReduce cases seek common friends in the map stage
PV operation daily question 1 - single wooden bridge problem (variant 1)
Software College of Shandong University Project Training - Innovation Training - network security range experimental platform (16)