zoukankan      html  css  js  c++  java
  • 栈和队列

    栈:先进先出,类似大盒子

    队列:先进后出,类似买票;ps:代码有bug

     1 /** 
     2  * @ClassName: MyStack 
     3  * @Description: 栈 
     4  * @author dongye 
     5  * @date 2016年3月3日 上午9:37:49 
     6  *  
     7  */
     8 public class MyStatck {
     9     //底层实现是数组
    10     private long[] arr;
    11     private int top;
    12     
    13     public MyStatck(){
    14         arr=new long[10];
    15         top=-1;
    16     }
    17     
    18     public MyStatck(int maxSize){
    19         arr=new long[maxSize];
    20         top=-1;
    21     }
    22     
    23     /**
    24      * 
    25     * @Description: 添加数据
    26     * @author dongye  
    27     * @date 2016年3月3日 上午9:41:11 
    28      */
    29     public void push(int value){
    30         top++;
    31         arr[top]=value;
    32     }
    33     
    34     /**
    35      * 
    36     * @Description: 移除数据
    37     * @author dongye  
    38     * @date 2016年3月3日 上午9:42:45 
    39      */
    40     public long pop(){
    41         return arr[top--];
    42         
    43     }
    44 
    45     /**
    46      * 
    47     * @Description: 查看数据 
    48     * @author dongye  
    49     * @date 2016年3月3日 上午9:44:13 
    50      */
    51     public long peek(){
    52         return arr[top];
    53     }
    54     
    55     /**
    56      * 
    57     * @Description: 判断是否为空
    58     * @author dongye  
    59     * @date 2016年3月3日 上午9:45:35 
    60     * @throws
    61      */
    62     public boolean isEmpty(){
    63         return top==-1;
    64     }
    65     
    66     /**
    67      * 
    68     * @Description: 判断是否满了    
    69     * @author dongye  
    70     * @date 2016年3月3日 上午9:45:35 
    71     * @throws
    72      */
    73     public boolean isFull(){
    74         return top==arr.length-1;
    75     }
     1 ** 
     2  * @ClassName: MyQueue 
     3  * @Description: 队列
     4  * @author dongye 
     5  * @date 2016年3月3日 上午10:02:17 
     6  *  
     7  */
     8 public class MyQueue {
     9     //底层使用数组
    10     private long[] arr;
    11     //有效大小
    12     private int elements;
    13     //对头
    14     private int front;
    15     //对尾
    16     private int end;
    17     
    18     public MyQueue(){
    19         arr=new long[5];
    20         front=0;
    21         end=-1;
    22         elements=0;
    23     }
    24     
    25     public MyQueue(int maxsize){
    26         arr=new long[maxsize];
    27         front=0;
    28         end=-1;
    29         elements=0;
    30     }
    31     
    32     /**
    33      * 
    34     * @Description: 添加数据
    35     * @author dongye  
    36     * @date 2016年3月3日 上午10:32:36 
    37      */
    38     public void insert(long value){
    39         if(end==arr.length-1){
    40             end=-1;
    41         }
    42         arr[++end]=value;
    43         elements++;
    44     }
    45     
    46     /** 
    47      * @Description: 删除
    48      * @author dongye  
    49      * @date 2016年3月3日 上午10:38:46 
    50      */
    51     public long remove() {
    52         if(front==arr.length){
    53             front=0;
    54         }
    55         elements--;
    56         return arr[front++];
    57     }
    58     
    59     /**
    60     * @Description: 查看
    61     * @author dongye  
    62     * @date 2016年3月3日 上午10:43:25 
    63      */
    64     public long peek(){
    65         return arr[front];
    66     }
    67     
    68     /**
    69      * 
    70     * @Description: 判断是否为空
    71     * @author dongye  
    72     * @date 2016年3月3日 上午9:45:35 
    73      */
    74     public boolean isEmpty(){
    75         return elements==0;
    76     }
    77     
    78     /**
    79      * 
    80     * @Description: 判断是否满了    
    81     * @author dongye  
    82     * @date 2016年3月3日 上午9:45:35 
    83      */
    84     public boolean isFull(){
    85         return elements==arr.length;
    86     }
    87 
    88 }
  • 相关阅读:
    从一个小程序跳到另一个小程序的写法
    小程序input组件失焦的使用
    小程序弹框wx.showModal的使用
    小程序在选择某一个东西的时候,可以用if,else 来做
    reverse啥时候可以用
    js中array.some()的用法
    element ui的table的头部自定义
    逻辑表达式
    cookie
    命名
  • 原文地址:https://www.cnblogs.com/snow1314/p/5240975.html
Copyright © 2011-2022 走看看