实验报告课程名称 数据结构
姓名
学号
专业班级指导教师
目录
第二章线性表的查找、插入、删除. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1
1 .1顺序表的查找. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1
1 .2顺序表的插入. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3
1 .3顺序表的删除. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7
单链表的建立、插入、删除. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .10
2.1单链表的建立尾插法. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .10
2.2单链表的插入. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .13
2.3单链表的删除. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .17
第三章栈. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .24
3.1链栈. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .24
3.2顺序栈. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .28
3.3队列. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .30
3.4杨辉三角. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .35
第四章串. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . .23
4.1字符串的建立. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .23
4.2顺序串的插入. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .25
1 .线性表的查找、插入、删除
1 .1顺序表的查找
程序
#include<stdio.h>
#include<stdl ib.h>
#include<mal loc.h>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define ElemType int
#define MAXSIZE 100/*此处的宏定义常量表示线性表可能达到的最大长度*/typedef struct
{
ElemType elem[MAXSIZE]; /*线性表占用的数组空间*/int last; /*记录线性表中最后一个元素在数组elem[]中的位置(下标值)空表为-1*/
}Seq{l ist;int Locate(Seql ist L,ElemType e)
/*在顺序表L中查找与e相等的元素若L。 elem[i]=e,贝U找到该元素并返回i+1 若找不到则返回-1*/
{ int i=0; /*i为扫描计数器初值为0 即从第一个元素开始比较*/whi le
((i<=L. last)&&(L.elem[i]!=e))
/*顺序扫描表直到找到值为e的元素或扫描到表尾仍没找到*/ i++;if(i<=L. last)retu rn(i+1); /*若找到值为e的元素 贝返回其序号*/elseretu rn(-1); /*若没找到 贝返回空序号*/
}void main()
{
Seql ist l ;intp,q,r;int i ;printf("请输入线性标的长度:");scanf("%d",&r);l . last=r-1;printf("请输入线性表的各元素值 \n");for (i=0; i<=l . last; i++)
}printf("请输入要查找的元素值:\n");sea nf("%d",&q);p=Locate(l ,q);if(p==-1)printf("在此线性表中没有该元素!\n");elseprintf("该素在线性表中的位置为:%d\n",p);
执行结果:
错误分析:在编写过程中在编写主函数的时候有落下未编写的导致运行过程中不识别。
1 .2顺序表的插入
程序:
#include<stdio.h>#include<stdl ib.h>//#include<mal loc.h>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define ElemType int
#define MAXSIZE 100typedef struct
{
ElemType elem[MAXSIZE];int last;
}SeqList;
//#include"common.h"
//#include"seql ist.h"int InsList(SeqList *L, int i ,ElemType e){
intk;if((i<1) | | (i>L->last+2))
{printf("插入位置i值不合法"); return(ERROR);}if(L->last>=MAXSIZE-1)
{printf("表已满无法插入"); return(ERROR);}for(k=L->last;k>=i-1;k--)
L->elem[k+1]=L->elem[k];
L->elem[i-1]=e;
L->last++;retu rn(OK);
}void main()
SeqList *l ;
{
intp,q,r;int i ; l=(SeqList*)mal loc(sizeof(SeqList));printf("请输入线性表的长度:");scanf("%d",&r);l->last=r-1;printf("请输入线性表的各元素值:\n");for(i=0; i<=l->last; i++)
{scanf("%d",&l->elem[i]);
}printf("请输入要插入的位置:\n");scanf("%d",&p);printf("请输入要插入的元素值:\n");scanf("%d",&q);
InsList(l ,p,q);for(i=0; i<=l->last; i++)
{printf("%d", l->elem[i]);
}
执行结果
}
photonvps怎么样?photonvps现在针对旗下美国vps推出半价促销优惠活动,2.5美元/月起,免费10Gbps DDoS防御,Linux系统,机房可选美国洛杉矶、达拉斯、芝加哥、阿什本。以前觉得老牌商家PhotonVPS贵的朋友可以先入手一个月PhotonVPS美国Linux VPS试试了。PhotonVPS允许合法大人内容,支持支付宝、paypal和信用卡,30天退款保证。Photo...
Hostigger 主机商在前面的文章中也有介绍过几次,这个商家运营时间是有一些年份,只不过在我们圈内好像之前出现的次数不多。最近这段时间商家有提供不限流量的VPS主机,逐渐的慢慢被人认识到。在前面的介绍到他们提供的机房还是比较多的,比如土耳其、美国等。今天看到Hostigger 商家居然改动挺大的,原来蛮好的域名居然这次连带官方域名都更换掉去掉一个G(Hostiger )。估摸着这个域名也是之前...
virmach送来了夏季促销,价格低到爆炸,而且在低价的基础上还搞首年8折,也就是说VPS低至7.2美元/年。不过,这里有一点要说明:你所购买的当前的VPS将会在09/30/2021 ~ 04/30/2022进行服务器转移,而且IP还会改变,当前的Intel平台会换成AMD平台,机房也会变动(目前来看以后会从colocrossing切换到INAP和Psychz),采取的是就近原则,原来的水牛城可能...