1. 将链表逆序,然后从头到尾输出,需要额外操作;
2. 顺序遍历过程中节点进栈,然后输出栈即可,需要额外的栈;
3. 由栈转到递归。
#include <iostream>
using namespace std;
typedef struct LNode{
int data;
LNode* next;
}*List;
void InsertList(List &l, int data)
{
LNode *p=new LNode;
p->data=data;
if(NULL==l)
p->next=NULL;
else
p->next=l;
l=p;
}
void Print(List l)//从头到尾
{
LNode *p=l;
while(p)
{
cout<<p->data<<" ";
p=p->next;
}
}
void ReversePrintList(List l)//从尾到头
{
if(l)
{
if(l->next)
ReversePrintList(l->next);
cout<<l->data<<" ";
}
}
void main()
{
List l=NULL;
InsertList(l, 23);
InsertList(l, 43);
InsertList(l, 11);
InsertList(l, 74);
InsertList(l, 69);
InsertList(l, 52);
InsertList(l, 73);
InsertList(l, 66);
cout<<"从头到尾:";
Print(l);
cout<<endl;
cout<<"从尾到头:";
ReversePrintList(l);
cout<<endl;
}