标题:求大神指点啊 看不懂的链表
取消只看楼主
wsws23
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:105
专家分:107
注 册:2012-3-13
结帖率:81.82%
已结贴  问题点数:20 回复次数:2 
求大神指点啊 看不懂的链表
#include  <stdio.h>
#include  <stdlib.h>
#define    N    5
typedef struct node {
  int  data;
  struct node  *next;
} NODE;
void fun(NODE  *h)
{ NODE  *p, *q, *r;
/**********found**********/
  p = h->next;
/**********found**********/
  if (p==NULL)  return;
  q = p->next;
  p->next = NULL;
  while (q)
  {  r = q->next;    q->next = p;
/**********found**********/
     p = q;         
     q =r;
  }
  h->next = p;
}
NODE *creatlist(int  a[])
{  NODE  *h,*p,*q;        int  i;
  h = (NODE *)malloc(sizeof(NODE));
  h->next = NULL;
  for(i=0; i<N; i++)
  {  q=(NODE *)malloc(sizeof(NODE));
     q->data=a[i];
     q->next = NULL;
     if (h->next == NULL)  h->next = p = q;
     else    {  p->next = q;  p = q;   }
  }
   return  h;
}
void outlist(NODE  *h)
{ NODE  *p;
  p = h->next;
  if (p==NULL)  printf("The list is NULL!\n");
  else
  {  printf("\nHead  ");
     do
     {  printf("->%d", p->data); p=p->next;  }
     while(p!=NULL);
     printf("->End\n");
  }
}
main()
{  NODE  *head;
   int  a[N]={2,4,6,8,10};
   head=creatlist(a);
   printf("\nThe original list:\n");
   outlist(head);
   fun(head);
   printf("\nThe list after inverting :\n");
   outlist(head);
}

解释 这个就行了
NODE *creatlist(int  a[])
{  NODE  *h,*p,*q;        int  i;
  h = (NODE *)malloc(sizeof(NODE));
  h->next = NULL;
  for(i=0; i<N; i++)
  {  q=(NODE *)malloc(sizeof(NODE));
     q->data=a[i];
     q->next = NULL;
     if (h->next == NULL)  h->next = p = q;
     else    {  p->next = q;  p = q;   }
  }
   return  h;
搜索更多相关主题的帖子: next void include return 
2012-03-24 21:36
wsws23
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:105
专家分:107
注 册:2012-3-13
得分:0 
错了 解释这个  p = h->next;
/**********found**********/
  if (p==NULL)  return;
  q = p->next;
  p->next = NULL;
  while (q)
  {  r = q->next;    q->next = p;
/**********found**********/
     p = q;         
     q =r;
  }
  h->next = p;
}
2012-03-24 21:52
wsws23
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:105
专家分:107
注 册:2012-3-13
得分:0 
要加把劲学习啊
2012-03-24 21:55



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-364231-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.253217 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved