数据结构实验报告Word格式文档下载.docx
《数据结构实验报告Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《数据结构实验报告Word格式文档下载.docx(23页珍藏版)》请在冰豆网上搜索。
![数据结构实验报告Word格式文档下载.docx](https://file1.bdocx.com/fileroot1/2023-1/21/2519b832-0a0a-415b-8b0b-0daf4ee1203f/2519b832-0a0a-415b-8b0b-0daf4ee1203f1.gif)
tail->
//这一行很关键,不然会出错,在输出多项式的时候会出错
tail)cout<
多项式创建失败!
factor=x;
index=y;
next=tail;
head=head->
next;
cin>
}while(x!
=111);
return0;
}
voidShow_Polynomial(Node*head)//显示多项式
{
intx=0;
Node*p=head->
while(p)
{if(x!
=0)
cout<
+"
if(p->
factor<
0)//当系数小于0时加括号
"
("
p->
)X^"
p->
index;
elseif(p->
factor==0)
{cout<
0"
}
factor==1)
X^"
index==0)
factor;
index==1)
factor<
X"
else
index;
p=p->
x++;
cout<
endl;
voidAdd(Listlist1,Listlist2)
{intx;
Node*p=list1.head->
next,*q=list2.head->
next;
Node*pre_p=list1.head,*temp;
while(p!
=NULL&
&
q!
=NULL)
{if(p->
index<
q->
index)
{pre_p=p;
p=p->
elseif(p->
index==q->
{x=p->
factor+q->
factor;
if(x!
=0)
{p->
pre_p=p;
}
else
{pre_p->
next=p->
deletep;
}
p=pre_p->
//p=p->
temp=q;
q=q->
deletetemp;
}
else//if(p->
index>
{temp=q->
q->
next=p;
pre_p->
next=q;
pre_p=q;
q=temp;
if(q)///如果第二个链表比第一个长则直接在的哥链表后面街上剩下的链表
{pre_p->
Poy.cpp内容如下:
#include<
#include"
usingnamespacestd;
intmain()
{Listlist1,list2;
system("
color97"
);
\n请输入第一个多项式...(输入111结束多项式设定)\n\n"
list1.Set_Polynomial(list1.head,list1.tail);
\n请输入第二个多项式...(输入111结束多项式设定)\n\n"
list2.Set_Polynomial(list2.head,list2.tail);
cls"
list1="
list1.Show_Polynomial(list1.head);
\nlist2="
list2.Show_Polynomial(list2.head);
\n\nlist1+list2="
list1.Add(list1,list2);
endl<
pause"
return0;
实验二
1.队列
#defineMAX10
structNode;
typedefstructNode*PNode;
{intdata;
PNodenext;
structLinkQueue
{PNodef;
PNoder;
typedefstructLinkQueue*PLinkQueue;
PLinkQueueCreatEmptyQueue_link()
{PLinkQueueplqu;
plqu=newLinkQueue;
if(plqu!
=NULL)
{plqu->
f=NULL;
plqu->
r=NULL;
elsecout<
Outofspace!
!
\n"
returnplqu;
intisEmptyQueue_link(PLinkQueueplqu)
{return(plqu->
f==NULL);
voidenQueue_link(PLinkQueueplqu,intx)
{PNodep;
p=newNode;
if(p==NULL)
cout<
else{
p->
data=x;
if(plqu->
f==NULL)
{plqu->
f=p;
}
else
r->
plqu->
r=p;
voiddeQueue_link(PLinkQueueplqu)
if(plqu->
Emptyqueue.\n"
else
{p=plqu->
f;
f=p->
deletep;
voidfrontQueue_link(PLinkQueueplqu)
{if(plqu->
f==NULL)
plqu->
f->
data<
Queue.cpp内容如下:
Queue.h"
{system("
color49"
PLinkQueuelink;
link=CreatEmptyQueue_link();
intx;
intcount=0;
pleaseinputdatatolinkendwith-1\n\n"
while(cin>
x)
{if(x==-1)
break;
elseif(count>
=MAX)
{cout<
}
{enQueue_link(link,x);
count++;
for(inti=0;
i<
count;
i++)
{frontQueue_link(link);
deQueue_link(link);
2.系统栈
#include<
stack>
intmain()
system("
intn;
doubleitem;
stack<
double>
numbers;
//declaresandinitializesastackofnumbers
mystack;
cout<
Typeinanintegernfollowedbyndecimalnumbers."
endl<
Thenumberswillbeprintedinreverseorder."
endl;
cin>
n;
for(inti=0;
i<
i++){
item;
numbers.push(item);
\n\nnumber="
while(!
numbers.empty()){
numbers.top()<
mystack.push(numbers.top());
numbers.pop();
\n\nmystack="
mystack.empty()){
mystack.top()<
mystack.pop();
cout<
return0;
实验三、四迷宫
Stack.h内容如下:
structData
inty;
intd;
structStack
{intMAXNUM;
intt;
Data*s;
typedefstructStack*PStack;
PStackCreateNULLStack(intm,intn)
{PStackstack=newStack;
if(stack!
{stack->
s=newData[m*n];
if(stack->
s!
{stack->
MAXNUM=m*n;
stack->
t=-1;
returnstack;
elsedeletestack;
存储分配失败!
returnNULL;
intisEmptyStack(PStackstack)
{return(stack->
t==-1);
voidPushStack(PStackstack,Datadata)
{if(stack->
t>
=stack->
MAXNUM-1)
上溢...\n\n"
t=stack->
t+1;
stack->
s[stack->
t].x=data.x;
t].y=data.y;
t].d=data.d;
voidPopStack(PStackstack)
t==-1)
空栈...\n\n"
elsestack->
t-1;
DataGetTopOfStack(PStackstack)
t]);
Stack.cpp内容如下:
Stack.h"
intm=9,n=19;
intMaze[11][21];
intRout[11][21];
voidSetMaze(intMaze[11][21],intm,intn)
{inti,j;
for(i=0;
m+2;
i++){
for(j=0;
j<
n+2;
j++){
if(i==0||i==m+1){
Maze[i][j]=1;
Rout[i][j]=1;
elseif(j==0||j==n+1){
elseif((i!
=1&
j!
=1)&
(i!
=9&
=19)&
(i)%2==0&
(j)%2==0)
{Maze[i][j]=1;
else
{Maze[i][j]=0;
Rout[i][j]=0;
voidShowMaze(intMaze[11][21],intm,intn)
for(i=0;
i<
i++){
for(j=0;
j<
j++){
if(Maze[i][j]==1)cout<
#"
elseif(Maze[i][j]==2)cout<
2"
elsecout<
}cout<
intMove[8][2]={{0,-1},{-1,-1},{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1}};
PStackstack,sta;
Datadata;
inti,j,k;
intg,h;
SetMaze(Maze,m,n);
================================\n"
============原始迷宫============\n"
ShowMaze(Maze,m,n);
=================================\n"
stack=CreateNULLStack(m+2,n+2);
sta=CreateNULLStack(m+2,n+2);
intX1,Y1,X2,Y2;
X1=1;
Y1=1;
//入口
X2=9;
Y2=19;
Maze[X1][Y1]=2;
data.x=X1;
data.y=Y1;
data.d=-1;
PushStack(stack,data);
while(!
isEmptyStack(stack))
{
data=GetTopOfStack(stack);
PopStack(stack);
i=data.x;
j=data.y;
k=data.d+1;
while(k<
8)
{g=i+Move[k][0];
h=j+Move[k][1];
if(g==X2&
h==Y2&
Maze[g][h]==0)
{while(!
{data=GetTopOfStack(stack);
Rout[data.x][data.y]=2;
PushStack(sta,data);
}cout<
迷宫路径是:
while(!
isEmptyStack(sta))
{data=GetTopOfStack(sta);
PopStack(sta);
第"
count++<
步\t"
data.x<
data.y<
data.d<
\n\n================================\n"
==========带路径的迷宫==========\n\n\n"
ShowMaze(Rout,m,n);
return0;
if(Maze[g][h]==0)
{Maze[g][h]=2;
data.x=i;
data.y=j;
data.d=k;
PushStack(stack,data);
i=g;
j=h;
k=-1;
k=k+1;
}
实验五归并排序
Merge.h内容如下:
time.h>
#definecount15
intGet_Data(intArray[count])
for(inti=0;
count;
i++)
Array[i]=rand()%50;
voidShow_Data(intArray[])
数组中的数据为:
for(inti=0;
Array[i]<
voidmerge(int*Node,int*Node1,intlow,intm,inthigh)
i=low;
j=m+1;
k=low;
while((i<
=m)&
(j<
=high))
{
if(Node[i]<
=Node[j])
Node1[k++]=Node[i++];
Node1[k++]=Node[j++];
while(i<
=m)
Node1[k++]=Node[i++];
while(j<
=high)
Node1[k++]=Node[j++];
voidmergePass(int*Node,int*Node1,intn,intlength)
intj,i=0;
while(i+2*length-1<
n)
merge(Node,Node1,i,i+length-1,i+2*length-1);
i+=2*length;
if(i+length-1<
n-1)
merge(Node,Node1,i,i+length-1,n-1);
for(j=i;
j<
n;
j++)
Node1[j]=Node[j];
voidmerfeSort(intData[])
intRecord[count];
intlength=1;
while(length<
count)
mergePass(Data,Record,count,length);
length*=2;
mergePass(Record,Data,count,length);
Guibing.cpp内容如下:
merge.h"
intNode[count];
intNode1[count];
Get_Data(Node);
Show_Data(Node);
merfeSort(Node);
实验六图
Graph.txt内容如下:
0101001001921
1005610011
100506100100
1006601814
1910010018033
211110014330
Vex.txt内容如下:
ABCDEF
Graph.h
/*********************************************************
**********************************************************
图的信息存放在当前目录的Graph.txt文件中
本程序采用教材图9.13,共六个节点
附:
无直接边相连文件中用10