《数据结构》上机实验报告 链表.docx
《《数据结构》上机实验报告 链表.docx》由会员分享,可在线阅读,更多相关《《数据结构》上机实验报告 链表.docx(18页珍藏版)》请在冰豆网上搜索。
《数据结构》上机实验报告链表
西华数学与计算机学院上机实践报告
课程名称:
数据结构
年级:
2011
上机实践成绩:
指导教师:
唐剑梅
姓名:
蒋俊
上机实践名称:
学号:
312011*********
上机实践日期:
2012-11-6
上机实践编号:
1
上机实践时间:
8:
00-9:
30
一、实验目的
1.了解线性表的逻辑结构特性,以及这种特性在计算机内的两种存储结构。
2.重点是线性表的基本操作在两种存储结构上的实现;其中以链表的操作为侧重点;并进一步学习程序设计方法。
3.掌握栈这种数据结构特性及其主要存储结构,并能在现实生活中灵活运用。
4.掌握队列这种数据结构特性及其主要存储结构,并能在现实生活中灵活运用。
5.了解和掌握递归程序设计的基本原理和方法。
6.掌握使用C++面向对象的程序设计技术设计数据结构源程序的方法。
二、实验内容
1.熟悉前面的【程序示例2】,按照约瑟夫问题的方法2,试着不设头结点改写原来的程序,上机调试运行。
2.用链表建立通讯录。
通讯录内容有:
姓名、通讯地址、电话号码。
要求:
(1)通讯录按姓名项的字母顺序排列;
(2)能查找通讯录中某人的信息;
[提示]用链表来存放这个通讯录,一个人的信息作为一个结点。
成链的过程可以这样考虑:
先把头结点后面的第一个数据元素结点作为链中的首结点,也是末结点。
从第二个数据开始逐一作为“工作结点”,需从链表的首结点开始比较,如果“工作结点”的数据元素的姓名字符串比链中的“当前结点”的数据元素的姓名字符串小,就插在其前面。
否则,再看后面是否还有结点,若没有结点了就插在其后面成为末结点;若后面还有结点,再与后面的结点逐一比较处理。
3.写一个程序,将输入的十进制数据M转换为八进制数据M8,将其调试通过。
在此基础上修改程序,实现十进制数据M向N进制(N为2或8或16)的转换。
(注:
辅助栈的实现可使用【程序示例3】)
三、实验环境
硬件:
微型计算机P4
软件:
WindowsXP+MicrosoftVisualC++6.0
四、程序源码及调试过程
第一题
1)代码
#include
structNodeType//结点的结构定义
{intnum;//编号子域
intpsw;//密码域
charname[20];//姓名子域
NodeType*next;//指针域
};
classJose//类声明
{private:
NodeType*Head;
public:
Jose(){};
~Jose(){};
voidcreat();
voidouts();
};
voidJose:
:
creat()
{inti=0,n;
NodeType*newp,*pre;
cout<<"\n输入总人数n=";cin>>n;
pre=newNodeType;
Head=newNodeType;
pre->num=1;
cout<<"\n编号"<<1<<"的人姓名=";
cin>>pre->name;
cout<<"\n密码"<<1<<"的人密码=";
cin>>pre->psw;
Head=pre;
Head->next=Head;
for(i=1;i{newp=newNodeType;
newp->num=i+1;
cout<<"\n编号"<>newp->name;
cout<<"\n密码"<
cin>>newp->psw;
newp->next=Head;
pre->next=newp;
pre=newp;
}
}
voidJose:
:
outs()
{intm,i;NodeType*q=Head,*p;
cout<<"\n输入m值(m>=2)";cin>>m;
cout<<"\n根据m值,开始报数输出:
"<while(q->next!
=q)
{for(i=1;inext;}
cout<<"编号为:
"<num<<"的人的姓名:
"<name<cout<<"\n编号为:
"<num<<"的人的密码:
"<psw<m=q->psw;
p->next=q->next;deleteq;
q=p->next;
}
cout<<"编号为:
"<num<<"的人的姓名:
"<name<cout<<"\n编号为:
"<num<<"的人的密码:
"<psw<deleteq;
}
intmain()
{Joseh;
h.creat();h.outs();
return0;
}
第二题
2)
#include
#include
structElemType//数据元素的类型
{charAdd[20];
charname[20];
chartel[20];
};
structNodeType
{
ElemTypedata;
NodeType*next;
};
classSqlist
{private:
NodeType*Head;
public:
Sqlist();
~Sqlist();
voidcreat();
voidInsert(ElemTypex);
voidDelet(ElemTypex);
voidPrintOut();
};
Sqlist:
:
Sqlist()
{
Head=newNodeType;
Head->next=NULL;
strcpy(Head->data.name,"姓名");
strcpy(Head->data.Add,"地址");
strcpy(Head->data.tel,"电话号码");
}
Sqlist:
:
~Sqlist()
{
NodeType*p=Head->next;
while(p!
=NULL)
{Head->next=p->next;
deletep;
p=Head->next;
}
}
voidSqlist:
:
creat()//初步建立一个通讯录
{NodeType*p,*s,*q;
ElemTypex;
inta;
q=Head;
cout<<"\n输入姓名:
";cin>>x.name;
cout<<"\n输入通讯地址:
";cin>>x.Add;
cout<<"\n输入电话号码:
";
cin>>x.tel;
p=newNodeType;
p->data=x;
Head->next=p;
p->next=NULL;
cout<<"输入一个数。
若为-1,结束输入:
"<cin>>a;
while(a!
=-1)
{
cout<<"\n输入姓名:
";cin>>x.name;
cout<<"\n输入通讯地址:
";cin>>x.Add;
cout<<"\n输入电话号码:
=";
cin>>x.tel;
s=newNodeType;
s->data=x;
if(strcmp(s->data.name,p->data.name)>0)
{p->next=s;
s->next=NULL;
p=s;
}
else{
s->next=p;
q->next=s;
}
q=q->next;
cout<<"输入一个数。
若为-1,结束输入:
"<cin>>a;
}
}
voidSqlist:
:
Insert(ElemTypex)//插入
{NodeType*p,*q,*s;
s=newNodeType;
s->data=x;
q=Head;
p=q->next;
while(p!
=NULL&&strcmp(p->data.name,x.name)<0)
{q=p;p=p->next;}
s->next=p;
q->next=s;
}
voidSqlist:
:
Delet(ElemTypex)//删除
{
NodeType*p,*q;
q=Head;
p=Head->next;
while(p!
=NULL&&strcmp(p->data.name,x.name)!
=0)
{q=p;p=p->next;}
if(p!
=NULL){
q->next=p->next;
deletep;
cout<<"删除结点成功"<}
else
cout<<"未找到要删除的信息"<}
voidSqlist:
:
PrintOut()//输出
{NodeType*p;
p=Head->next;
while(p!
=NULL){
cout<data.name<<"";
cout<data.tel<<"";
cout<data.Add<<"";
p=p->next;
}
cout<}
intmain()
{intk;ElemTypee;
Sqlistas;
cout<<"\n通讯录演示";
do{
cout<<"\n\n";
cout<<"\n\n1.初步建立一个通讯录(单链表)";
cout<<"\n\n2.插入新的电话记录";
cout<<"\n\n3.删除一个电话记录";
cout<<"\n\n4.结束程序";
cout<<"\n********************************";
cout<<"\n请输入你的选择(1,2,3,4)";cin>>k;
switch(k)
{case1:
{as.creat();as.PrintOut();}break;
case2:
{
cout<<"\n插入的数据姓名";cin>>e.name;
cout<<"\n插入的数据电话号";cin>>e.tel;
cout<<"\n插入的数据地址";cin>>e.Add;
as.Insert(e);as.PrintOut();
}break;
case3:
{
cout<<"\n被删除的姓名=";
cin>>e.name;
as.Delet(e);
as.PrintOut();
}break;
default:
break;
}
}while(k>=1&&k<4);
cout<<"\n再见!
";
return0;
}
第三题
2)代码
#include
#include
typedefintElemType;//数据元素的类型
constintMAXSIZE=100;//数组的容量
classSqStack
{private:
ElemTypeelem[MAXSIZE];
inttop;
public:
SqStack();
~SqStack(){};
voidSqStack:
:
push(ElemTypee);
ElemTypeSqStack:
:
pop();
voidSqStack:
:
PrintOut();
intSqStack:
:
IsEmpty();
voidf(ElemTypeN,ElemTypeM);
};
voidSqStack:
:
f(ElemTypeN,ElemTypeM)
{
SqStacks;
ElemTypee;
while(N)
{
s.push(N%M);
N=N/M;
}
while(!
s.IsEmpty())
{
e=s.pop();
if(e>=10)
{
e=e%10;
switch(e)
{
case1:
cout<<"b"<case2:
cout<<"c"<case3:
cout<<"d"<case4:
cout<<"e"<case5:
cout<<"f"<default:
cout<<"a"<}
}
else
cout<}
cout<}
SqStack:
:
SqStack(){top=0;}
voidSqStack:
:
push(ElemTypee)
{if(top==MAXSIZE-1)
{cout<<"栈满溢出"<return;
}
else{top++;
elem[top]=e;}
}
ElemTypeSqStack:
:
pop()
{ElemTypex;
if(top==0)
{cout<<"栈为空,不能出栈操作"<else{x=elem[top];
top--;
returnx;}
}
voidSqStack:
:
PrintOut()
{intk;
cout<<"\nPrintOutData:
\n";
for(k=top;k>=1;k--)cout<cout<}
intSqStack:
:
IsEmpty()
{if(top==0)return1;
elsereturn0;
}
voidmain()
{
ElemTypea,m;
cout<<"请输入一个正整数:
"<cin>>a;
cout<<"请输入要转换的进制:
"<cin>>m;
SqStackas;
as.f(a,m);
}
五、总结
通过本次实验,我熟悉了链表的操作,了解了线性表在现实生活中的运用,认识了顺序存储和链式存储这两种结构。
本次上机实践基本完成了实验内容,但完成的不是很好,以后需要更加努力地掌握基本的知识。
实验内容对于队列的运用没有涉及,希望以后有所涉及。