用字符数组#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node
{
int num;
char name[64];
struct node *next;
} node, list_t;
list_t *init_list(void)
{
node *head = (node *)malloc(sizeof(node));
memset(head, 0, sizeof(node));
node *p = head;
while(1)
{
p->next = (node *)malloc(sizeof(node));
printf("请输入学生学号(输入0表示结束): ");
scanf("%d", &p->next->num);
if(p->next->num == 0)
break;
printf("请输入学生姓名: ");
scanf("%s", p->next->name);
p = p->next;
}
free(p->next);
p->next = NULL;
return head;
}
void deinit_list(list_t *head)
{
node *p = head;
while(p)
{
node *temp = p;
p = p->next;
free(temp);
}
}
void print_list(list_t *head)
{
node *p = head->next;
while(p)
{
printf("%d %s\n", p->num, p->name);
p = p->next;
}
}
int main(void)
{
list_t *head = init_list();
print_list(head);
deinit_list(head);
return 0;
}
|