• 用数组结构实现大小固定的栈和队列


    给定长度为size的数组,将其长度设置为大小为size的栈(队列),添加的数字如果超过了既定的长度就报错!

    先实现栈(先进后出);

    后实现队列(先进先出);

    代码如下:

      1 //例子:长度为6的数组,设置大小为6的栈,超过长度就会报错
      2 //用数组结构实现大小固定的队列和栈
      3 public class Bokeyuan {
      4     //使用数组实现栈
      5     public static class ArrayStack{
      6         private Integer[] arr;//数组
      7         private Integer size;//
      8         
      9         public ArrayStack(int initSize) {//固定数组实现栈;initSize确定数组多大
     10             if(initSize <0) {
     11                 throw new IllegalArgumentException("The init size is less than 0");
     12             }
     13             arr=new Integer[initSize];
     14             size=0;
     15         }
     16     public Integer peek() {//返回栈顶保留原值
     17         if(size==0) {
     18             return null;
     19         }
     20         return arr[size-1];
     21     }
     22     public void push (int obj) {//新进入一个数
     23         if(size==arr.length) {//当数组长度等于栈大小时不能再添加数值
     24             throw new ArrayIndexOutOfBoundsException("The queue is full");
     25         }
     26         arr[size++]=obj;//否则添加数值,size++
     27     }
     28     public Integer pop() {//当size=0,空了 报错
     29         if(size==0) {//进行数字的添加和弹出操作
     30             throw new ArrayIndexOutOfBoundsException("The queue is empty");
     31         }
     32         return arr[--size];//当size=0,将size-1位置给用户,然后size--;
     33     }
     34     }
     35     
     36     //使用数组实现队列
     37     public static class ArrayQueue{
     38         private Integer [] arr;
     39         private Integer size;//约束start、end
     40         private Integer start;//表示取一个数取哪里的
     41         private Integer end;//新添加的数加到哪里
     42         /* 1.当size<n,把数放到end上
     43          * 2.当size!=0,start指的数字给用户*/
     44         
     45     public ArrayQueue(int initSize) {//确定数组大小
     46         if(initSize<0) {
     47             throw new IllegalArgumentException("The initSize is less than 0");
     48         }
     49         arr=new Integer[initSize];
     50         size=0;
     51         start=0;
     52         end=0;
     53         }
     54     public Integer peek() {
     55         if(size==0) {
     56             return null;
     57         }
     58         return arr[start];
     59     }
     60     public void push(int obj) {//当size=数组长度时,报错不能继续添加数值
     61         if(size==arr.length) {
     62             throw new ArrayIndexOutOfBoundsException("The queue is full");
     63         }
     64         size++;//否则添加到end所指位置
     65         arr[end]=obj;
     66         end=end==arr.length-1? 0:end+1;//到底部的时候end跳回0,当没有到底的时候,就last+1
     67     }
     68     public Integer poll() {//弹出返回值
     69         if(size==0) {//当size=0,报错空了
     70             throw new ArrayIndexOutOfBoundsException("The queue is empty");
     71         }
     72         size--;
     73         int tmp=start;//记录start的位置
     74         start=start==arr.length-1? 0:start+1;//调整start位置,到底就返回开头,否则+1
     75         return arr[tmp];//返回
     76     }
     77     }
     78     public static void main(String[] args) {
     79         //测试栈  正好装满
     80          ArrayStack stack = new ArrayStack(3);
     81          stack.push(1);
     82          stack.push(2);
     83          stack.push(3);
     84             
     85          System.out.println(stack.pop());
     86          System.out.println(stack.pop());
     87          System.out.println(stack.pop());
     88          System.out.println();
     89          //测试队列  添加的数字超过长度满了
     90          ArrayQueue queue =new ArrayQueue(2);
     91          queue.push(3);
     92          queue.push(2);
     93          queue.push(2);
     94          
     95          System.out.println(queue.poll());
     96          System.out.println(queue.poll());
     97          System.out.println(queue.poll());
     98          
     99     }
    100     }
  • 相关阅读:
    jquery index与eq
    尝试一下
    document
    2017-03-28 java script DOM操作
    2017-03-25 CSS 样式
    CSS 样式表分类
    CSS 样式表
    HTML 框架
    表格
    HTML常用标记
  • 原文地址:https://www.cnblogs.com/Vsxy/p/10472872.html
Copyright © 2020-2023  润新知