实验三-栈和队列_第1页
实验三-栈和队列_第2页
实验三-栈和队列_第3页
免费预览已结束,剩余1页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、实验报告三 栈和队列一、 实验目的:1掌握栈的根本操作的实现方法。2利用栈先进后出的特点,解决一些实际问题。3掌握链式队列及循环队列的根本操作算法。4应用队列先进先出的特点,解决一些实际问题。 二、 实验内容:1、 使用一个栈,将一个十进制转换成二进制。 粘贴源程序: package Q1;public class SeqStack publicintelement ;publicinttop ;publicstaticSeqStack p;publicSeqStack( int size)this . element =new int size; this . top =-1;public

2、void push( int x)this . top +;this . element this . top =x;public int pop(). top -;. top ;return this . top =-1 ? -1: (int )this . element thispublic int get()return this . top =-1 ? -1: (int )this . element thispublic static void disp(SeqStack p)int t = -2;while (t!=-1) t=p.pop(); if (t!=-1)System.

3、 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);粘贴测试数据及运行结果:<terminated110&011|2、回文是指正读反读均相同的字符序列,如"acdca、“dceecd 均是回文,但"book不是回文。利用1中

4、的根本算法,试写一个算法判定给定的字符串是否为回文。提示:将半字符入栈,依次弹出与另一半逐个比拟 粘贴源程序:package Q2;public classSeqStack publicint element ;publicint top ;publicstatic SeqStackp;publicSeqStack( intsize)this.element =newint size;this.top =-1;public void push( int x)this . top +;this . element this . top =x;public int pop()return this

5、 . top =-1 ? -1:(public int get()return this . top =-1 ? -1:(int ) this . element this . top -;int ) this . element this . top ;public static void input(String str)int i=0;int t=str.le ngth();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

6、staticboolea n compare(Str ing str,SeqStack p)boolean flag = true ;char t;int len gth=str.le ngth();if (length%2=0)for (int i=0;i<length/2;i+)t=str.charAt(le ngth/2+i);if (t!=p.pop()flag= false ; break ; else for (int i=0;i<length/2+1;i+)t=str.charAt(le ngth/2+i);if (t!=p.pop()flag= false ; br

7、eak ;return flag;public static void main( Str in g args) boolea n flag;p=new SeqStack(100);Stri ng str = new String( "acbca");p. input (str):flag= p pare (str. p):if (flag= true )System. out .println("yes");else System. out .println( "No");粘贴测试数据及运行结果:3、使用3个队列分别保存上最近10个

8、“未接来电、“已接来电、“已拨粘贴源程序:package Q3;public class SeqQueue<T> publicObject element ;publicint front,rear ;publicSeqQueuep;publicSeqQueue(int len gth)this.element=new Objectle ngth;this.front =this . rear =0;public boolea n isEmpty()return this . front = this . rearpublicboolea n isFull()boolea n fl

9、ag= false ;if (this .front != this . rear && ( this . rear +1)%this . eleme nt 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 n ull ;T temp=(T) this _ele

10、ment Lthis .front 丄this . front =( this . front +1)%this . element . length ;return temp;public static void disp(SeqQueue p)int i=1;.length =while (p.isEmpty()!= true )System. out .println(i+"."+p.dequeue();i+;System. out .println();public static void fun() long num=0;new SeqQueue(11);new

11、SeqQueue(11);int t=0;SeqQueue missedCall =SeqQueue receivedCall =SeqQueue outgo in gCall =new SeqQueue(11);while (!(missedCall.isFull() && receivedCall.isFull() && outgoi ngCall.isFull()num=( int )(Math.random ()*100000000);t=( int )(Math. random ()*101);if (t%3=0)missedCall.e nquere

12、 (n um);if (t%3=1)receivedCall.e nquere( num);if (t%3=2)outgo in gCall.e nquere (n um);"missedCall" );"receivedCall")"outgoi ngCall")main( Stri ng args) System. out .println( disp (missedCall);System. out .println( disp (receivedCall);System. out .println( disp (outgoingCall);public static voidfun ();粘贴测试数据及运行结果:4.41ME2E百用7W27A6127S82631824«57rsceiyedcall&47S*,4697426.7783 &05 37.92174S25均眈弼旳outgpi ngCall1-B324BM72.m3tl9353.274277285u673107S56-777&&

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论