C语言综合实验设计报告1.doc
文本预览下载声明
C语言综合实验设计报告
题目:假设有两个链表a和b,链表按学号顺序保存着学生的考试成绩,将两个链表合并,合并后的链表也要求按学号顺序排列。
学院:电气与电子工程学院
专业:测控技术与仪器
班级
姓名
设计日期: 年 月 日
总分:
一、设计题目:
假设有两个链表a和b,链表按学号顺序保存着学生的考试成绩,将两个链表合并,合并后的链表也要求按学号顺序排列。
二、题目阐述及设计思路:
分析:新建一空链表c,将表a和表b中的结点逐个插入到c中。使用3个指针pa、pb、pc,其中pa和pb分别指向a表和b表中当前待比较插入的结点,而pc指向表c中当前最后一个结点。若pa-num=pb-num,则将pa所指向结点链接到pc所指向的结点之后,否则将pb所指向结点链接到pc所指向的结点之后。当一个表的结点插入完毕后,将另一个表中的剩余结点链接在pc所指结点之后即可。
三、主要知识点: while语句、if-else语句、条件语句、指针
四:程序清单:
#define NULL 0
struct stunode
{
int num;
float score;
struct stunode *next;
};
struct stunode *Merge(ah,bh)
struct stunode *ah,*bh;
{
struct stunode *pa,*pb,*pc,*ch;
pa=ah;
pb=bh;
ch=NULL;
while(pa!=NULLpb!=NULL)
{
if(pa-num=pb- num)
{
if(ch= =NULL)
{ ch=pa;pc=ch;}
pc-next=pa;
pc=pa;
pa=pa-next;
}
else
{
if(ch= =NULL)
{
ch=pb;
pc=ch;
}
pc-next=pb;
pc=pb;
pb=pb-next;
}
}
pc-next=pa?pa:pb;
return(ch);
}
struct stunode *Create()
{
int x;
float y;
struct stunode *head, *p, *q;
head=NULL;
q=NULL;
printf(“put in the data in order”);
scanf(“%d,%f”,x,y);
while(x0)
{
p=(struct stunode *)malloc(sizeof(struct stunode));
p-num=x;
p-score=y;
p-next=NULL;
if(head= =NULL)
head=p;
else
q-next=p;
q=p;
scanf(“%d,%f”,x,y);
}
return(head);
}
void print(p)
struct stunode *p;
{
while(p!=NULL)
{
printf(“%d,%5.2f\n”,p-num,p-score);
p=p-next;
}
}
main()
{
struct stunode *pa,*pb,*pc;
pa=Create();
print(pa);
pb=Create();
print(pb);
pc=Merge(pa,pb);
print(pc);
}
运行结果:
put in the data in order
101,88
103,89
0
put in the data in order
102,90
104,91
0
101,88.00
102,90.00
103,89.00
104,91.00
五:设计结果说明:
设计优点:
可以很好的将两个链表合并成一个链表,程序无错。
设计不足:
程序太过复杂,不易懂。
显示全部