设为首页 加入收藏

TOP

C++实现单链表逆序
2014-11-01 08:45:05 来源: 作者: 【 】 浏览:67
Tags:实现 单链表

  将一个单链表逆序:


  struct list_node


  {


  list_node(int a,list_node* b):data(a),next(b) //这个为了测试方便


  {}


  int data;


  list_node* next;


  };


  void reserve(list_node* phead)


  {


  list_node* p = phead->next;


  if(p == NULL || p->next == NULL) return; //只有头节点或一个节点


  list_node* p1=p->next;


  p->next=NULL;


  while(p1!=NULL)


  {


  p = p1->next;


  p1->next = phead->next;


  phead->next = p1;


  p1 = p;


  }


  }


  测试程序:


  list lt;


  lt.phead = new list_node(0,0);


  lt.phead->next = new list_node(1,0);


  lt.phead->next->next = new list_node(2,0);


  lt.phead->next->next->next = new list_node(3,0);


  lt.reserve();


  list_node * p = lt.phead;


  while(p)


  {


  cout next;


  }


  编辑特别推荐:


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C++循环链表的节点对换和删除 下一篇C++自定义动态数组模板

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: