链表反转,指针指向反转其他不变

链表反转,指针指向反转其他不变,第1张

本人是初学者  内容记录一下,分享给各网友参考

#include
#include
struct gg
{
  int i;
  struct gg *next;  //指向于 同类型 的指针
};
void xun(struct gg *head)
{
  struct gg *p =head;
  while(p != NULL){ 
  printf("%p\n%d\n%p\n",p,p->i,p->next);  
  p=p->next; 
  }
}
 struct gg *fan(struct gg *head)  //指针指向反转  其他不改动
{
  struct gg *p=head;
  struct gg *b=NULL;
  struct gg *c;

/* while(p != NULL){ //写法1  先偏移 再修改
    c=p;             //保存上一个节点1
    p=p->next;       //链表指向下一个节点2
    c->next=b;       //修改上一个节点  尾的指向   //同等于(b覆盖c) c->next = b->next;
      b=c;             //把c的头节点保存 上一个节点头保存用于下一次修改
  } */
  while(p != NULL){  //写法2   先修改 再偏移
      c=p->next;     //保存下一个节点 方便下一次偏移
      p->next=b;     //修改当前节点  尾的指向
      b=p;           //头节点保存 上一个节点头保存用于下一次修改   
      p=c;           //链表指向下一个节点2  
  }

int main()
{
  struct gg t1={1,NULL};
  struct gg t2={2,NULL};
  struct gg t3={3,NULL};
  struct gg t4={4,NULL};
  t1.next=&t2;
  t2.next=&t3;
  t3.next=&t4;
  xun(&t1);
  fan(&t1);
  xun(&t4); 
  while(1);
    return 0;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/562466.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-01
下一篇 2022-04-01

发表评论

登录后才能评论

评论列表(0条)

保存