结构体与链表编程题及解答Word格式文档下载.docx
- 文档编号:22257047
- 上传时间:2023-02-03
- 格式:DOCX
- 页数:18
- 大小:17.75KB
结构体与链表编程题及解答Word格式文档下载.docx
《结构体与链表编程题及解答Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《结构体与链表编程题及解答Word格式文档下载.docx(18页珍藏版)》请在冰豆网上搜索。
,stu[i].num);
name:
stu[i].name);
for(j=0;
j<
3;
j++)
{
printf("
score%d."
j+1);
scanf("
%d"
&
stu[i].score[j]);
}
\n"
}
voidprint(structstudentstu[])
printf("
\nNo.NameScore1Score2Score3\n"
%-5s%-9s"
stu[i].num,stu[i].name);
%-8d"
stu[i].score[j]);
【程序2】
建立100名学生的信息表,每个学生的数据包括学号、姓名、及一门课的成绩,要求从键盘输入这100名学生的信息,并按照每一行显示一名学生信息的格式将他们的信息显示出来。
#defineN3
structstudent
intnum;
charname[20];
intscore;
};
voidmain()
{
inti;
structstudents[N];
/*定义结构体类型数组,长度100*/
inputnumber:
"
s[i].num);
inputname:
s[i].name);
inputscore:
s[i].score);
Number:
Name:
score:
%-8d%-10s%d\n"
s[i].num,s[i].name,s[i].score);
【程序3】
创建一个链表。
/*creatalist*/
#include"
stdlib.h"
structlist
intdata;
structlist*next;
};
typedefstructlistnode;
typedefnode*link;
linkptr,head;
intnum,i;
intj;
pleaseinputnumbers==>
scanf("
j);
ptr=(link)malloc(sizeof(node));
head=ptr;
pleaseenternumbers:
=j-1;
num);
ptr->
data=num;
next=(link)malloc(sizeof(node));
if(i==j-1)
ptr->
next=NULL;
else
ptr=ptr->
next;
ptr=head;
while(ptr!
=NULL)
Thevalueis==>
%d\n"
ptr->
data);
ptr=ptr->
==============================================================
【程序4】
反向输出一个链表。
1.程序分析:
2.程序源代码:
/*reverseoutputalist*/
linkptr,head,tail;
tail=(link)malloc(sizeof(node));
tail->
ptr=tail;
\npleaseinputdata==>
head=(link)malloc(sizeof(node));
head->
next=ptr;
ptr=head;
ptr=ptr->
【程序5】
已有a,b两个链表,每个链表中的的结点包括学号、成绩。
要求把两个链表合并,按学号升序排列
#include
<
stdlib.h>
typedef
struct
student
int
num;
float
score;
student
*next;
}STU;
STU
*create()
{
STU
*p,*head=NULL,*tail=head;
while
(1)
p=(STU
*)malloc(sizeof(STU));
scanf("
%d%f"
p->
num,&
score);
if
(p->
num<
0)
free(p);
break;
if(head==NULL)
head=p;
else
tail->
next=p;
tail=p;
return
head;
void
output(STU
*p)
while
(p!
printf("
%d\t%.2f\n"
p->
num,p->
p=p->
*link(STU
*p1,STU
*p2)
*p,*head;
(p1->
=p2->
num)
head=p=p1;
p1=p1->
head=p=p2;
p2=p2->
(p1!
=NULL&
&
p2!
next=p1;
p=p1;
next=p2;
p=p2;
if(p1!
main(int
argc,
char
*argv[])
*a,*b,*c;
\n请输入链表a的信息,学号小于零时结束输入:
格式(学号
成绩)\n"
a=create();
\n请输入链表b的信息,学号小于零时结束输入:
b=create();
\n链表a的信息为:
output(a);
\n链表b的信息为:
output(b);
c=link(a,b);
\n合并后的链表信息为:
output(c);
0;
【程序6】13个人转成一圈,从第一个人开始报数,报到3的退出,问最后退出的人原来的序号。
(用链表实现)
malloc.h>
#defineLENsizeof(structstudent)
structstudent*next;
intmain()
structstudent*head,*p1,*p2;
//p1指前,p2指后
head=p1=p2=(structstudent*)malloc(LEN);
p2->
num=1;
for(i=2;
13;
p1=(structstudent*)malloc(LEN);
p1->
num=i;
p2->
p2=p1;
p1=(structstudent*)malloc(LEN);
p1->
num=13;
next=head;
//建立链表完成
p2=p1;
p1=head;
while(p1->
next!
=p1)
for(i=0;
2;
{
p1=p1->
p2=p2->
No.%2dhasbeendeleted!
p1->
next=p1->
p1=p1->
\nThelastoneisNo.%d.\n"
return1;
【程序7】有两个链表a和b,设结点中包含学号、姓名。
从a链表中删去与b链表中有相同学号的那些结点。
i;
*del(STU
*a,STU
*b)
*head,*p1,*p2;
p1=p2=head=a;
//让p1、p2、head结点指向链表a的头部
(b!
p1=p2=head;
//每次循环前让p1、p2始终指向删除后链表的头部
(b->
num==p1->
num)
//学号相同,删除结点信息
if(p1==head)
//如果删除的是头结点,则头结点位置要后移
head=p1->
free(p1);
else
//如果删除的是中间结点
p2->
p1=p2->
//学号不同,则p1,p2指针依次后移
p2=p1;
b=b->
system("
cls"
c=del(a,b);
\n删除后的链表信息为:
【程序8】建立一个链表,每个结点包括:
学号、姓名、性别、年龄。
输入一个年龄,如果链表中的结点所包含的年龄等于此年龄,则将此结点删去。
#include<
#defineLENsizeof(structstudent)
structstudent
charsex[6];
intage;
}stu[10];
voidmain()
structstudent*p,*pt,*head;
inti,length,iage,flag=1;
intfind=0;
/*若找到待删除元素,find=1,否则find=0*/
while(flag==1)
inputlengthoflist(<
10):
length);
if(length<
10)
flag=0;
}
length;
i++)
p=(structstudent*)malloc(LEN);
if(i==0)
head=pt=p;
else
pt->
pt=p;
NO:
name);
sex:
sex);
age:
age);
p->
p=head;
\nNO.namesexage\n"
while(p!
=NULL)
%4s%8s%6s%6d\n"
num,p->
name,p->
sex,p->
p=p->
inputage:
iage);
/*输入待删除年龄*/
pt=head;
p=pt;
if(pt->
age==iage)/*链头是待删除元素*/
p=pt->
head=pt=p;
find=1;
else/*链头不是是待删除元素*/
pt=pt->
while(pt!
if(pt->
age==iage)
p->
next=pt->
find=1;
}
p=pt;
if(!
find)
Notfoundage%d."
iage);
while(p!
%4s%8s"
%6s%6d\n"
sex,p->
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 结构 编程 解答