c代码一段,谁帮我看一下。

来源:百度知道 编辑:UC知道 时间:2024/09/21 20:27:55
#define NULL 0

struct stu
{
char name[20];
char sex;
int no;
int age;
struct stu *next;
};

typedef struct stu Student_L;

Student_L *createList( int n )
{
int i;
Student_L *head; /*指向链表头*/
Student_L *current; /*当前对象指针*/
Student_L *last; /*最后一个对象的指针*/

head = (Student_L *)malloc( sizeof(Student_L) );
current = head;
head->next = NULL;

for (i=0; i<n; i++)
{
last = (Student_L *)malloc( sizeof(Student_L) );
if( NULL != last )
{
current->next = last;
printf("Please input the information of Student %d:(name sex No. age)\n", i+1 );
scanf( "%s %c %d %d", last->name, &(last->sex), &(last->no), &(last->age) );
last->next = NULL;
current = last;
}
else
{
printf( "Student %

#include<stdio.h>
#include<stdlib.h>
//#define NULL 0 这个不用你定义,库里面有定义的
typedef struct
{
char name[20];
char sex;
int no;
int age;
struct stu *next;
}Student_L;//这么定义和你的效果一样

//实际上你这里没有必要创造头节点 我还是保留了你的头节点
Student_L *createList( int n )
{
int i;
Student_L *head; /*指向链表头*/
Student_L *current; /*当前对象指针*/
Student_L *last; /*最后一个对象的指针*/

head = (Student_L *)malloc( sizeof(Student_L) );//头节点
if(NULL == head)//分配不成功时
{
printf("CreatList failed!\n");
return NULL;
}
current = head;
head->next = NULL;

for (i=0; i<n; i++)
{
last = (Student_L *)malloc( sizeof(Student_L) );
if(NULL == last)//分配不成功时
{
while(NULL == head)//释放内存
{
last = he