实验三栈和队列

上传人:mg****2 文档编号:124435408 上传时间:2020-03-12 格式:DOC 页数:6 大小:529.50KB
返回 下载 相关 举报
实验三栈和队列_第1页
第1页 / 共6页
实验三栈和队列_第2页
第2页 / 共6页
实验三栈和队列_第3页
第3页 / 共6页
实验三栈和队列_第4页
第4页 / 共6页
实验三栈和队列_第5页
第5页 / 共6页
点击查看更多>>
资源描述

《实验三栈和队列》由会员分享,可在线阅读,更多相关《实验三栈和队列(6页珍藏版)》请在金锄头文库上搜索。

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;p

2、ublic 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);public st

3、atic 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;public class Seq

4、Stack 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 ? -1: (int)thi

5、s.elementthis.top;public static void input(String str)int i=0;int t=str.length();if(t%2=0)for(i=0;it/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%2=0)for(int i=0;il

6、ength/2;i+)t=str.charAt(length/2+i);if(t!=p.pop()flag=false;break;elsefor(int i=0;ilength/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);p.input(str);flag=pare(str, p);if(fl

7、ag=true)System.out.println(yes);else System.out.println(No);粘贴测试数据及运行结果:3、 使用3个队列分别保留手机上最近10个“未接来电”、“已接来电”、“已拨电话”。粘贴源程序:package Q3;public class SeqQueue public Object element;public int front,rear;public SeqQueue p;public SeqQueue(int length)this.element=new Objectlength;this.front =this.rear =0;pub

8、lic 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.elementthis.rear=x;this.rear=(this.rear+1)%this.element.length;public T dequeue()i

9、f(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.dequeue();i+;System.out.println();public static void fun()long num=0;int t=0;SeqQueue missedC

10、all = new SeqQueue(11);SeqQueue receivedCall = new SeqQueue(11);SeqQueue outgoingCall = new SeqQueue(11);while(!(missedCall.isFull() & receivedCall.isFull() & outgoingCall.isFull()num=(int)(Math.random()*100000000);t=(int)(Math.random()*101);if(t%3=0)missedCall.enquere(num);if(t%3=1)receivedCall.enq

11、uere(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();粘贴测试数据及运行结果:三、 心得体会:(含上机中所遇问题的解决办法,所使用到的编程技巧、创新点及编程的心得)在第三题编程中,我创建了一个isFull的函数来完成判断循环队列是否为满的函数,这样可以来判断三个循环队列元素是否都达到了10个电话号码全满的情况。在未接电话、已接电话、已拨电话是使用随机数来判断的,这样符合人们的习惯,在位置的情况下来完成程序。.

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 建筑/环境 > 设计及方案

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号