单链表的反转实现(C++)

  1 #include  < iostream >
  2 #include  < cstdlib >
  3
  4 using   namespace  std;
  5
  6 class  Node
  7 {
  8       private:
  9         int data;
 10          Node* next;
 11       public:
 12          friend class List;
 13          Node(int theValue)
 14          {
 15              data = theValue;
 16              next = 0;              
 17          }
                 
 18            
 19}
;
 20       
 21 class  List
 22 {
 23     public:
 24            List()
 25            {
 26                  first = 0;
 27                  end = 0;
 28            }

 29            ~List()
 30            {
 31                   Node* next;
 32                   while (first)
 33                   {
 34                         next = first->next;
 35                         delete first;
 36                         first = next;
 37                   }

 38            }

 39            
 40            
 41            //增加item,从屁股后面插入 ^_^
 42            void Add(int value)
 43            {
 44                 if (first == 0)
 45                 {
 46                    first = end = new Node(value);
 47                 }

 48                 else
 49                 {
 50                     end->next = new Node(value);
 51                     end = end->next;
 52                 }

 53            }

 54            
 55            //反转List,从前面插入(原有的item,不要想歪了 ^_^)
 56            void Reverse()
 57            {
 58                 if (first == 0)
 59                    return;
 60                 
 61                 Node* newFirst = first;//临时变量,保存新的first. 
 62                 end = first;//反转后,first 刚好和end 互换. 
 63                 Node* nextOfFirst;//辅助变量,其值始终为first的下一个成员,即first->next. 
 64                 
 65                 first = first->next;                
 66                 while (first)
 67                 {
 68                       nextOfFirst = first->next;
 69                       
 70                       first->next = newFirst;
 71                       newFirst = first;
 72                       
 73                       first = nextOfFirst;
 74                 }

 75                 
 76                 first = newFirst;  
 77                 end->next = 0;
 78                 
 79            }

 80            
 81            //显示List的所有成员 
 82            void Display()
 83            {
 84                 if (first == 0)
 85                 {
 86                     cout << "The List has no items." << endl;
 87                     return;
 88                 }

 89                 else
 90                 {
 91                     cout << "The List's items:" << endl;
 92                 }

 93                    
 94                 Node* item = first;
 95                 while (item)
 96                 {
 97                       cout << item->data << " ";
 98                       item = item->next;                       
 99                 }

100                 
101                 cout << endl;
102            }

103
104     private:
105            Node* first;
106            Node* end;
107      
108}
;
109
110
111 int  main()
112 {
113    List theList;
114    for (int i = 0; i < 10; i++)
115        theList.Add(i);
116        
117    //显示其初始值
118    cout << "List 的原始值为:" <<endl; 
119    theList.Display();
120    
121
122    //反转,并显示其值
123    cout << endl <<endl
124         <<"List 反转之后的值为:" << endl; 
125    theList.Reverse();
126    theList.Display();
127    
128    system("pause");  
129
130}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值