数组实现顺序栈与队列
来源:互联网 发布:oracle性能优化 编辑:程序博客网 时间:2024/05/08 04:08
任务和代码:
package com.zf.s4;class Stack{ //实现栈的类long stackArray[]; //栈数组int size; //栈的大小int top; //栈的顶部public Stack(int size){ //构造方法初始化大小为size的栈this.size=size;this.stackArray=new long[size];this.top=-1;} public long pop(){ //出栈操作return stackArray[top--];}public void push(long value){ //入栈操作stackArray[++top]=value;}public boolean isEmpty(){ //判断栈是否为空return top==-1;}public boolean isFull(){ //判断栈是否已满return top==size-1;}public long peek(){ //栈顶元素return stackArray[top];}} class Queue{ //实现顺序队列的类 private long queueArray[]; //队列数组 private int front; //队列的前端下标 private int rear; //队列的尾端下标 private int size; //队列的大小 private int count; //队列中元素的个数 public Queue(int size){ //构造方法初始大小为size的队列 this.queueArray=new long[size]; this.size=size; this.front=0; this.rear=-1; this.count=0; } public void insert(long value){ //插入操作 if(rear==size-1) //队列已满 rear=-1; queueArray[++rear]=value; count++; } public long remove(){ //删除操作 long temp=queueArray[front++]; if(front==size) front=0; count--; return temp; } public long peakFront(){ //返回队列第一个元素 return queueArray[front]; } public boolean isEmpty(){ //判断是否为空 return count==0; } public boolean isFull(){ //判断队列是否已满 return count==size; } public void print(){ //输出队列元素 for(int i=front;i<front+count;i++){ System.out.print(queueArray[i]+"\t"); } System.out.println(); } } class PriorityQueue{ //实现优先队列的类 private int count; //队列中元素的个数 private long priorityArray[]; //队列数组 private int size; public PriorityQueue(int size){ //构造方法初始化大小为size的队列 this.size=size; this.priorityArray=new long[size]; this.count=0; } public void insert(long value){ //插入操作 int i; if(count==0) priorityArray[count++]=value; else{ for(i=count-1;i>=0;i--){ //循环找到比插入值大的位置 if(value<priorityArray[i]){ priorityArray[i+1]=priorityArray[i];//依次移动位置 }else break; } priorityArray[i+1]=value; //插入值放到指定位置 count++; } } public long remove(){ //删除操作 return priorityArray[--count]; } public boolean isEmpty(){ //判断是否为空 return count==0; } public boolean isFull(){ //判断是否已满 return count==size; } public void print(){ //输出队列元素 for(int i=0;i<count;i++) System.out.print(priorityArray[i]+"\t"); System.out.println(); } }public class TextStackAndQueue {public static void main(String[] args) {// TODO 自动生成的方法存根System.out.println("1.数组实现顺序栈");Stack stack=new Stack(6); //实例化顺序栈,栈的大小为6while(!stack.isFull()){ //只要栈不满便循环long r=(long)(Math.random()*20);stack.push(r); //入栈System.out.print(r+"\t");} System.out.println(); while(!stack.isEmpty()){ //只要栈不为空便循环 long value=stack.pop(); //获取栈顶元素 System.out.print(value+"\t"); } System.out.println(); System.out.println("-----------------------"); System.out.println("2.数组实现顺序队列"); Queue queue=new Queue(6); //实例化队列,队列大小为6 while(!queue.isFull()){ //只要队列不满便循环 long value=(long)(Math.random()*20); queue.insert(value); //元素插入队列 } queue.print(); while(!queue.isEmpty()){ //只要栈不满便循环 queue.remove(); //元素移除 queue.print(); //出入队列元素 } queue.print(); System.out.println("----------------------"); System.out.println("3.数组实现优先队列"); PriorityQueue priority=new PriorityQueue(6); //实例化顺序队列,队列的大小为6 while(!priority.isFull()){ long value=(long)(Math.random()*20); priority.insert(value); } priority.print();}}运行结果:
知识点:
代码中出现System.out.print(value+"\t");其中\t的意思是:在同一个缓冲区内横向跳8个空格.相当于键盘的Tab键
0 0
- 数组实现顺序栈与队列
- Java数组实现顺序栈与队列
- 数组实现顺序队列、顺序循环队列
- 队列的顺序实现(循环数组)与链式实现
- Java 数组实现顺序队列
- 数据结构与算法-----队列-使用数组(顺序结构)实现
- Java用数组实现顺序队列
- java使用数组实现顺序队列
- Java用数组实现顺序队列
- Java用数组实现顺序队列
- C语言数据结构-顺序队列-数组实现
- 两个顺序栈实现顺序队列功能
- 顺序表实现队列-栈
- 顺序栈-数组实现
- 队列的顺序表示与实现
- java 队列与栈实现(链表与数组)
- 栈与队列-顺序队列与链队列类模板的实现(数据结构基础 第3周)
- 循环队列Queue--使用顺序存储结构(数组)实现
- 第21题
- 【瞎搞】 Codeforces Round #215 (Div. 1) A Sereja and Algorithm
- 搬家APP-竞品分析报告
- Jsoup解析html页面实现CSDN博客客户端
- 学习设计模式五步走
- 数组实现顺序栈与队列
- Nginx 在 ubuntu 14.04上的安装配置。
- 51页第18题
- 字符,字符集,编码之间的区别
- android网易顶部导航栏demo .
- Log4j配置详解
- USACO--2.4Fractions to Decimals
- 2-19(是否交替出现)
- 设计模式 笔记 单例模式 Singleton