数据结构上机实验报告Word文档下载推荐.docx
《数据结构上机实验报告Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《数据结构上机实验报告Word文档下载推荐.docx(33页珍藏版)》请在冰豆网上搜索。
建立单链表.h"
输出单链表.h"
单链表逆置.h"
voidmain()
{
linklist*head;
creat(head);
print(head);
invert(head);
//调用单链表逆置的函数
}
//单链表结构类型定义.h
typedefchardatatype;
typedefstructnode
datatypedata;
structnode*next;
}linklist;
//建立单链表.h
voidcreat(linklist*&
head)
//采用尾插法建立具有结点的单链表
charch;
linklist*s,*r;
head=newlinklist;
r=head;
while((ch=getchar())!
='
*'
)
{
s=newlinklist;
s->
data=ch;
r->
next=s;
r=s;
}
r->
next=NULL;
//输出单链表.h
voidprint(linklist*head)
linklist*p=head->
next;
while(p!
=NULL)
cout<
<
p->
data<
"
"
;
p=p->
cout<
endl;
//单链表逆置.h
voidinvert(linklist*head)
linklist*p,*q,*r;
p=head->
q=p->
while(q!
r=q->
q->
next=p;
p=q;
q=r;
head->
next->
单链表结果截图见下方实验结果。
顺序表代码:
//顺序表逆置主文件.cpp
顺序表结构类型定义.h"
建立顺序表.h"
输出顺序表.h"
顺序表逆置.h"
sequenlist*L;
creat(L);
print(L);
invert(L);
//调用顺序表逆值的函数
//顺序表的结构类型定义.h
constintmaxsize=1024;
typedefstruct
{datatypedata[maxsize];
intlast;
}sequenlist;
//建立顺序表.h
voidcreat(sequenlist*&
L)
L=newsequenlist;
L->
last=0;
L->
data[L->
last]=ch;
last++;
//输出顺序表.h
voidprint(sequenlist*L)
for(inti=0;
i<
L->
last;
i++)
data[i]<
//顺序表逆置.h
voidinvert(sequenlist*L)
{charmid;
inti,j;
i=0;
j=L->
last-1;
while(i<
j)
mid=L->
data[i];
data[i]=L->
data[j];
data[j]=mid;
i++;
j--;
顺序表实验截图见下方实验结果。
2.已知由不具有头结点的单链表表示的线性表中,含有三类字符的数据元素(字母、数字和其他字符),试编写算法构造三个以循环链表表示的线性表,使每个表中只含有同一类的字符,且利用原表中的结点空间,头结点可另辟空间。
习题2)
//分解单链表主程序文件.cpp
输出循环链表.h"
在循环链表中插入.h"
分解单链表.h"
{linklist*head,*letter,*digit,*other;
print1(head);
letter=newlinklist;
letter->
next=letter;
digit=newlinklist;
digit->
next=digit;
other=newlinklist;
other->
next=other;
resolve(head,letter,digit,other);
//调用分解单链表的函数
print2(letter);
print2(digit);
print2(other);
//单链表结构类型定义
{datatypedata;
//建立单链表
{datatypex;
cin>
>
x;
while(x!
$'
{
s=newlinklist;
s->
data=x;
r=s;
voidprint1(linklist*head)
//输出单链表
{linklist*p=head->
{cout<
data;
p=p->
voidprint2(linklist*head)
//输出循环链表
=head)
//在循环链表中插入.h
voidinsert(linklist*h,linklist*p)
{linklist*q=h;
while(q->
next!
=h)q=q->
q->
p->
next=h;
//分解单链表.h
voidresolve(linklist*head,linklist*letter,linklist*digit,linklist*other)
linklist*p=head->
next,*t;
next=NULL;
while(p!
t=p;
if(t->
data>
0'
&
&
t->
data<
9'
insert(digit,t);
elseif((t->
a'
z'
)||
(t->
A'
Z'
))
insert(letter,t);
elseinsert(other,t);
return;
截图见下方实验结果。
3、实验结果
四、个人思路
顺序表做逆置操作时将对应的首尾元素位置交换,单链表的指针end指向链表的末尾,指针start指向链表头结点,指针s用来找到指向end节点的节点,将指向链表末尾和头结点的存储内容交换,然后头结点指针指向下一节点,s指针从start节点开始遍历寻找指向end指针的节点,并将end指针赋值为s指针,就完成了单链表的逆置,可以看出单链表和顺序表都可以完成线性表的逆置。
分解单链表的实现思路是首先新建3个循环链表,然后顺序遍历单链表,ASCII码判断链表中的元素属于哪一类元素,然后将这个元素添加到对应的循环链表中,从而实现分解单链表的功能。
实验二栈和队列
1.熟悉栈和队列的顺序和链式存储结构
2.掌握栈和队列的基本运算
3.能够利用栈和队列的基本运算完成栈和队列应用的运算
1.假设以数组sequ[m]存放循环队列的元素,同时设变量rear和quelen分别指示循环队列中队尾元素的位置和内含元素的个数。
编写实现该循环队列的入队和出队操作的算法。
提示:
队空的条件:
sq->
quelen==0;
队满的条件:
quelen==m。
习题3)
//循环队列入队出队的主程序文件.cpp
stdlib.h>
循环队列的结构类型定义.h"
置空队.h"
入队.h"
出队.h"
{qu*sq;
datatypex,*p;
intkey;
sq=newqu;
setnull(sq);
do
{cout<
1.EnterQueue2.DeleteQueue-1.Quit:
cin>
key;
switch(key)
{case1:
EntertheData:
enqueue(sq,x);
break;
case2:
p=dequeue(sq);
if(p!
=NULL)cout<
*p<
break;
case-1:
exit(0);
}while
(1);
//出队.h
datatype*dequeue(qu*sq)
{
datatype*temp;
if(sq->
quelen==0)