我自己写的C语言链表,我自己弄糊涂了,

来源:百度知道 编辑:UC知道 时间:2024/06/05 15:35:24
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
struct student
{unsigned int stu_id;
char sex;
unsigned short age;
struct student *next;}
main()
{struct student *head,*new_node,*cur,*prev;
int i=20;unsigned short c;
new_node=(struct student*)malloc(sizeof(struct student));
head=new_node;
do{
new_node->next=malloc(sizeof(struct student));
new_node=new_node->next;
new_node->stu_id=i+1000;
new_node->sex='M';new_node->age=i;
i++;}while(i<31);
new_node->next=NULL;
printf("make a student chained list:\n");
for(new_node=head;new_node;new_node=new_node->next)
printf("stu_id=%d,sex=%c,age=%d\n",new_node->stu_id,new_node->sex,new_node->age);
printf("_____________________\n");
printf("enter the num");
scanf("%d",&c);
for(cur=head,prev=NULL;

已经改好了:

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
struct student
{unsigned int stu_id;
char sex;
unsigned short age;
struct student *next;}
main()
{struct student *head,*new_node,*cur,*prev;
int i=20;unsigned short c;
new_node=(struct student*)malloc(sizeof(struct student));
head=new_node;
do{
new_node->next=(struct student*)malloc(sizeof(struct student)); //由于malloc的返回值为void*,所以需要强制类型转换为struct student*
new_node=new_node->next;
new_node->stu_id=i+1000;
new_node->sex='M';new_node->age=i;
i++;}while(i<31);
new_node->next=NULL;
printf("make a student chained list:\n");
for(new_node=head;new_node;new_node=new_node->next)
printf("stu_id=%d,sex=%c,age=%d\n",new_node->stu_id,new_node->sex,new_node->age);
printf("_____________________\n