《数据结构实验与实训教程第3版》课件Word文档下载推荐.docx
《《数据结构实验与实训教程第3版》课件Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《《数据结构实验与实训教程第3版》课件Word文档下载推荐.docx(110页珍藏版)》请在冰豆网上搜索。
#include<
stdio.h>
intsumabc(inta,intb,intc)/*求三个整数之和*/
{ints;
a=b+c;
s=a+b+c;
returns;
}
voiddisplayLine(void)
{printf(”----------------------\n“);
voidmain()
{intx,y,z,sabc;
x=y=z=8;
display();
/*画一条线*/
printf(“\nsum=%d”,sumabc(x,y,z));
/*在输出语句中直接调用函数sumabc()*/
printf(“\n%6d%6d%6d”,x,y,z);
/*画一条线*/
x=2;
y=4;
z=6;
sabc=sumabc(x,y,z);
/*在赋值语句中调用函数sumabc()*/
printf(“\n“sum=%d”,sabc);
}
例1.2
intsumabc(int*a,intb,intc)
{
ints;
*a=b+c;
s=*a+b+c;
}
预备知识实验
intmain()
{//在main函数中调用上述声明的函数
intn;
//记录个数
STUDENTstu[MAXSIZE;
//顺序存储结构,方法一静态一维数组。
/*
顺序存储结构,方法二动态一维数组,用malloc函数分配如下:
STUDENT*stu;
stu=(STUDENT*)malloc(sizeof(STUDENT)*MAXSIZE);
//内存空间的分配
注意:
分配空间可用malloc()函数,释放空间用free()函数,如free(stu);
*/
intindex;
printf("
\n请输入学生记录个数n="
);
scanf(%d”,&
n);
InputStu(stu,n);
//预先处理输入,建表
while
(1)//永真循环,重复显示菜单,直至退出
{
printf("
\n**********************学生信息管理主菜单**********************\n"
\t1.显示学生信息\n"
\t2.查找学生信息\n"
\t3.修改学生信息\n"
\t4.添加学生信息\n"
\t5.退出\n\n"
\t\t请选择(1~5):
"
scanf("
%d"
&
index);
\n**************************************************************\n"
switch(index){
case1:
OutputStu(stu,n);
break;
case2:
SearchStu(stu,n);
case3:
UpdateStu(stu,n);
case4:
AppendStu(stu,&
case5:
return0;
default:
\n输入有误,请重新输入!
\n"
}//switch
}//while
(1)
}//main
第二部分基础实验
实验1线性表的基本操作
四、参考程序
程序1:
题1线性表基本操作函数
#include<
stdlib.h>
alloc.h>
structLinearList/*定义线性表结构*/
{
int*list;
/*存线性表元素*/
intsize;
/*存线性表长度*/
intMaxSize;
/*存list数组元素个数*/
};
typedefstructLinearListLIST;
voidInitList(LIST*L,intms)/*初始化线性表*/
if((L->
list=1)==NULL){
printf("
内存申请错误!
\n"
);
exit
(1);
2
L->
MaxSize=ms;
intInsertList(LIST*L,intitem,intrc)
/*item:
记录值rc:
插入位置*/
inti;
if(3)/*线性表已满*/
return-1;
if(rc<
0)/*插入位置为0-->
size*/
rc=0;
if(4)
rc=L->
size;
for(i=L->
size-1;
i>
=rc;
i--)/*将线性表元素后移*/
5
list[rc]=item;
size++;
voidOutputList(LIST*L)/*输出线性表元素*/
for(i=0;
6i++)
%d"
L->
list[i]);
printf("
intFindList(LIST*L,intitem)/*返回>
=0为元素位置-1没找到*/
i<
i++)
if(7)/*找到相同的元素,返回位置*/
returni;
return-1;
/*没找到*/
intDeleteList1(LIST*L,intitem)
/*删除指定元素值的线性表记录,返回>
=0:
删除成功*/
inti,n;
if(item==L->
list[i])/*找到相同的元素*/
break;
if(i<
size){
for(n=i;
n<
n++)
L->
list[n]=L->
list[n+1];
L->
size--;
returni;
intDeleteList2(LISTL,intrc)/*删除指定位置的线性表记录*/
8/*编写删除指定位置的线性表记录子程序*/
程序2:
题2
voidmain()
LISTLL;
inti,r;
listaddr=%p\tsize=%d\tMaxSize=%d\n"
LL.list,LL.size,LL.MaxSize);
InitList(&
LL,100);
while
(1)
请输入元素值,输入0结束插入操作:
"
fflush(stdin);
/*清空标准输入缓冲区*/
scanf("
&
i);
if
(1)
break;
请输入插入位置:
r);
InsertList
(2);
线性表为:
3
请输入查找元素值,输入0结束查找操作:
if(i==0)
r=4
if(r<
0)
printf("
没找到\n"
else
有符合条件的元素,位置为:
%d\n"
r+1);
请输入删除元素值,输入0结束查找操作:
r=5
else{
%d\n线性表为:
OutputList(&
LL);
请输入删除元素位置,输入0结束查找操作:
if(r==0)
i=6
位置越界\n"
线性表为:
程序4:
题4
#defineX10
#defineY30
#defineN20
intA[N]={2,5,15,30,1,40,17,50,9,21,32,8,41,22,49,31,33,18,80,5};
voiddel(int*A,int*n,intx,inty)
inti,j;
for(i=j=0;
*n;
if(A[i]>
y||A[i]<
x)//不在x到y之间,则保留
1;
2=j;
voidoutput(int*A,intn)
inti