输入5 1 2 3 4 5
输出 5 4 3 2 1
#include <iostream>
using namespace std;
struct node{
int val;
node * pre;
node * next;
};
struct node * init()
{// 构造一个空链表
struct node *head;
head=new node;
head->pre=NULL;
head->next=NULL;
return head;
}
void creat(struct node *head,int n){
struct node *p,*q;
p=head;
int t;
for(int i=0;i<n;i++){
q=new node;
cin>>t;
q->val=t;
q->pre=p;
p->next=q;
p=q;
};
p->next=NULL;
}
void printRev(struct node *head){
struct node *p=head;
while(p->next){
p=p->next;
}
while(p->pre!=NULL){
cout<<p->val<<‘ ’;
p=p->pre;
}
}
int main(){
int n;
cin>>n;
// n=5;
struct node *head;
head=init();
creat(head,n);
//逆序输出节点
printRev(head);
return 0;
}