#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 12: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"); }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 Python爬取指定微博话题下的内容,保存为txt
- ¥15 vue2登录调用后端接口如何实现
- ¥65 永磁型步进电机PID算法
- ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
- ¥88 找成都本地经验丰富懂小程序开发的技术大咖
- ¥15 如何处理复杂数据表格的除法运算
- ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
- ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
- ¥15 latex怎么处理论文引理引用参考文献
- ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?