链表创建,打印,插入,删除等操作。(谭浩强:《c语言程序设计-v2》)
#include
#include
#include
#define LEN sizeof(struct student)
struct student
{
long num;
float score;
struct student *next;
};
int n;
// ---- creat chain
struct student *creat_chain(void)
{
struct student *head = NULL, *p = NULL, *q = NULL;
n = 0;
q = p = (struct student *) malloc(LEN);
scanf("%ld, %f", &p->num, &p->score); // 输入时用逗号隔开
printf("Num is %d,Score is %5.1f \n", p->num, p->score);
while (p->num != 0)
{
n=n+1;
if (n == 1) head = p;
else q->next = p;
q = p;
p = (struct student *) malloc(LEN);
scanf("%ld, %f", &p->num, &p->score);
printf("Num is %d,Score is %f \n", p->num, p->score);
}
q->next = NULL;
return (head);
}
//---- print chain
void print_chain(struct student *head)
{
struct student *p;
printf("The chain is : \n");
p = head;
if (head != NULL)
do
{
printf("%ld, %5.1f\n", p->num, p->score);
p = p->next;
}
while(p!=NULL);
}
//----
int main(void)
{
struct student *head;
head = creat_chain();
print_chain(head);
return 0;
}
/*-- E --*/
谢谢欣赏
阅读(847) | 评论(0) | 转发(0) |