马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
建立一个单链表,并将其输出?
出现的问题是:只能输入一个节点的数据,不能全部输入
#include<stdio.h>
#include<stdlib.h>
#define n 5
struct student{
int num;
char name[10];
char sex[10];
int age;
struct student *next;
};
struct student *jianli(struct student *head)
{
struct student *p,*q;
head=(struct student *)malloc(sizeof(struct student));
p=head=NULL;
int i;
for(i=0;i<n;i++)
{
q=(struct student *)malloc(sizeof(struct student));
q->next=NULL;
printf("请输入学号,姓名,性别,年龄\n");
scanf("%d %s %s %d",&(q->num),q->name,q->sex,&(q->age));
q->next=p->next;
p->next=q;
p=q;
}
p->next=NULL;
return head;
}
struct student *shuchu(struct student *head)
{
struct student *p,*q;
int x;
p=head;
q=p->next;
printf("请输入一个年龄");
scanf("%d",&x);
while(q!=NULL)
{
if(q->age==x)
p->next=q->next;
else
{
p=q;
q=q->next;
}
}
return head;
}
int main()
{ struct student *head,*p,*q;
jianli(head);
shuchu(head);
q=head->next;
while(q!=NULL)
{
printf("%d %10s %10s %d\n",q->num,q->name,q->sex,q->age);
q=q->next;
}
}
|