当前所在位置:珠峰网资料 >> 计算机 >> 计算机等级考试 >> 正文
C++实现单链表逆序
发布时间:2010/6/30 11:03:22 来源:城市学习网 编辑:ziteng
  将一个单链表逆序:
  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<data<        p = p->next;
  }
广告合作:400-664-0084 全国热线:400-664-0084
Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号
珠峰网 版权所有 All Rights Reserved