算法与数据结构实验报告.docx
- 文档编号:25135383
- 上传时间:2023-06-05
- 格式:DOCX
- 页数:49
- 大小:26.09KB
算法与数据结构实验报告.docx
《算法与数据结构实验报告.docx》由会员分享,可在线阅读,更多相关《算法与数据结构实验报告.docx(49页珍藏版)》请在冰豆网上搜索。
算法与数据结构实验报告
算法与数据结构实验报告
学院:
计算机与信息学院
专业__
__
__
实验一栈和队列
实验目的:
掌握栈和队列特点、逻辑结构和存储结构
熟悉对栈和队列的一些基本操作和具体的函数定义.
利用栈和队列的基本操作完成一定功能的程序.
实验任务:
1.给出顺序栈的类定义和函数实现,利用栈的基本操作完成十进制数N与其它d进制数的转换.〔如N=1357,d=8〕
实验原理:
将十进制数N转换为八进制时,采用的是"除取余数法〞,即每次用8除N所得的余数作为八进制数的当前个位,将相除所得的商的整数部分作为新的N值重复上述计算,直到N为0为止.此时,将前面所得到的各余数反过来连接便得到最后的转换结果.
程序清单:
#include
#include
usingnamespacestd;
typedefintDATA_TYPE;
constintMAXLEN=100;
enumerror_code
{
success,overflow,underflow
};
classstack
{
public:
stack<>;
boolempty<>const;
error_codeget_top
error_codepush
error_codepop<>;
boolfull<>const;
private:
DATA_TYPEdata[MAXLEN];
intcount;
};
stack:
:
stack<>
{
count=0;
}
boolstack:
:
empty<>const
{
returncount==0;
}
error_codestack:
:
get_top
if
returnunderflow;
else
{
x=data[count-1];
returnsuccess;
}
}
error_codestack:
:
push
{
if
returnoverflow;
else
{
data[count]=x;
count++;
}
}
error_codestack:
:
pop<>
{
if
returnunderflow;
else
{
count--;
returnsuccess;
}
}
boolstack:
:
full<>const
{
returncount==MAXLEN;
}
voidmain<>
{
stackS;
intN,d;
cout<<"请输入一个十进制数N和所需转换的进制d"< cin>>N>>d; if { cout<<"输出转换结果: "< } while S.push N=N/d; } cout<<"输出转换结果: "< while S.empty<>> { S.get_top cout< S.pop<>; } cout< } while S.empty<>> { S.get_top cout< S.pop<>; } } 测试数据: N=1348d=8 运行结果: 2.给出顺序队列的类定义和函数实现,并利用队列计算并打印杨辉三角的前n行的内容. 实验原理: 杨辉三角的规律是每行的第一和最后一个数是1,从第三行开始的其余的数是上一行对应位置的左右两个数之和.因此,可用上一行的数来求出对应位置的下一行内容.为此,需要用队列来保存上一行的内容.每当由上一行的两个数求出下一行的一个数时,其中 的前一个便需要删除,而新求出的数就要入队. 程序清单: #include #include usingnamespacestd; typedefintDATA_TYPE; constintMAXLEN=100; enumerror_code { success,underflow,overflow }; classqueue { public: queue<>; boolempty<>const; error_codeget_front boolfull<>const; private: intfront,rear; DATA_TYPEdata[MAXLEN]; }; queue: : queue<> { rear=0; front=0; } boolqueue: : empty<>const { return } error_codequeue: : get_front if returnunderflow; else { x=data[front%MAXLEN]; returnsuccess; } } error_codequeue: : append if returnoverflow; else { data[rear%MAXLEN]=x; rear++; } } error_codequeue: : serve<> { if returnunderflow; else { front++; returnsuccess; } } boolqueue: : full<>const { return< voidmain<> { queueQ; intnum1,num2; inti=0; cout<<1< Q.append<1>; num1=0; num2=1; for { intj=0; intk=0; num1=0; for { Q.get_front Q.serve<>; cout< Q.append num1=num2; } cout<<1< Q.append<1>; } } 运行结果: 3.给出链栈的类定义和函数实现,并设计程序完成如下功能: 读入一个有限大小的整数n,并读入n个数,然后按照与输入次序相反的次序输出各元素的值. 实验原理: 依次将栈中的元素出栈,因为栈的一个特点就是先进后出,这样,当将原栈为空时,输出与输入次序相反,从而实现了本题的要求. 程序清单: #include #include usingnamespacestd; typedefintDATA_TYPE; typedefstructLNode { DATA_TYPEdata; LNode*next; }LNode; enumerror_code { range_error,success,underflow classlinkstack { public: linkstack<>; ~linkstack<>; boolempty<>const; error_codepush error_codeget_top error_codepop<>; private: LNode*top; intcount; DATA_TYPEdata; }; linkstack: : linkstack<> { top=NULL; count=0; } boollinkstack: : empty<>const { return } error_codelinkstack: : push { LNode*s=newLNode; s->data=x; s->next=top; top=s; count++; returnsuccess; } error_codelinkstack: : get_top { if returnunderflow; else { x=top->data; returnsuccess; } } error_codelinkstack: : pop<> if returnunderflow; else { LNode*u=newLNode; u=top; top=top->next; deleteu; count--; returnsuccess; } } linkstack: : ~linkstack<> { while empty<>> { pop<>; } } voidmain<> { linkstackL; intn; cout<<"请任意输入一个整数n: "< cin>>n; for { L.push; } while L.empty<>> { L.get_top; cout< L.pop<>; } } 测试数据: n=9i=1 运行结果: 实验二单链表 实验目的: 理解线性表的链式存储结构. 熟练掌握动态链表结构与有关算法的设计. 根据具体问题的需要,设计出合理的表示数据的链表结构,并设计相关算法. 实验任务: 1.在一个递增有序的链表L中插入一个值为x的元素,并保持其递增有序特性. 实验数据: 链表元素为〔10,20,30,40,50,60,70,80,90,100〕,x分别为25,85,110和8. 实验原理: 给出了要插入的条件,但没有给定插入位置.因此,需要搜索满足这一条件的插入位置的前驱结点而不是序号. 程序清单: #include usingnamespacestd; enumerror_code{ success, overflow, underflow, rangeerror }; typedefintelementtype; typedefstructLinkNode{ elementtypedata; structLinkNode*next; }node; classlist{ private: intcount; node*head; public: list<>; ~list<>{}; public: error_codeget_element node*get_head<>const{returnhead;} voidcreate<>; voiddisplay<>; voidinsert1 }; list: : list<>{ head=newnode; head->next=NULL; count=0; } voidlist: : create<>{ elementtypex;node*s,*rear; cin>>x; rear=head; while =-9999>{ count++; s=newnode; s->data=x; s->next=NULL; rear->next=s; rear=s; cin>>x; } } voidlist: : display<>{ node*p; p=head->next; while =NULL>{ cout< p=p->next; } cout< } voidlist: : insert { node*u,*P; P=head; while =NULL&&P->next->data P=P->next; if {u=newnode; u->data=x; u->next=P->next; P->next=u; count++; } } voidmain<> { listL; elementtypex; L.create<>; L.display<>; cout<<"请输入要插入的值x"< cin>>x; L.insert L.display<>; } 运行结果: X=25 X=85 X=110 X=8 2.将单链表L中的奇数项和偶数项结点分解开,并分别连成一个带头结点的单链表,然后再将这两个新链表同时输出在屏幕上,并保留原链表的显示结果,以便对照求解结果. 实验测试数据: 第一组数据: 链表为〔1,2,3,4,5,6,7,8,9,10,20,30,40,50,60〕 第二组数据: 链表为〔10,20,30,40,50,60,70,80,90,100〕 实验原理: 依据题目的要求,需要再创建两个新链表来存储分离后的奇偶项,而奇偶项可以根据数字来控制,把他们取出来并重新连起来. 程序清单: #include usingnamespacestd; enumerror_code{ success, overflow, underflow, rangeerror }; typedefintelementtype; typedefstructLinkNode{ elementtypedata; structLinkNode*next; }node; classlist{ private: intcount; node*head; public: list<>; ~list<>{}; public: node*get_head<>const{returnhead;} voidcreate<>; voiddisplay<>; }; list: : list<>{ head=newnode; head->next=NULL; count=0; } voidlist: : create<>{ elementtypex;node*s,*rear; cin>>x; rear=head; while =-9999>{ count++; s=newnode; s->data=x; s->next=NULL; rear->next=s; rear=s; cin>>x; } } voidlist: : display<>{ node*p; p=head->next; while =NULL>{ cout< p=p->next; } cout< } voiddivide { node*PA,*PB,*PC,*s; PB=B.get_head<>; PC=C.get_head<>; PA=A.get_head<>->next; while =NULL> { if< =0> { s=newnode; s->data=PA->data; PB->next=s; PB=s; PB->next=NULL; PA=PA->next; } else { s=newnode; s->data=PA->data; PC->next=s; PC=s; PC->next=NULL; PA=PA->next; } } } voidmain<> { listL,L1,L2; L.create<>; divide cout<<"原链表: "< L.display<>; cout<<"偶链表: "< L2.display<>; cout<<"奇链表: "< L1.display<>; 实验结果: 第一组数据: 第二组数据: 1.求两个递增有序链表L1和L2中的公共元素,并以同样方式连接成链表L3. 实验测试数据: 第一组数据: 第一个链表为〔1,3,6,10,15,16,17,18,19,20〕 第二个链表为〔1,2,3,4,5,6,7,8,9,10,18,20,30〕 第二组数据: 第一个链表元素为〔1,3,6,10,15,16,17,18,19,20〕 第二个链表元素为〔2,4,5,7,8,9,12,22〕 实验原理: 设置两个指针怕,pa,pb分别依次指示A,B表中的元素,其初始值分别为A.head->next和B.head->next.在pa,pb均非空时,根据其值的大小关系可能有如下三种情况. <1>.pa->data==pb->data: 搜索到公共元素,应在C表表尾插入一个结点,其值为pa->data,然后继续A表中下一个元素的搜索,即pa=pa->next,同时pb也往后移. <2>.pa->data>pb->data: 表明A表中这一元素可能在B表当前元素的后面,因此要往B表的后面搜索,故而执行pb=pb->next,然后继续搜索. <3>.pa->data 表明A中这一元素在B中不存在,因而执行pa=pa->next以继续对A表中下一个元素的判断.反复执行上述比较,直到pa,pb至少有一个为空为止.此时,剩余的非空部分没有所需要的公共元素,因而搜索结束. 程序清单: #include #include usingnamespacestd; typedefstructsnode { intdata; structsnode*next; }node; enumerror_code{arrange_error,success}; classlist { public: list<>; voidcreate2<>; intlength<>const; error_codeget_element error_codeinsert error_codedelete_element node*locate node*get_head<>{returnhead;} voidlist: : gongyou voidprint<>; private: intcount; node*head; }; list: : list<> { head=newnode;head->next=NULL; count=0; } voidlist: : create2<> { intx;node*p=head; node*s;cout<<"输入一个值: "; cin>>x; while =-1> { s=newnode;s->data=x; s->next=NULL;p->next=s; p=s;cout<<"输入一个值: "; cin>>x; } } intlist: : length<>const { returncount; } error_codelist: : get_element { intj=1;node*p=head->next; while =NULL&&j! =i> { p=p->next;j++; } if returnarrange_error; x=p->data; returnsuccess; } voidlist: : gongyou { node*p1=L1.head->next;node*p2=L2.head->next; node*p3=head;node*u; while =NULL&&p2! =NULL> { if { u=newnode;u->data=p1->data;p3->next=u;p1=p1->next;p2=p2->next;p3=p3->next; } else{ if p1=p1->next; else p2=p2->next; } } p3->next=NULL; } node*list: : locate { node*p=head->next; while =NULL> { if returnp; p=p->next; } returnNULL; } voidlist: : divide { node*u;node*pa=head->next; node*pb=B.get_head<>;node*pc=C.get_head<>;for =NULL;i++,pa=pa->next> { u=newnode;u->data=pa->data; if { pb->next=u;pb=pb->next; } else {
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 算法 数据结构 实验 报告
![提示](https://static.bdocx.com/images/bang_tan.gif)