#include
#include
struct student
{
int num;
float score;
struct student pnext;
};
typedef struct student st;
void add(st **phead, int inum, float iscore)
{
if (*phead == NULL)
{
st *newnode = (st)malloc(sizeof(st));
newnode->num = inum;
newnode->score = iscore;
newnode->pnext = NULL;
phead = newnode;
}
else
{
st *p = *phead;
while (p->pnext != NULL)
{
p = p->pnext;
}
st *newnode = (st)malloc(sizeof(st));
newnode->num = inum;
newnode->score = iscore;
newnode->pnext = NULL;
p->pnext = newnode;
}
}
void show_all(st*head)
{
while (head != NULL)
{
printf("%d,%f\n", head->num, head->score);
head = head->pnext;
}
}
void main()
{
st *head=NULL;
add(&head, 1, 20);
add(&head, 2, 30);
add(&head, 3, 40);
add(&head, 4, 50);
add(&head, 5, 60);
show_all(head);
printf("\n");
show_all(head);
system("pause");
}

为什么第二次调用show_all还能打印出数据,指针不是已经到了链表的最后了吗
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- 小灸舞 2016-04-14 04:43关注
有几个地方少了*
#include<iostream> using namespace std; struct student { int num; float score; struct student *pnext; }; typedef struct student st; void add(st **phead, int inum, float iscore) { if (*phead == NULL) { st *newnode = (st*)malloc(sizeof(st)); newnode->num = inum; newnode->score = iscore; newnode->pnext = NULL; *phead = newnode; } else { st *p = *phead; while (p->pnext != NULL) { p = p->pnext; } st *newnode = (st*)malloc(sizeof(st)); newnode->num = inum; newnode->score = iscore; newnode->pnext = NULL; p->pnext = newnode; } } void show_all(st*head) { while (head != NULL) { printf("%d,%f\n", head->num, head->score); head = head->pnext; } } void main() { st *head=NULL; add(&head, 1, 20); add(&head, 2, 30); add(&head, 3, 40); add(&head, 4, 50); add(&head, 5, 60); show_all(head); printf("\n"); show_all(head); system("pause"); }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报