-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrintListReversingly_Iteratively.cpp
More file actions
56 lines (54 loc) · 1.12 KB
/
PrintListReversingly_Iteratively.cpp
File metadata and controls
56 lines (54 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*输入一个链表的头结点,从尾到头打印链表每个节点的值。*/
#include<iostream>
#include<stack>
using namespace std;
struct ListNode{
int m_nKey;
ListNode* m_pNext;
};
// void printListReversingly_Iteratively(ListNode* head)
// {
// stack<ListNode*> nodes;
// while(head)
// {
// nodes.push(head);
// head=head->m_pNext;
// }
// while(!nodes.empty())
// {
// cout<<nodes.top()->m_nKey<<endl;
// nodes.pop();
// }
// }
void printListReversingly_Iteratively(ListNode* head)
{
if(head)
{
if(head->m_pNext)
{
printListReversingly_Iteratively(head->m_pNext);
}
cout<<head->m_nKey<<endl;
}
}
void initList(ListNode* l)
{
ListNode* p=l;
int value;
while(cin>>value)
{
ListNode* newNode=new ListNode;
newNode->m_nKey=value;
p->m_pNext=newNode;
p=p->m_pNext;
}
p->m_pNext=NULL;
}
int main(int argc, char const *argv[]) {
/* code */
ListNode l;
initList(&l);
printListReversingly_Iteratively(l.m_pNext);
cin.get();
return 0;
}