1、算法与数据结构实验报告算法与数据结构实验报告算法与数据结构实验报告学院:计算机与信息学院专业班级:姓名:学号:实验一栈和队列实验目的:掌握栈和队列特点、逻辑结构和存储结构熟悉对栈和队列的一些基本操作和具体的函数定义。利用栈和队列的基本操作完成一定功能的程序。实验任务:1.给出顺序栈的类定义和函数实现,利用栈的基本操作完成十进制数N与其它d进制数的转换。(如N=1357,d=8)实验原理:将十进制数N转换为八进制时,采用的是“除取余数法”,即每次用8除N所得的余数作为八进制数的当前个位,将相除所得的商的整数部分作为新的N值重复上述计算,直到N为0为止。此时,将前面所得到的各余数反过来连接便得到最
2、后的转换结果。程序清单:#include#includeusingnamespacestd;typedefintDATA_TYPE;constintMAXLEN=100;enumerror_codesuccess,overflow,underflow;classstackpublic:stack();boolempty()const;error_codeget_top(DATA_TYPE&x)const;error_codepush(constDATA_TYPEx);error_codepop();boolfull()const;private:DATA_TYPEdataMAXLEN;intc
3、ount;stack:stack()count=0;boolstack:empty()constreturncount=0;error_codestack:get_top(DATA_TYPE&x)constif(empty()returnunderflow;elsex=datacount-1;returnsuccess;error_codestack:push(constDATA_TYPEx)if(full()returnoverflow;elsedatacount=x;count+;error_codestack:pop()if(empty()returnunderflow;elsecoun
4、t-;returnsuccess;boolstack:full()constreturncount=MAXLEN;voidmain()stackS;intN,d;cout请输入一个十进制数N和所需转换的进制dNd;if(N=0)cout输出转换结果:Nendl;while(N)S.push(N%d);N=N/d;cout输出转换结果:endl;while(!S.empty()S.get_top(N);coutN;S.pop();coutendl;while(!S.empty()S.get_top(x);coutx;S.pop();测试数据:N=1348d=8运行结果:2.给出顺序队列的类定义和
5、函数实现,并利用队列计算并打印杨辉三角的前n行的内容。(n=8)实验原理:杨辉三角的规律是每行的第一和最后一个数是1,从第三行开始的其余的数是上一行对应位置的左右两个数之和。因此,可用上一行的数来求出对应位置的下一行内容。为此,需要用队列来保存上一行的内容。每当由上一行的两个数求出下一行的一个数时,其中的前一个便需要删除,而新求出的数就要入队。程序清单:#include#includeusingnamespacestd;typedefintDATA_TYPE;constintMAXLEN=100;enumerror_codesuccess,underflow,overflow;classque
6、uepublic:queue();boolempty()const;error_codeget_front(DATA_TYPE&x)const;error_code append(const DATA_TYPE x);error_codeserve();boolfull()const;private:intfront,rear;DATA_TYPEdataMAXLEN;queue:queue()rear=0;front=0;boolqueue:empty()constreturn(front%MAXLEN=rear%MAXLEN);error_codequeue:get_front(DATA_T
7、YPE&x)constif(empty()returnunderflow;elsex=datafront%MAXLEN;returnsuccess;error_codequeue:append(constDATA_TYPEx)if(full()returnoverflow;elsedatarear%MAXLEN=x;rear+;error_codequeue:serve()if(empty()returnunderflow;elsefront+;returnsuccess;boolqueue:full()constreturn(rear+1)%MAXLEN=front);voidmain()q
8、ueueQ;intnum1,num2;inti=0;cout1endl;Q.append(1);num1=0;num2=1;for(i=0;i=7;i+)intj=0;intk=0;num1=0;for(j=0;j=i;j+)Q.get_front(num2);Q.serve();coutnum1+num2;Q.append(num1+num2);num1=num2;cout1endl;Q.append(1);运行结果:3.给出链栈的类定义和函数实现,并设计程序完成如下功能:读入一个有限大小的整数n,并读入n个数,然后按照与输入次序相反的次序输出各元素的值。实验原理:依次将栈中的元素出栈,因为
9、栈的一个特点就是先进后出,这样,当将原栈为空时,输出与输入次序相反,从而实现了本题的要求。程序清单:#include#includeusingnamespacestd;typedefintDATA_TYPE;typedefstructLNodeDATA_TYPEdata;LNode*next;LNode;enumerror_coderange_error,success,underflowclasslinkstackpublic:linkstack();linkstack();boolempty()const;error_codepush(constDATA_TYPEx);error_code
10、get_top(DATA_TYPE&x)const;error_codepop();private:LNode*top;intcount;DATA_TYPEdata;linkstack:linkstack()top=NULL;count=0;boollinkstack:empty()constreturn(count=0);error_codelinkstack:push(constDATA_TYPEx)LNode*s=newLNode;s-data=x;s-next=top;top=s;count+;returnsuccess;error_code&x)constlinkstack:get_
11、top(DATA_TYPEif(empty()returnunderflow;elsex=top-data;returnsuccess;error_codelinkstack:pop()if(empty()returnunderflow;elseLNode*u=newLNode;u=top;top=top-next;deleteu;count-;returnsuccess;linkstack:linkstack()while(!empty()pop();voidmain()linkstackL;intn;cout请任意输入一个整数n:n;for(inti=1;i=n;i+)L.push(i);
12、while(!L.empty()L.get_top(i);couti;L.pop();测试数据:n=9i=1运行结果:实验二单链表实验目的:理解线性表的链式存储结构。熟练掌握动态链表结构及有关算法的设计。根据具体问题的需要,设计出合理的表示数据的链表结构,并设计相关算法。实验任务:1.在一个递增有序的链表L中插入一个值为x的元素,并保持其递增有序特性。实 验 数 据 : 链 表 元 素 为(10,20,30,40,50,60,70,80,90,100),x分别为25,85,110和8。实验原理:给出了要插入的条件,但没有给定插入位置。因此,需要搜索满足这一条件的插入位置的前驱结点而不是序号。程
13、序清单:#includeusingnamespacestd;enumerror_codesuccess,overflow,underflow,rangeerror;typedefintelementtype;typedefstructLinkNodeelementtype data;structLinkNode *next;node;classlistprivate:int count;node *head;public:list();list();public:error_code get_element(const int i,elementtype&x)const;node*get_he
14、ad()constreturnhead;voidcreate();voiddisplay();voidinsert1(elementtypex);list:list()head=newnode;head-next=NULL;count=0;void list:create()elementtypex;node*s,*rear;cinx;rear=head;while(x!=-9999)count+;s=newnode;s-data=x;s-next=NULL;rear-next=s;rear=s;cinx;voidlist:display()node*p;p=head-next;while(p
15、!=NULL)coutdatanext;coutnext!=NULL&P-next-datanext;if(P-next=NULL|P-next-datax)u=newnode;u-data=x;u-next=P-next;P-next=u;count+;voidmain()listL;elementtypex;L.create();L.display();cout请输入要插入的值xx;L.insert(x);L.display();运行结果:X=25X=85X=110X=82.将单链表中的奇数项和偶数项结点分解开,并分别连成一个带头结点的单链表,然后再将这两个新链表同时输出在屏幕上,并保留原
16、链表的显示结果,以便对照求解结果。实验测试数据:第一组数据:链表为(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)实验原理:依据题目的要求,需要再创建两个新链表来存储分离后的奇偶项,而奇偶项可以根据数字来控制,把他们取出来并重新连起来。程序清单:#includeusingnamespacestd;enumerror_codesuccess,overflow,underflow,rangeerror;typedefintelementtype;typedefstructLinkNodeel
17、ementtype data;structLinkNode *next;node;classlistprivate:int count;node *head;public:list();list();public:node*get_head()constreturnhead;voidcreate();voiddisplay();list:list()head=newnode;head-next=NULL;count=0;void list:create()elementtypex;node*s,*rear;cinx;rear=head;while(x!=-9999)count+;s=newno
18、de;s-data=x;s-next=NULL;rear-next=s;rear=s;cinx;voidlist:display()node*p;p=head-next;while(p!=NULL)coutdatanext;coutnext;while(PA!=NULL)if(PA-data)%2!=0)s=newnode;s-data=PA-data;PB-next=s;PB=s;PB-next=NULL;PA=PA-next;elses=newnode;s-data=PA-data;PC-next=s;PC=s;PC-next=NULL;PA=PA-next;voidmain()listL
19、,L1,L2;L.create();divide(L,L1,L2);cout原链表:endl;L.display();cout偶链表:endl;L2.display();cout奇链表:next和B.head-next。在pa,pb均非空时,根据其值的大小关系可能有如下三种情况。(1).pa-data=pb-data:搜索到公共元素,应在C表表尾插入一个结点,其值为pa-data,然后继续A表中下一个元素的搜索,即pa=pa-next,同时pb也往后移。(2).pa-datapb-data:表明A表中这一元素可能在B表当前元素的后面,因此要往B表的后面搜索,故而执行pb=pb-next,然后继
20、续搜索。(3).pa-datadata:表明A中这一元素在B中不存在,因而执行pa=pa-next以继续对A表中下一个元素的判断。反复执行上述比较,直到pa,pb至少有一个为空为止。此时,剩余的非空部分没有所需要的公共元素,因而搜索结束。程序清单:#include#includeusingnamespacestd;typedefstructsnodeintdata;structsnode*next;node;enumerror_codearrange_error,success;classlistpublic:list();voidcreate2();intlength()const;erro
21、r_codeget_element(constinti,int&x)const;error_codeinsert(constint&x);error_codedelete_element(constinti);node*locate(constintx)const;node*get_head()returnhead;voidlist:gongyou(list&L1,list&L2)voidprint();private:intcount;node*head;list:list()head=newnode;head-next=NULL;count=0;voidlist:create2()intx
22、;node*p=head;node*s;coutx;while(x!=-1)s=newnode;s-data=x;s-next=NULL;p-next=s;p=s;coutx;intlist:length()constreturncount;error_codelist:get_element(constinti,int&x)constintj=1;node*p=head-next;while(p!=NULL&j!=i)p=p-next;j+;if(p=NULL)returnarrange_error;x=p-data;returnsuccess;voidlist:gongyou(list&L
23、1,list&L2)node *p1=L1.head-next; node*p2=L2.head-next;node*p3=head;node*u;while(p1!=NULL&p2!=NULL)if(p1-data=p2-data)u=newnode;u-data=p1-data;p3-next=u;p1=p1-next;p2=p2-next;p3=p3-next;elseif(p1-datadata)p1=p1-next;elsep2=p2-next;p3-next=NULL;node*list:locate(constintx)constnode*p=head-next;while(p!=NULL)if(p-data=x)returnp;p=p-next;returnNULL;voidlist:divide(list&B,list&C)node*u;node*pa=head-next;
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1