题目:输入一个链表的头节点,从尾到头打印出每个节点的值
#include <iostream>
#include <string>
#include <cstdlib>
#include <map>
#include <vector>
#include <algorithm>
#include <thread>
#include <array>
#include <ctime>
#include <memory>
#include <stack>
using namespace std;
typedef struct listnode
{
int val;
struct listnode *next;
}node, *ListNode;
void creatList(ListNode& pHead)
{
int nodeVal;
cout << "Please Enter NodeVal:";
cin >> nodeVal;
ListNode head, tail;
if (nodeVal != -1)
{
head = new node;
head->val = nodeVal;
head->next = nullptr;
pHead = head;
tail = head;
}
else
return;
cout << "Please Enter NodeVal:";
cin >> nodeVal;
while (nodeVal != -1)
{
ListNode tempNode = new node;
tempNode->val = nodeVal;
tempNode->next = nullptr;
tail->next = tempNode;
tail = tempNode;
cout << "Please Enter NodeVal:";
cin >> nodeVal;
}
}
int main()
{
ListNode head = nullptr;
creatList(head);
stack<int> st;
for (ListNode temp = head; temp != nullptr; temp = temp->next)
{
st.push(temp->val);
}
while (!st.empty())
{
cout << st.top() << endl;
st.pop();
}
}