实验报告三栈和队列一、实验目的:(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 int[size];this.top=-1;}public void push(int x){this.top++;this.element[this.top]=x;}public int pop(){return this.top==-1 ? -1: (int)this.element[this.top--];}public int get(){return this.top==-1 ? -1: (int)this.element[this.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 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;public class SeqStack {public int element[];public int top;public static SeqStack p;public SeqStack(int size){this.element=new int[size];this.top=-1;}public void push(int x){this.top++;this.element[this.top]=x;}public int pop(){return this.top==-1 ? -1: (int)this.element[this.top--];}public int get(){return this.top==-1 ? -1: (int)this.element[this.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));}else{for(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;i<length/2;i++){t=str.charAt(length/2+i);if(t!=p.pop()){flag=false;break;}}}else{for(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");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 SeqQueue(int length){this.element=new Object[length];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.element[this.rear]=x;this.rear=(this.rear+1)%this.element.length;}public T dequeue(){if(isEmpty())return null;T temp=(T)this.element[this.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 missedCall = 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.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();}}粘贴测试数据及运行结果:三、心得体会:(含上机中所遇问题的解决办法,所使用到的编程技巧、创新点及编程的心得)在第三题编程中,我创建了一个isFull的函数来完成判断循环队列是否为满的函数,这样可以来判断三个循环队列元素是否都达到了10个电话号码全满的情况。
在未接电话、已接电话、已拨电话是使用随机数来判断的,这样符合人们的习惯,在位置的情况下来完成程序。