数据结构实验报告合工大Word下载.docx
《数据结构实验报告合工大Word下载.docx》由会员分享,可在线阅读,更多相关《数据结构实验报告合工大Word下载.docx(49页珍藏版)》请在冰豆网上搜索。
![数据结构实验报告合工大Word下载.docx](https://file1.bdocx.com/fileroot1/2023-1/7/21dc1c8b-06dc-43cc-aaeb-bead26dc3bf1/21dc1c8b-06dc-43cc-aaeb-bead26dc3bf11.gif)
intcount;
stack:
:
stack()
count=0;
}
boolstack:
empty()const
returncount==0;
error_codestack:
get_top(DATA_TYPE&
x)const
if(empty())
returnunderflow;
else
x=data[count-1];
returnsuccess;
push(constDATA_TYPEx)
if(full())
returnoverflow;
data[count]=x;
count++;
pop()
count--;
full()const
returncount==MAXLEN;
voidmain()
stackS;
intN,d;
cout<
<
"
请输入一个十进制数N和所需转换的进制d"
endl;
cin>
>
N>
d;
if(N==0)
输出转换结果:
N<
while(N)
(N%d);
N=N/d;
while(!
())
(N);
N;
();
(x);
x;
测试数据:
N=1348d=8
运行结果:
2.给出顺序队列的类定义和函数实现,并利用队列计算并打印杨辉三角的前n行的内容。
(n=8)
实验原理:
杨辉三角的规律是每行的第一和最后一个数是1,从第三行开始的其余的数是上一行对应位置的左右两个数之和。
因此,可用上一行的数来求出对应位置的下一行内容。
为此,需要用队列来保存上一行的内容。
每当由上一行的两个数求出下一行的一个数时,其中的前一个便需要删除,而新求出的数就要入队。
程序清单:
success,underflow,overflow
classqueue
queue();
error_codeget_front(DATA_TYPE&
error_codeappend(constDATA_TYPEx);
error_codeserve();
intfront,rear;
queue:
queue()
rear=0;
front=0;
boolqueue:
return(front%MAXLEN==rear%MAXLEN);
error_codequeue:
get_front(DATA_TYPE&
x=data[front%MAXLEN];
append(constDATA_TYPEx)
data[rear%MAXLEN]=x;
rear++;
serve()
front++;
return((rear+1)%MAXLEN==front);
queueQ;
intnum1,num2;
inti=0;
1<
(1);
num1=0;
num2=1;
for(i=0;
i<
=7;
i++)
intj=0;
intk=0;
for(j=0;
j<
=i;
j++)
(num2);
num1+num2<
"
;
(num1+num2);
num1=num2;
3.给出链栈的类定义和函数实现,并设计程序完成如下功能:
读入一个有限大小的整数n,并读入n个数,然后按照与输入次序相反的次序输出各元素的值。
依次将栈中的元素出栈,因为栈的一个特点就是先进后出,。
这样,当将原栈为空时,输出与输入次序相反,从而实现了本题的要求。
typedefstructLNode
DATA_TYPEdata;
LNode*next;
}LNode;
range_error,success,underflow
classlinkstack
linkstack();
~linkstack();
LNode*top;
DATA_TYPEdata;
linkstack:
linkstack()
top=NULL;
boollinkstack:
return(count==0);
error_codelinkstack:
LNode*s=newLNode;
s->
data=x;
next=top;
top=s;
x=top->
data;
LNode*u=newLNode;
u=top;
top=top->
next;
deleteu;
~linkstack()
empty())
pop();
linkstackL;
intn;
请任意输入一个整数n:
n;
for(inti=1;
=n;
(i);
n=9i=1
实验二:
单链表
理解线性表的链式存储结构。
熟练掌握动态链表结构及有关算法的设计。
根据具体问题的需要,设计出合理的表示数据的链表结构,并设计相关算法。
实验任务:
在一个递增有序的链表L中插入一个值为x的元素,并保持其递增有序特性。
1.实验数据:
链表元素为(10,20,30,40,50,60,70,80,90,100),x分别为25,85,110和8。
给出了要插入的条件,但没有给定插入位置。
因此,需要搜索满足这一条件的插入位置的前驱结点而不是序号。
程序清单:
typedefstructsnode
intdata;
structsnode*next;
}node;
enumerror_code{arrange_error,success};
classlist
list();
voidcreate2();
intlength()const;
error_codeget_element(constinti,int&
x)const;
error_codeinsert(constint&
x);
error_codedelete_element(constinti);
node*locate(constintx)const;
node*get_head(){returnhead;
voidprint();
node*head;
list:
list()
head=newnode;
head->
next=NULL;
count=0;
voidlist:
create2()
intx;
node*p=head;
node*s;
cout<
输入一个值:
cin>
while(x!
=-1)
{
s=newnode;
s->
s->
p->
next=s;
p=s;
cout<
cin>
}
intlist:
length()const
returncount;
error_codelist:
get_element(constinti,int&
x)const
intj=1;
node*p=head->
while(p!
=NULL&
&
j!
=i)
p=p->
j++;
if(p==NULL)
returnarrange_error;
x=p->
returnsuccess;
node*list:
locate(constintx)const
node*p=head->
=NULL)
if(p->
data==x)
returnp;
returnNULL;
insert(constint&
x)
node*q=head;
node*p=head->
p->
data<
q=p;
p=p->
s->
q->
count++;
else{
next=q->
q->
delete_element(constinti)
node*u;
node*p=head;
intj=0;
while(j!
=i-1&
p!
if(i<
1||i>
count)
u=p->
p->
next=u->
deleteu;
count--;
print()
cout<
listl;
创建一个链表(输入‘-1’结束):
();
输入要插入的数(输入‘-1’结束):
输入一个数:
(x);
测试数据:
2.将单链表L中的奇数项和偶数项结点分解开,并分别连成一个带头结点的单链表,然后再将这两个新链表同时输出在屏幕上,并保留原链表的显示结果,以便对照求解结果。
依据题目的要求,需要再创建两个新链表来存储分离后的奇偶项,而奇偶项可以根据数字来控制,把他们取出来并重新连起来。
divide(list&
B,list&
C);
divide(list&
C)
node*pa=head->
node*pb=();
node*pc=();
for(inti=0;
pa!
=NULL;
i++,pa=pa->
next)
u=newnode;
u->
data=pa->
if(i%2==0)
{
pb->
next=u;
pb=pb->
}
else
pc->
pc=pc->
pb->
listA,B,C;
intx,y,z;
(B,C);
原表:
();
奇数表:
();
偶数表:
第一组数据:
链表元素为(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)
3.求两个递增有序链表L1和L2中的公共元素,并以同样方式连接成链表L3。
设置两个指针怕,pa,pb分别依次指示A,B表中的元素,其初始值分别为>
next和>
next。
在pa,pb均非空时,根据其值的大小关系可能有如下三种情况。
(1).pa->
data==pb->
data:
搜索到公共元素,应在C表表尾插入一个结点,其值为pa->
data,然后继续A表中下一个元素的搜索,即pa=pa->
next,同时pb也往后移。
(2).pa->
data>
pb->
表明A表中这一元素可能在B表当前元素的后面,因此要往B表的后面搜索,故而执行pb=pb->
next,然后继续搜索。
(3).pa->
表明A中这一元素在B中不存在,因而执行pa=pa->
next以继续对A表中下一个元素的判断。
反复执行上述比较,直到pa,pb至少有一个为空为止。
此时,剩余的非空部分没有所需要的公共元素,因而搜索结束。
int