反向输出一个链表(链表逆置)

/*
This file was made for Link_list Exp on Linux

Main.cpp 

Founded in others' Blog ,Thanks for sharing your knowledge

*/


#include <iostream>
using namespace std;

class node
{
      public:
      node * next;
      int data;
};

node *test = NULL;

node *nodereverse(node *head)
{
    //如果一个函数的输入参数有指针,一定要记住判断指针时候为空
    //1>:在使用一个指针之前一定要判断它是否为空;
    //2>:使用完后要释放由指针指向的存储单元
    //3>:释放完存储单元后要将指针赋值为NULL;
     if(head->next==NULL || head==NULL)
          return head;


     node* temp1=head;
     node* temp2=NULL;
     node* temp3=head->next;
     temp1->next = NULL;
     //要注意这里面的顺序,先将temp3保存在temp2中,
     //然后再将temp3移动到下一个元素,然后才能改动temp2
     //
     while(temp3->next!=NULL)
     {
        temp2 = temp3;
        temp3 = temp3->next;
        temp2->next = temp1;//不能再temp3= temp3->next;之前执行
        temp1 = temp2;

     }
     temp3->next = temp2;
     return temp3;
}
void initnode()
{
     node * tmp = NULL;
     for(int i=0; i<4; i++)
     {
         tmp = new node;
         tmp->data = i;
         tmp->next = test;
         test = tmp;
     }
}
void display(node *nn)
{
if(nn==NULL)
{
    cout << "no data to display\n";
    return ;
}
node *dis = nn;
 while(dis!=NULL)
 {
        cout << dis->data << endl;
        dis = dis->next;
 }
}

//释放动态申请的空间
void distroy(node *nn)
{
    if (nn==NULL)
    {
        return ;
    }
    while (nn!=NULL)
    {
        node *tmp = nn;
        nn = nn->next;
        delete tmp;
    }
}



int main()
{
    initnode();
    display(test);
    cout << "**************" << endl;
    node *tmp = nodereverse(test);
    if(test==NULL)
;
      //  exit(0);
    display(tmp);
    //tmp和test指向的存储空间已经使用完毕,应该释放掉他们申请的空间!
    //并且,要将他们赋值为NULL,否则他们将成为野指针!!!!,一定要注意了~~
    distroy(tmp);//释放动态申请的内存
    tmp = NULL;//将他们重新赋值为NULL,不然就会成为野指针~~~~~
    test = NULL;
    cout << "tmp= " << tmp << endl;

    //如果上面没有tmp = NULL;test = NULL;,display将会出错,
    //因为在display开始的时候判断传入的参数是否为NULL,如果不把野指针赋值为NULL,
    //那么判断就没有效果,会继续指向display中的while语句,而此时指针所指向的存储空间已经被释放掉了,
    //这样就会出现异常.
    display(test);
    //system("pause");
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值