递归反转链表


  1 #include <iostream>
  2 #include <stdlib.h>
  3 using namespace std;
  4 
  5 typedef struct list
  6 {
  7     int data;
  8     struct list *next;
  9 }list;
 10 
 11 void init_list(list *&node)
 12 {
 13     node=NULL;
 14 }
 15 void insert_list(list *&node,int value)
 16 {
 17     if(node==NULL)
 18     {
 19     //  cout<<"NULL"<<endl;
 20         node=new list;
 21         node->data=value;
 22         node->next=NULL;
 23     }else
 24     {
 25     //  cout<<"NOT NULL"<<endl;
 26         list *ptr=node;
 27         list *p=ptr;
 28         while(ptr->next !=NULL)
 29         {
 30         //  cout<<ptr->data<<" ";
 31             ptr=ptr->next;
 32         }
 33         list *tmp=new list;
 34         tmp->data=value;
 35         tmp->next=NULL;
 36         ptr->next=tmp;
 37     //  while(p !=NULL)
 38     //  {
 39     //      cout<<p->data<<" ";
 40     //      p=p->next;
 41     //  }
 42     //  cout<<endl;
 43     }
 44 }
 45 void show(list *node)
 46 {
 47     list *cur=node;
 48     while(cur !=NULL)
 49     {
 50         cout<<cur->data<<" ";
 51         cur=cur->next;
 52     }
 53     cout<<endl;
 54 }
 55 //反转输出
 56 void rever_list(list *node)
 57 {
 58     if(node !=NULL)
 59     {
 60         rever_list(node->next);
 61         cout<<node->data<<" ";
 62     }
 63 }
 64 int main()
 65 {
 66     list *node=NULL;
 67 //  init_list(node);
 68     int value=0;
 69     while(cin>>value,value !=-1)
 70         insert_list(node,value);
 71     cout<<"show_list:";
 72     show(node);
 73     cout<<"rever_list:";
 74     rever_list(node);
 75     cout<<endl;
 76 }                                                                                                                    
                                     


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值