您的位置:首页 > 其它

输入一个链表的头结点,从尾到头反过来打印每个节点的值。

2016-05-02 13:48 274 查看
第一种:

先逆转链表,再依次访问。
(1)摘节点,改变原有链表结构
(2)申请空间
#include<stdio.h>
#include<stdlib.h>
typedef int Datatype;
typedef struct Listnode
{
Datatype _data;
struct Listnode* _next;
}Listnode;
Listnode* Buynode()
{
Listnode* tmp=(Listnode*)malloc(sizeof(Listnode));
return tmp;
}
void Push(Listnode* &head,Datatype data)
{
Listnode* cur=head;
if(cur==NULL)
{
cur=Buynode();
cur->_data=data;
cur->_next=NULL;
head=cur;
}
else
{
while(cur->_next !=NULL)
{
cur=cur->_next;
}
Listnode* tmp=NULL;
tmp=Buynode();
tmp->_data=data;
tmp->_next=NULL;
cur->_next=tmp;
}
}
void Pop(Listnode* head)
{
Listnode* cur=head;
Listnode* prev=NULL;
if(cur==NULL)
{
return ;
}
while(cur->_next!=NULL)
{
prev=cur;
cur=cur->_next;
}
delete cur;
prev->_next=NULL;
cur=NULL;

}
void Reverse(Listnode* head)
{
Listnode* cur=head;
Listnode* tmp=NULL;
Listnode* newhead=NULL;
while(cur)
{
tmp=cur;
cur=cur->_next;
tmp->_next=newhead;
newhead=tmp;
}
while(newhead)
{
printf("%d\n",newhead->_data);
newhead=newhead->_next;
}
}

int main()
{
Listnode* head=NULL;
Push(head,1);
Push(head,2);
Push(head,3);
Push(head,4);
Push(head,5);

Reverse(head);
system("pause");
return 0;
}
第二种:
利用栈的先进后出特性,依次遍历链表,将链表数据依次存入栈中,然后再遍历栈
#include<iostream>
#include<stack>
using namespace std;
template<class T>
struct Listnode
{
Listnode<T>(T data=0)
:_data(data)
,_next(NULL)
{}
T _data;
Listnode<T>* _next;
};
template<class T>
class List
{
public:

List(Listnode<T>* head=NULL)
:_head(head)
{
}
~List()
{
Listnode<T>* cur=_head;
while(cur)
{
Listnode<T>* tmp=cur;
cur=cur->_next;
delete tmp;
}
}
void Push(T data)
{
Listnode<T>* cur=_head;
if(cur==NULL)
{
cur=new Listnode<T>(data);
_head=cur;
}
else
{
while(cur->_next!=NULL)
{
cur=cur->_next;
}
cur->_next=new Listnode<T>(data);
}
}
void Pop()
{
Listnode<T>* cur=_head;
if(cur==NULL)
{
return;
}
Listnode<T>* prev=NULL;
while(cur->_next!=NULL)
{
prev=cur;
cur=cur->_next;
}
delete cur;
prev->_next=NULL;
cur=NULL;
}

void ReverOrderPrint()
{
stack<int> s;
Listnode<T>* cur=_head;
while(cur)
{
s.push(cur->_data);
cur=cur->_next;
}
int size=s.size();
while(size--)
{
cout<<s.top()<<' ';
s.pop();
}
}
private:
Listnode<T> *_head;
};
void test()
{
List<int> l;
l.Push(1);
l.Push(2);
l.Push(3);
l.Push(4);
l.Push(5);

l.ReverOrderPrint();

}
int main()
{
test();
system("pause");
return 0;
}
第三种:递归实现
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef int Datatype;
typedef struct Listnode
{
Datatype _data;
struct Listnode* _next;
}Listnode;
Listnode* Buynode()
{
Listnode* tmp=(Listnode*)malloc(sizeof(Listnode));
return tmp;
}
void Push(Listnode* &head,Datatype data)
{
Listnode* cur=head;
if(cur==NULL)
{
cur=Buynode();
cur->_data=data;
cur->_next=NULL;
head=cur;
}
else
{
while(cur->_next !=NULL)
{
cur=cur->_next;
}
Listnode* tmp=NULL;
tmp=Buynode();
tmp->_data=data;
tmp->_next=NULL;
cur->_next=tmp;
}
}
void Pop(Listnode* head)
{
Listnode* cur=head;
Listnode* prev=NULL;
if(cur==NULL)
{
return ;
}
while(cur->_next!=NULL)
{
prev=cur;
cur=cur->_next;
}
delete cur;
prev->_next=NULL;
cur=NULL;

}
void RevOrderPrint(Listnode* head)
{
Listnode* cur=head;
if(cur)
{
RevOrderPrint(cur->_next);
printf("%d\n",cur->_data);
}
else
{
return;
}
}

int main()
{
Listnode* head=NULL;
Push(head,1);
Push(head,2);
Push(head,3);
Push(head,4);
Push(head,5);

RevOrderPrint(head);
system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: