|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include <stdio.h>
#include <stdlib.h>
typedef struct list
{
int num;
char name;
struct list *next;
}node;//定义一个链表存储结构;
void init_list(node *L,node *temp)
{
int item;
char item1;
int n=1;
while(1)
{
printf("请输入学生学号(输入0表示结束):\n");
scanf("%d",&item);
if(item == 0)
break;
printf("请输入学生姓名:\n");
scanf("%s",&item1);
L = (node*)malloc(sizeof(struct list));
L->num = item;
L->name = item1;
if(n==1)
{
temp = (node*)malloc(sizeof(struct list));
temp = L;
n++;
}
L = L->next;
}
L = NULL;
}
void print_list(node *temp)
{
while(temp != NULL)
{
printf("%d", temp->num);
printf("%c", temp->name);
temp = temp->next;
}
}
int main()
{
node *L;
node *temp;
init_list(L,temp);
print_list(temp);
return 0;
}
求助大神帮帮忙,一直改都是错的。要疯了。
用字符数组
- #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;
- }
复制代码
|
|