




已阅读5页,还剩2页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
数据结构上机一目的: Stack and Queue 是最为简单的数据结构,然而它在一些重要的应用中是极为基本的。本次实验将练习Stack and Queue的一些操作,通过本次实验熟悉Stack and Queue。步骤:1 仅使用Stack类中的构造函数、push()、peek()、pop()、以及empty()等方法编写以下方法: Public Stack reversed (Stack stack,int maxSize);/返回一个新栈,该栈中包含了原指定栈中的内容,但内容的次序与原栈正好相反。2仅使用Stack类中的构造函数、push()、peek()、pop()、以及empty()等方法编写以下方法:Public int popbottom(Stack stack,int maxSize);/返回栈底的元素,并将该元素从栈中移出3仅使用ArrayQueue类中的构造函数、enqueue()、dequeue()、以及empty()等方法编写以下方法: Puplic static ArrayQueue merge (ArrayQueue q1, ArrayQueue q2,int num);/将两个队列合并到一起,合并的方式为交替使用两队列中的元素,并把剩余的队列中的元素添加到最后,并将合并产生的新队列返回答案:/Stack.javaclass Stack private int maxSize; / size of stack array private long stackArray; private int top; / top of stack/- public Stack(int s) / constructor maxSize = s; / set array size stackArray = new longmaxSize; / create array top = -1; / no items yet /- public void push(long j) / put item on top of stack stackArray+top = j; / increment top, insert item /- public long pop() / take item from top of stack return stackArraytop-; / access item, decrement top /- public long peek() / peek at top of stack return stackArraytop; /- public boolean isEmpty() / true if stack is empty return (top = -1); /- public boolean isFull() / true if stack is full return (top = maxSize-1); /- public Stack reversed(Stack stack,int maxSize) Stack a=new Stack(maxSize); while(!stack.isEmpty() a.push(stack.pop(); return a; /- public long popbottom(Stack stack ,int maxSize) Stack a=new Stack(maxSize); while(!stack.isEmpty() a.push(stack.pop(); long temp=a.pop(); while(!a.isEmpty() stack.push(a.pop(); return temp; / end class StackX/class StackApp public static void main(String args) Stack theStack = new Stack(10); / make new stack theStack.push(20); / push items onto stack theStack.push(40); theStack.push(60); theStack.push(80); System.out.println(theStack.popbottom(theStack, 10); while( !theStack.isEmpty() ) / until its empty, / delete item from stack long value = theStack.pop(); System.out.print(value); / display it System.out.print( ); / end while System.out.println(); /Stack newStack=(Stack)theStack.reversed(theStack, 10); / end while / end main() / end class StackApp/class ArrayQueue private int maxSize; private long queArray; private int front; private int rear; private int nItems;/- public ArrayQueue(int s) / constructor maxSize = s; queArray = new longmaxSize; front = 0; rear = -1; nItems = 0; /- public void enqueue(long j) / put item at rear of queue if(rear = maxSize-1) / deal with wraparound rear = -1; queArray+rear = j; / increment rear and insert nItems+; / one more item /- public long dequeue() / take item from front of queue long temp = queArrayfront+; / get value and incr front if(front = maxSize) / deal with wraparound front = 0; nItems-; / one less item return temp; /- public long peekFront() / peek at front of queue return queArrayfront; /- public boolean isEmpty() / true if queue is empty return (nItems=0); /- public boolean isFull() / true if queue is full return (nItems=maxSize); /- public int size() / number of items in queue return nItems; /- static ArrayQueue c; public static ArrayQueue merge (ArrayQueue q1, ArrayQueue q2 ,int num) ArrayQueue c=new ArrayQueue(num); while(!q1.isEmpty()&!q2.isEmpty() c.enqueue(q1.dequeue();c.enqueue(q2.dequeue(); if(q1.isEmpty() while(!q2.isEmpty()c.enqueue(q2.dequeue(); else while(!q1.isEmpty()c.enqueue(q1.dequeue(); return c; / class QueueApp public static void main(String args) ArrayQueue theQueue1 = new ArrayQueue(5); / queue holds 5 items ArrayQueue theQueue2 = new ArrayQueue(5); theQueue1.enqueue(10); / insert 4 items theQueue1.enqueue(20); theQueue1.enqueue(30); theQueue1.enqueue(40); theQueue1.dequeue(); / remove 3 items theQueue1.dequeue(); / (10, 20, 30) theQueue1.dequeue(); theQueue1.enqueue(50); / insert 4 more items theQueue1.enqueue(60); / (wraps around) theQueue1.enqueue(70); theQueue1.enqueue(80); theQueue2.enqueue(11); / insert 4 items theQueue2.enqueue(23); theQueue2.enqueue(36); theQueue2.enqueue(90); ArrayQueue newQueue = new ArrayQueue(10); newQueue=(ArrayQueue)newQueue.merge(theQ
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 三明事业单位笔试真题2025
- 农发行长春市公主岭市2025秋招笔试英语题专练及答案
- 农发行滨州市惠民县2025秋招群面模拟题及高分话术
- 数字艺术作品版权保护与版权交易平台法律风险研究报告:2025年防范与应对策略
- 平等尊重你我他课件
- 新能源行业2025年技术创新驱动产业升级研究报告
- 环保产业园循环经济模式与绿色金融产品创新应用报告
- 2025年生物质能源供应链信息化解决方案报告
- 平煤矿井安全培训理念课件
- 2025-2030年新能源汽车核心零部件供应链金融风险控制策略报告
- 2025房屋宅基地买卖合同
- 2025年浙江警务辅助人员招聘考试(写作)历年参考题库含答案详解
- 上饶市属国有企业2025年度第一批次公开招聘【105人】考试参考题库及答案解析
- (新版)《普速铁路工务安全规则》考试题库及答案(含各题型)
- 小儿腺病毒感染护理查房
- 毒品成瘾原因课件
- 2025年疾病预防控制中心招聘考试笔试试题含答案
- 金太阳福建省2025-2026学年高三上学期9月开学联考数学试卷
- 2025年甘肃政治高考试题及答案
- 三腔二囊管护理课件
- 商户维护与管理办法
评论
0/150
提交评论