初学C语言简单链表的问题,书上说p要指向head才能使用链表,我直接p=&a 发现结果和p=head一样,为什么
答案:3 悬赏:30 手机版
解决时间 2021-03-25 00:12
- 提问者网友:爱唱彩虹
- 2021-03-24 14:17
初学C语言简单链表的问题,书上说p要指向head才能使用链表,我直接p=&a 发现结果和p=head一样,为什么
最佳答案
- 五星知识达人网友:时间的尘埃
- 2021-03-24 14:24
head 是链表规范的写法,p一般做为游动指针,你若这样写结果上是没错的,但是给阅读程序的人带来不便,你要知道,真正的程序代码不是这么几行的,所以基本就约定俗称了链表头有专门的head 表示,你把链表头赋给了p,若在程序的某个地方再次需要操作链表,就需要链表头,只要链表头才能把整个链表联系起来进行操作,而你此时的p已经到NULL了,链表头丢失,无法操作,且随便用字母声明的变量在专业的程序里是极不赞成的,因该使用有一定意义的单词或字母的组合同时兼顾大小写,这样的程序别人看了一目了然,且日后你看的时候也不至于忘了当初这个变量代表啥,所以head就表示为链表头了。
给你随便找了一个链表的程序,看了你就会知道head 的重要性。
#include
#include
#define NULL 0
#define LEN sizeof(struct student)
struct student
{
long num;
float score;
struct student *next;
};
int n;
struct student *Create()
{
struct student *head;
struct student *p1=NULL;
struct student *p2=NULL;
n = 0;
p1 = (struct student *)malloc(LEN);
p2 = p1;
if (p1 == NULL)
{
printf("\nCann't create it, try it again in a moment!\n");
return NULL;
}
else
{
head = NULL;
printf("Please input %d node -- num,score: ",n+1);
scanf("%ld,%f",&(p1->num),&(p1->score));
}
while(p1->num != 0)
{
n += 1;
if (n==1)
{
head = p1;
p2->next = NULL;
}
else
{
p2->next = p1;
}
p2 = p1;
p1 = (struct student *)malloc(LEN);
printf("Please input %d node -- num,score: ",n+1);
scanf("%ld,%f",&(p1->num),&(p1->score));
}
p2->next = NULL;
free(p1);
p1 = NULL;
return head;
}
void Print(struct student *head)
{
struct student *p;
printf("\nNow , These %d records are:\n",n);
p = head;
if(head != NULL)
{
printf("head is %o\n", head);
do
{
printf("%o %ld %5.1f %o\n", p, p->num, p->score, p->next);
p = p->next;
}
while (p != NULL);
}
}
struct student *Del(struct student *head, long num)
{
struct student *p1;
struct student *p2;
if (head == NULL)
{
printf("\nList is null!\n");
return head;
}
p1 = head;
while (p1->num != num && p1->next != NULL)
{
p2 = p1;
p1 = p1->next;
}
if (num == p1->num)
{
if (p1 == head)
{
head = p1->next;
}
else
{
p2->next = p1->next;
}
free(p1);
p1 = NULL;
printf("\ndelete %ld success!\n",num);
n -= 1;
}
else
{
printf("\n%ld not been found!\n",num);
}
return head;
给你随便找了一个链表的程序,看了你就会知道head 的重要性。
#include
#include
#define NULL 0
#define LEN sizeof(struct student)
struct student
{
long num;
float score;
struct student *next;
};
int n;
struct student *Create()
{
struct student *head;
struct student *p1=NULL;
struct student *p2=NULL;
n = 0;
p1 = (struct student *)malloc(LEN);
p2 = p1;
if (p1 == NULL)
{
printf("\nCann't create it, try it again in a moment!\n");
return NULL;
}
else
{
head = NULL;
printf("Please input %d node -- num,score: ",n+1);
scanf("%ld,%f",&(p1->num),&(p1->score));
}
while(p1->num != 0)
{
n += 1;
if (n==1)
{
head = p1;
p2->next = NULL;
}
else
{
p2->next = p1;
}
p2 = p1;
p1 = (struct student *)malloc(LEN);
printf("Please input %d node -- num,score: ",n+1);
scanf("%ld,%f",&(p1->num),&(p1->score));
}
p2->next = NULL;
free(p1);
p1 = NULL;
return head;
}
void Print(struct student *head)
{
struct student *p;
printf("\nNow , These %d records are:\n",n);
p = head;
if(head != NULL)
{
printf("head is %o\n", head);
do
{
printf("%o %ld %5.1f %o\n", p, p->num, p->score, p->next);
p = p->next;
}
while (p != NULL);
}
}
struct student *Del(struct student *head, long num)
{
struct student *p1;
struct student *p2;
if (head == NULL)
{
printf("\nList is null!\n");
return head;
}
p1 = head;
while (p1->num != num && p1->next != NULL)
{
p2 = p1;
p1 = p1->next;
}
if (num == p1->num)
{
if (p1 == head)
{
head = p1->next;
}
else
{
p2->next = p1->next;
}
free(p1);
p1 = NULL;
printf("\ndelete %ld success!\n",num);
n -= 1;
}
else
{
printf("\n%ld not been found!\n",num);
}
return head;
全部回答
- 1楼网友:西风乍起
- 2021-03-24 16:07
p=head调用子函数
- 2楼网友:青尢
- 2021-03-24 15:13
你改过之后运行过吗?我运行后结果是不一样的。
这是没改过的
这是改过后的结果
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯