数组实现顺序栈与队列

来源:互联网 发布: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