/*判断指定字符串是否回文*/
#include
intfunc(char*str)
{
inti,j,t,flag;
t=strlen(str);
for(i=0,j=t-1;i<=t/2;i++,j--)
{
if(str[i]==str[j])continue;
else
{flag=0;break;}
}
if(i>=t/2)flag=1;
returnflag;
}
voidmain()
{
/********************blank********************/
____________________;/*填空位置1:
变量定义*/
____________________;/*填空位置2:
从键盘输入需要判定的字符串*/
____________________;/*填空位置3:
调用func函数*/
____________________;/*填空位置4:
根据返回值flag的值,输出判定结果*/
}
该算法的时间复杂度是:
____________________。
2.定义函数intf(char*x,chary)判断x所指的字符串是否包含字符y,若是则函数返回1,否则返回0。
写出完整的源程序代码。
#include
#include
intf(char*x,chary)/*写出自定义函数f的函数体部分,实现函数功能*/
{
}
voidmain()
{
char*str,ch;
intt;
gets(str);
scanf("%c",&ch);
t=f(str,ch);
printf("t=%d\n",t);
}
3.用结构体类型编写一个程序,输入一个学生的学号、姓名及3门课的成绩,计算并输出其平均成绩。
写出完整的源程序代码。
#include
structstudent
{
intid;
charname[10];
floatscore[3];
}s;
main()/*写出main函数的函数体部分,实现函数功能*/
{
}
实验2线性表
院(系):
信息科学与技术学院课程名称:
数据结构教师签名:
班级
学号
实验室
专业
姓名
计算机号
实验名称
所用软件
实验成绩
2.1实验目的
1.掌握顺序表的基本特点。
2.熟练掌握顺序表的建立、查找、插入和删除等操作。
3.掌握单链表的基本特点。
4.熟练掌握单链表的建立、插入、删除等基本操作。
5.理解循环链表、双向链表的含义及其特点。
6.了解循环链表、双向链表的基本操作。
2.2实验准备
1.线性表顺序存储结构的表示。
2.顺序表的基本操作:
顺序表的建立、查找、插入、删除。
3.线性表链式存储结构的表示。
4.链表的基本操作:
链表的建立、查找、插入、删除。
2.3实验任务
1.实现顺序表的各种基本操作。
(1)源程序代码。
#include
#defineMaxsize100
typedefintdatatype;
typedefstructseqnode/*顺序表的类型定义*/
{
datatypelist[Maxsize];/*定义一个存放顺序表的一维数组list*/
intlen;/*定义线性表的实际长度*/
}seqlist;
voidcreate(seqlist*L)/*建立一个顺序存储的线性表*/
{
inti;
printf("Pleaseinputthelenoftheseqlist:
");/*从键盘输入当前顺序表的实际长度*/
scanf("%d",&(*L).len);
printf("Pleaseinputtheeveryelementoftheseqlist:
");
for(i=0;i<(*L).len;i++)/*从键盘输入顺序表的每个元素*/
scanf("%d",&(*L).list[i]);
printf("Outputtheeveryelementoftheseqlist:
\n");
for(i=0;i<(*L).len;i++)/*顺序表建立成功后,输出整个顺序表*/
printf("%5d",(*L).list[i]);/*运行结果
(1)*/
printf("\n");
}
voidaccess(seqlist*L,inti)/*根据指定位置访问线性表*/
{
if((i<0)||(i>(*L).len-1))/*判断给定位置是否为合法取值*/
printf("Theplaceisnotcorrect!
\n");
else
printf("%d\n",(*L).list[i]);/*运行结果
(2)*/
}
voidbefore_after(seqlist*L,inti)/*根据指定位置寻找其前趋元素和后继元素*/
{
if((i<0)||(i>(*L).len-1))
printf("Theplaceisnotcorrect!
\n");
elseif(i==0)
printf("%d\n",(*L).list[i+1]);
elseif(i==(*L).len-1)
printf("%d\n",(*L).list[i-1]);
elseif((i>0)&&(i<(*L).len-1))
printf("%d,%d\n",(*L).list[i-1],(*L).list[i+1]);/*运行结果(3)*/
}
voidsearch(seqlist*L,intkey)/*根据给定元素key查找顺序表*/
{
intm;
for(m=0;m<(*L).len;m++)
{
if((*L).list[m]!
=key)
continue;
else
{
printf("Seachingisseccessful!
\n");
printf("Theplaceofthesearchkeyis:
%d\n",m);
break;
}
}
if(m>=(*L).len)
printf("Theseqlisthasnothekey!
\n");/*运行结果(4)*/
}
voiddelete(seqlist*L,inti)/*删除顺序表的元素*/
{
intm;
printf("Outputtheeveryelementoftheseqlistbeforedeleting:
\n");/*删除前输出顺序表中的所有元素*/
for(m=0;m<(*L).len;m++)
printf("%5d",(*L).list[m]);/*运行结果(5)*/
printf("\n");
for(m=i;m<(*L).len;m++)/*删除位置之后的所有元素依次左移一位*/
(*L).list[m]=(*L).list[m+1];
(*L).len--;
printf("Outputtheeveryelementoftheseqlistafterdeleting:
\n");/*删除后输出顺序表中的所有元素*/
for(m=0;m<(*L).len;m++)
printf("%5d",(*L).list[m]);/*运行结果(6)*/
printf("\n");
}
voidinsert(seqlist*L,inti,inte)/*在顺序表指定位置i后插入元素e*/
{
intm;
printf("Outputtheeveryelementoftheseqlistbeforeinserting:
\n");/*插入前输出顺序表中的所有元素*/
for(m=0;m<(*L).len;m++)
printf("%5d",(*L).list[m]);/*运行结果(7)*/
printf("\n");
(*L).len++;
for(m=(*L).len-1;m>=i;m--)/*插入位置之后的元素依次右移一位*/
(*L).list[m+1]=(*L).list[m];
(*L).list[i]=e;/*在指定位置i上插入元素e*/
printf("Outputtheeveryelementoftheseqlistafterinserting:
\n");/*插入后输出顺序表中的所有元素*/
for(m=0;m<(*L).len;m++)
printf("%5d",(*L).list[m]);/*运行结果(8)*/
printf("\n");
}
voidmain()
{
staticseqlist*L;
inti,e;
printf("\n--------------------function:
create--------------------\n");
create(L);/*调用函数create建立一个顺序表*/
printf("\n--------------------function:
access-----------