#include <iostream>
#include <malloc.h>
#include <stack>
using namespace std;
typedef struct LNode
{
int value;
LNode *next;
}*LinkedList;
LinkedList createList() //前插入法创建链表
{
LinkedList head = NULL;
LNode *p;
int val;
scanf("%d",&val);
while(val != -1)
{
p =(LinkedList)malloc(sizeof(LinkedList));
p->value = val;
p->next = head;
head = p;
scanf("%d",&val);
}
return head;
}
LinkedList createList1() //后插入法建立链表
{
LinkedList head = NULL;
LNode *tail,*p;
int val;
scanf("%d",&val);
while(val != -1)
{
p=(LinkedList)malloc(sizeof(LinkedList));
p->value = val;
p->next = NULL;
if(head == NULL)
{
head = p;
tail = head;
}
else
{
tail->next = p;
tail = p;
}
scanf("%d",&val);
}
return head;
}
void printLinkedList(LinkedList head) //从头到尾输出链表
{
LNode *p = head;
while(p != NULL)
{
cout << p->value << endl;
p = p->next;
}
}
void printreverseList(LinkedList head) //从尾到头输出链表
{
stack<LinkedList> list; //建立栈,通过标准库中的栈建立
LinkedList tail = head;
while(tail != NULL)
{
list.push(tail);
tail = tail->next;
}
while(!list.empty())
{
LinkedList n = list.top();
printf("%d\n",n->value);
//printf("\n");
list.pop();
}
}
void printreverseList1 (LinkedList head) //递归法从尾到头输出链表
{
LinkedList tail = head;
if(tail != NULL)
{
printreverseList1(tail->next);
printf("%d\n",tail->value);
}
}
int main(int argc, char *argv[])
{
LinkedList head = createList1();
printreverseList1(head);
return 0;
}