#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");
}
有幾個地方少了*
#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");
}