求大佬解释一下这个链表,strcpy(p->name,name);这句是干啥的,有能力的话把整个链表详解一下,谢谢。 120
#include<stdio.h>#include<stdlib.h>#include<string.h>structSTUDENT{charname[20];intag...
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct STUDENT{
char name[20];
int age;
float salary;
struct STUDENT *next;
};
struct STUDENT *creat_list();
void print_list(struct STUDENT *head);
int main()
{
struct STUDENT *head;
head = creat_list();
print_list(head);
}
struct STUDENT *creat_list(){
struct STUDENT *head,*q,*p;
char name[20];
int i=1;
head = (struct STUDENT*)malloc(sizeof(struct STUDENT));//申请头节点
q = head;
fgets(name,20,stdin);
while(strlen(name))
{
p = (struct STUDENT*)malloc(sizeof(struct STUDENT));
strcpy(p->name,name);
printf("请输入第%d位的年龄:",i);
scanf("%d",&p->age);
printf("请输入第%d位的工资:",i);
scanf("%f",&p->salary);
getchar();//吸收回车
q->next = p;
q = p;//q移动到最后一个节点
fgets(name,20,stdin);
i++;
}
q->next = NULL;
return head;
}
//打印链表
void print_list(struct STUDENT *head)
{
struct STUDENT *p;
p = head->next;
while(p!=NULL)
{
printf("姓名:%s\n年龄:%d\n工资:%f\n",p->name,p->age,p->salary);
p = p->next;
}
} 展开
#include <stdlib.h>
#include <string.h>
struct STUDENT{
char name[20];
int age;
float salary;
struct STUDENT *next;
};
struct STUDENT *creat_list();
void print_list(struct STUDENT *head);
int main()
{
struct STUDENT *head;
head = creat_list();
print_list(head);
}
struct STUDENT *creat_list(){
struct STUDENT *head,*q,*p;
char name[20];
int i=1;
head = (struct STUDENT*)malloc(sizeof(struct STUDENT));//申请头节点
q = head;
fgets(name,20,stdin);
while(strlen(name))
{
p = (struct STUDENT*)malloc(sizeof(struct STUDENT));
strcpy(p->name,name);
printf("请输入第%d位的年龄:",i);
scanf("%d",&p->age);
printf("请输入第%d位的工资:",i);
scanf("%f",&p->salary);
getchar();//吸收回车
q->next = p;
q = p;//q移动到最后一个节点
fgets(name,20,stdin);
i++;
}
q->next = NULL;
return head;
}
//打印链表
void print_list(struct STUDENT *head)
{
struct STUDENT *p;
p = head->next;
while(p!=NULL)
{
printf("姓名:%s\n年龄:%d\n工资:%f\n",p->name,p->age,p->salary);
p = p->next;
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询