//从尾到头打印链表
#include <iostream>
#include <stack>
using namespace std;
struct Node
{
int value;
Node* next;
};
void printReversely(Node* pHead)
{
stack<int> istack;
Node* pNode = pHead;
while(pNode!=NULL)
{
istack.push(pNode->value);
pNode = pNode->next;
}
while(!istack.empty())
{
cout<<istack.top()<<" ";
istack.pop();
}
}
int main()
{
int n = 5;
Node *preNode,*pNode;
preNode = NULL;
while(n)
{
pNode = new Node();
pNode->value = 2*n;
pNode->next = preNode;
preNode = pNode;
--n;
}
cout<<"reverse: ";
printReversely(pNode);
cout<<endl;
cout<<"ordinary index: ";
Node *tmp = pNode;
while(tmp != NULL)
{
cout<<tmp->value<<" ";
tmp = tmp->next;
}
cout<<endl;
return 0;
}