建材秒知道
登录
建材号 > 链条 > 正文

建立一个链表,每个节点包括学生的学号、姓名、性别、年龄,学号

俊逸的花瓣
爱笑的小鸽子
2023-02-14 12:19:36

建立一个链表,每个节点包括学生的学号、姓名、性别、年龄,学号

最佳答案
笨笨的心锁
凶狠的蓝天
2026-05-17 04:56:19

# include"stdio.h"

#include"stdlib.h"

struct data

{

int num

char name[20]

char sex[4]

int age

struct data*next

}

int main()

{

int i,k

struct data*head,*p,*q,*t

head=t=p=q=(struct data*)malloc(sizeof(struct data))

printf("输入学号:")

scanf("%d",&p->num)

getchar()

printf("输入姓名:")

gets(p->name)

printf("输入性别:")

gets(p->sex)

printf("输入年龄:")

scanf("%d",&p->age)

while(p->num!=-1)

{

q->next=p

q=p

p=(struct data*)malloc(sizeof(struct data))

   printf("输入学号:")

scanf("%d",&p->num)

getchar()

if(p->num==-1)  break

printf("输入姓名:")

gets(p->name)

printf("输入性别:")

gets(p->sex)

printf("输入年龄:")

scanf("%d",&p->age)

}

q->next=NULL

free(p)

printf("输出的结果是:\n")

while(t!=NULL)

{printf("%-5d%8s%8s%8d",t->num,t->name,t->sex,t->age)

t=t->next

   printf("\n")

}

printf("请输入你要查找的学生的学生号:")

scanf("%d",&i)

p=head

while(p!=NULL)

{

if(i == p->num)

{

printf("\n%-5d%8s%8s%8d\n",p->num,p->name,p->sex,p->age)

}

p=p->next

}

return 0

}

姐姐啊!你要两个学号啊!我给你编了可以查学号的!祝你成功!

我运行的情况!

最新回答
可靠的镜子
怕孤单的季节
2026-05-17 04:56:19

我写过类似的,

你稍微改一下!这个是输入两个整数链表,然后相同的删除,比如一个输入1,2,3,4,0是结束,第二个输入1,2,0那么结果就是3,4!

#include

#include

#include

using

namespace

std

typedef

int

elemtype

//定义数据域的类型

typedef

struct

linknode

//定义节点类型

{

elemtype

data

struct

linknode

*next

}

nodetype

nodetype

*create()

//建立单链表,由用户输入各节点data域的值,以0表示输入结束

{

elemtype

d

nodetype

*h=null,*s,*t

int

i=1

cout<<"建立一个单链表"<

>d

if(d==0)break

if(i==1)

{

h=(nodetype

*)malloc(sizeof(nodetype))

h->data=d

h->next=null

t=h

}

else

{

s=(nodetype

*)malloc(sizeof(nodetype))

s->data=d

s->next=null

t->next=s

t=s//t一直指向最后一个节点

}

i++

}

return

h

}

void

disp(nodetype

*h)

{

nodetype

*p=h

cout<<"输出一个单链表:"<

data<<"

"

p=p->next

}

cout<

next=null

r

=

head

p

=

ha

while(p!=null)

{

q=hb

while(q!=null&&p->data!=q->data)

q=q->next

if(q!=null)

{

r->next

=

p->next

free(p)

p

=

r->next

}

else

{

r->next

=

p

r

=

p

p

=

p->next

r->next=null

}

}

s

=

head

head

=

head->next

free(s)

return

head

}

void

main()

{

nodetype

*ha,*hb,*s

ha=create()

disp(ha)

hb=create()

disp(hb)

s=sub(ha,hb)

disp(s)

}

超级的日记本
瘦瘦的美女
2026-05-17 04:56:19
#include<stdio.h>

#include<malloc.h>

#define LEN sizeo(struct student)

struct student

{

char num[6]

char name[8]

char sex[2]

int age

struct student *next

}stu[10]

void main()

{

struct student *p,*pt,*head

int i,length,iage,flag=1

int find=0

while(flag==1)

{

printf("input length of list(<10):")

scanf("%d",&length)

if(length<10)

flag=0

}

for (i=0i<lengthi++)

{

p=(struct student * ) malloc(LEN)

if(i==0)

head=pt=p

else

pt->next=p

pt=p

printf("No.:")

scanf("%s",p->num)

printf("name:")

scanf("%s",p->name)

printf("sex:")

scanf("%s",p->sex)

printf("age:")

scanf("%d",&p->age)

}

p->next=NULL

p=head

printf("\n No. name sex age\n")

while(p!=NULL)

{

printf("%4s%8s%6s%6d\n",p->num,p->name,p->sex,p->age)

p=p->next

}

printf("input age:")

scanf("%d",&iage)

pt=head

p=pt

if(pt->age==iage)

{

p=pt->next

head=pt=p

find=1

}

else

pt=pt->next

while(pt!=NULL)

{

if(pt->age==iage)

{

p->next=pt->next

find=1

}

else

p=pt

pt=pt->next

}

if(!find)

printf("not found %d.",iage)

p=head

printf("\n No. name sex age\n")

while(p!=NULL)

{

printf("%4s%8s",p->num,p->name)

printf("%6s%6d\n",p->sex,p->age)

p=p->next

}

}

阔达的人生
自然的鞋垫
2026-05-17 04:56:19
#include

#include

#include

typedef

struct

student

{

unsigned

id

//

学号

char

name[20]

unsigned

age

struct

student

*next}*LinkList,*pNodeLinkList

GetEmptyList()

{

LinkList

head

=

(pNode)malloc(sizeof(struct

student))

head->next

=

NULL

return

head}void

AddNode(LinkList

head,

pNode

newnode)

{

newnode->next

=

head->next

head->next

=

newnode}void

WhatName(LinkList

head,

unsigned

age)

{

pNode

p

=

head,q

while(p->next)

{

if(p->next->age

==

age)

{

q

=

p->next

p->next

=

q->next

free(q)

}

else

p

=

p->next

}}void

ShowList(LinkList

head)

{

pNode

p

=

head->next

while(p)

{

printf("%u\t%s\t%u\n",p->id,p->name,p->age)

p

=

p->next

}}int

main()

{

unsigned

id,age

char

name[20]

pNode

newnode

LinkList

head

=

GetEmptyList()

printf("学号

姓名

年龄(q

to

quit):")

while(scanf("%u%s%u",&id,name,&age)

==

3)

{

newnode

=

(pNode)malloc(sizeof(struct

student))

newnode->id

=

id

newnode->age

=

age

strcpy(newnode->name,name)

AddNode(head,newnode)

printf("学号

姓名

年龄(q

to

quit):")

}

ShowList(head)

WhatName(head,21)

//

删除年龄为21周岁的学生(荒唐)

ShowList(head)

return

0}

激昂的中心
糊涂的鸵鸟
2026-05-17 04:56:19
首先,你的while控制while(flag=1) 里面是两个等于,这样写只会死循环,一直停在这儿。

其次,scanf("%d\n",&length), 里面的\n去掉。

第三,scanf("%d\n%s\n%s\n%d\n",&p->num,p->name,p->sex,&p->age)别用\n做分隔符,用逗号scanf("%d,%s,%s,%d",&p->num,p->name,p->sex,&p->age)

最后scanf("%d\n",&iage)把\n去掉。

改完就能运行了!

现在,批评一下你的代码,你这代码给的,一点注释都没有,相关变量是干什么的要标示出来,要不别人怎么去阅读你的代码。

还有,以后给这么长的代码,而且还是又长又难的,放点财富值,要不然大部分人看了就没兴趣了