




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、实验报告三 栈和队列一、 实验目的:(1) 掌握栈的基本操作的实现方法。(2) 利用栈先进后出的特点,解决一些实际问题。(3) 掌握链式队列及循环队列的基本操作算法。(4) 应用队列先进先出的特点,解决一些实际问题。二、 实验内容:1、 使用一个栈,将一个十进制转换成二进制。粘贴源程序:package Q1;public class SeqStack public int element;public int top;public static SeqStack p;public SeqStack(int size)this.element=new intsize;this.top=-1;pu
2、blic void push(int x)this.top+;this.elementthis.top=x;public int pop()return this.top=-1 ? -1: (int)this.elementthis.top-;public int get()return this.top=-1 ? -1: (int)this.elementthis.top;public static void disp(SeqStack p)int t = -2;while(t!=-1)t=p.pop();if(t!=-1)System.out.printf("%d",t
3、);public static void fun(int x)int t;while(x!=1)t=x%2;x=x/2;p.push(t);if(x=1)p.push(x);public static void main(String args)p=new SeqStack(13);fun(99);disp(p);粘贴测试数据及运行结果:2、回文是指正读反读均相同的字符序列,如“acdca”、“dceecd”均是回文,但“book”不是回文。利用1中的基本算法,试写一个算法判定给定的字符串是否为回文。(提示:将一半字符入栈,依次弹出与另一半逐个比较)粘贴源程序:package Q2;publi
4、c class SeqStack public int element;public int top;public static SeqStack p;public SeqStack(int size)this.element=new intsize;this.top=-1;public void push(int x)this.top+;this.elementthis.top=x;public int pop()return this.top=-1 ? -1: (int)this.elementthis.top-;public int get()return this.top=-1 ? -
5、1: (int)this.elementthis.top;public static void input(String str)int i=0;int t=str.length();if(t%2=0)for(i=0;i<t/2;i+)p.push(str.charAt(i);elsefor(i=0;i<=t/2;i+)p.push(str.charAt(i);public static boolean compare(String str,SeqStack p)boolean flag = true;char t;int length=str.length();if(length
6、%2=0)for(int i=0;i<length/2;i+)t=str.charAt(length/2+i);if(t!=p.pop()flag=false;break;elsefor(int i=0;i<length/2+1;i+)t=str.charAt(length/2+i);if(t!=p.pop()flag=false;break;return flag;public static void main(String args) boolean flag;p=new SeqStack(100);String str =new String("acbca"
7、;);p.input(str);flag=pare(str, p);if(flag=true)System.out.println("yes");else System.out.println("No");粘贴测试数据及运行结果:3、 使用3个队列分别保留手机上最近10个“未接来电”、“已接来电”、“已拨电话”。粘贴源程序:package Q3;public class SeqQueue<T> public Object element;public int front,rear;public SeqQueue p;public SeqQue
8、ue(int length)this.element=new Objectlength;this.front =this.rear =0;public boolean isEmpty()return this.front =this.rear ;public boolean isFull()boolean flag=false;if(this.front!=this.rear && (this.rear+1)%this.element.length = this.front)flag=true;return flag;public void enquere(T x)this.e
9、lementthis.rear=x;this.rear=(this.rear+1)%this.element.length;public T dequeue()if(isEmpty()return null;T temp=(T)this.elementthis.front;this.front =(this.front+1)%this.element.length;return temp;public static void disp(SeqQueue p)int i=1;while(p.isEmpty()!=true)System.out.println(i+"."+p.
10、dequeue();i+;System.out.println();public static void fun()long num=0;int t=0;SeqQueue missedCall = new SeqQueue(11);SeqQueue receivedCall = new SeqQueue(11);SeqQueue outgoingCall = new SeqQueue(11);while(!(missedCall.isFull() && receivedCall.isFull() && outgoingCall.isFull()num=(int)
11、(Math.random()*100000000);t=(int)(Math.random()*101);if(t%3=0)missedCall.enquere(num);if(t%3=1)receivedCall.enquere(num);if(t%3=2)outgoingCall.enquere(num);System.out.println("missedCall");disp(missedCall);System.out.println("receivedCall");disp(receivedCall);System.out.println("outgoingCall");disp(outgoingCall);public static void main(String args) fun();
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 门业工程合同协议书模板
- 阳光房搭建合同协议范本
- 煅烧车间承包合同协议书
- 生物信息咨询费合同范本
- 消防施工合同终止协议书
- 江苏商标转让协议书模板
- 防盗玻璃承包协议书范本
- 自媒体账号归属合同范本
- 湛江复印机租赁合同范本
- 自建危房拆除赔偿协议书
- 用柴油管理制度
- 信用卡消费者保护培训
- 青蓝工程工作总结师傅
- 石油化工生产与操作作业指导书
- 卫生间卫浴知识培训课件
- GB/T 45251-2025互联网金融个人网络消费信贷贷后催收风控指引
- (高清版)DB11∕T2256-2024城市轨道交通钢轨踏面维修技术规范
- 2025-2030年咖啡馆咖啡制作机器人行业跨境出海战略研究报告
- 产业园项目弱电智能化规划方案
- 2019-2025年中国菜籽油市场发展现状调研及投资趋势前景分析报告
- 中华人民共和国内河交通安全管理条例
评论
0/150
提交评论