如圖,為什么第一行沒有了,卻把0 0也輸出來了???

uj5u.com熱心網友回復:
遍歷鏈表為咐用do...while呢一般都是這樣:
while(p->next != NULL)
{
printf("%d, %f", p->num, p->pay);
p=p->next;
}
uj5u.com熱心網友回復:
貼原始碼吧~不建議貼圖片~uj5u.com熱心網友回復:
問題出在哪啊?像你這樣改了也不對,直接沒有第一個職工的工資了。
uj5u.com熱心網友回復:
好的????
uj5u.com熱心網友回復:
#include<stdio.h>
#include<malloc.h>
struct Employee
{
int num;
float pay;
struct Employee* next;
};
struct Employee* creat()
{
struct Employee* head, * p1, * p2;
int n=0;
p1 = p2 = (struct Employee*)malloc(sizeof(struct Employee));
head = NULL;
scanf("%d%f", &p1->num, &p2->pay);
while (p1->num != 0)
{
n = n + 1;
if (n == 1)
head = p1;
else
p2->next = p1;
p2 = p1;
p1 = (struct Employee*)malloc(sizeof(struct Employee));
scanf("%d%f", &p1->num, &p2->pay);
}
p2->next = NULL;
return(head); //回傳鏈表中第一個節點得起始地址
}
void list(struct Employee* head)
{
struct Employee* p;
p = head;
printf("The linked list:\n");
if(head!=NULL)
do
{
printf("%d,%.2f\n", p->num, p->pay);
p = p->next;
} while (p != NULL);
}
int main()
{
struct Employee* head;
head = creat();
list(head);
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/238812.html
標籤:C語言
下一篇:一道難題,求幫助!
