zoukankan      html  css  js  c++  java
  • java 栈和队列的模拟--java

    栈的定义:栈是一种特殊的表这种表只在表头进行插入和删除操作。因此,表头对于栈来说具有特殊的意义,称为栈顶。相应地,表尾称为栈底。不含任何元素的栈称为空栈。 
    栈的逻辑结构:假设一个栈S中的元素为an,an-1,..,a1,则称a1为栈底元素,an为栈顶元 素。栈中的元素按a1 ,a2,..,an-1,an的次序进栈。在任何时候,出栈的元素都是栈顶元素。换句话说,栈的修改是按后进先出的原则进行的.因此,栈又称为后进先出(Last In First Out)表,简称为LIFO表。所以,只要问题满足LIFO原则,就可以使用栈。
    notice:换句话说,栈就是可以一个元素进后,可以接着进行输出的表.
    java 模拟栈操作:
     1 package com.zsy.stack;
     2 /**
     3  * 栈 java 实现  先进后出
     4  * @author 偶my耶
     5  *
     6  */
     7 public class stack {
     8     private Object[] stack;
     9 
    10     //元素个数;
    11     private int size;
    12     
    13     //默认长度为10;
    14     public stack(){
    15         this(10);
    16     }
    17     
    18     //也可以自己设置长度,即容量;
    19     public stack(int len){
    20         stack = new Object[len];
    21     }
    22     
    23     //返回元素个数;
    24     public int size(){
    25         return size;
    26     }
    27     
    28     //返回数组长度,即容量;
    29     public int capacity(){
    30         return stack.length;
    31     }
    32     
    33     //实现动态的数组;
    34     public void ensureCapacity(){
    35         if(size() == capacity()){
    36             Object[] newStack = new Object[size() * 3 / 2 + 1];
    37             System.arraycopy(stack, 0, newStack, 0, size());
    38             stack = newStack;
    39         }
    40     }
    41     
    42     //入栈;
    43     public void push(Object o){
    44         size++;
    45         ensureCapacity();
    46         stack[size - 1] = o;
    47     }
    48     
    49     
    50     
    51     //判空;
    52     public boolean isEmpty(){
    53         return size == 0;
    54     }
    55     
    56     //出栈;
    57     public Object pop(){
    58         //首先要判空;
    59         if(isEmpty()){
    60             throw new ArrayIndexOutOfBoundsException("不能为空");
    61         }
    62         
    63         Object o = stack[--size];
    64         stack[size] = null;
    65         return o;
    66     }
    67     
    68     
    69     public static void main(String[] args) {
    70         stack stack = new stack(3);
    71         String[] data = new String[] { "a", "b", "c1" ,"f2","s3", "a4", "b5", "c6" , "a7", "b", "c" };
    72         for(String i:data)
    73         {    stack.push(i);
    74             System.out.print(i + "	");
    75         }
    76     
    77         System.out.println("
    ***********************************");
    78         while (!stack.isEmpty()) {
    79             System.out.print(stack.pop() + "	");
    80         }
    81         //} 
    82     }
    83 } 

    运行效果:

    队列
      在java5中新增加了java.util.Queue接口,用以支持队列的常见操作。该接口扩展了java.util.Collection接口。
    Queue使用时要尽量避免Collection的add()和remove()方法,而是要使用offer()来加入元素,使用poll()来获取并移出元素。它们的优
    点是通过返回值可以判断成功与否,add()和remove()方法在失败的时候会抛出异常。 如果要使用前端而不移出该元素,使用
    element()或者peek()方法。
    值得注意的是LinkedList类实现了Queue接口,因此我们可以把LinkedList当成Queue来用。

    java代码模拟:

     1 package com.zsy.stack;
     2 /**
     3  *     java 队列实现 先进先出
     4  * @author 偶my耶
     5  *
     6  */
     7 public class Queue {
     8      
     9             private int maxSize; //队列长度,由构造函数初始化
    10             private long[] queArray; // 队列
    11             private int front; //队头
    12             private int rear; //队尾
    13             private int nItems;  //元素的个数
    14         
    15             public Queue(int s)           // 构造函数
    16                {
    17                maxSize = s;
    18                queArray = new long[maxSize];
    19                front = 0;
    20                rear = -1;
    21                nItems = 0;
    22                }
    23         
    24             public void insert(long j)    // 进队列
    25                {
    26                if(rear == maxSize-1)          // 处理循环
    27                   rear = -1;
    28                queArray[++rear] = j;          // 队尾指针加1,把值j加入队尾
    29                nItems++;                    
    30                }
    31         
    32             public long remove()          // 取得队列的队头元素。
    33                {
    34                long temp = queArray[front++]; // 取值和修改队头指针
    35                if(front == maxSize)            // 处理循环
    36                   front = 0;
    37                nItems--;                      
    38                return temp;
    39                }
    40         
    41             public long peekFront()       // 取得队列的队头元素。该运算与 remove()不同,后者要修改队头元素指针。
    42                {
    43                return queArray[front];
    44                }
    45         
    46             public boolean isEmpty()     // 判队列是否为空。若为空返回一个真值,否则返回一个假值。
    47                {
    48                return (nItems==0);
    49                }
    50         
    51             public boolean isFull()      // 判队列是否已满。若已满返回一个真值,否则返回一个假值。
    52                {
    53                return (nItems==maxSize);
    54                }
    55         
    56             public int size()            // 返回队列的长度
    57                {
    58                return nItems;
    59                }
    60         public static void main(String[] args)
    61            {
    62             Queue theQueue = new Queue(5);   // 队列有5个元素
    63 
    64            theQueue.insert(10);             // 添加4个元素
    65            theQueue.insert(20);
    66            theQueue.insert(30);
    67            theQueue.insert(40);
    68 
    69            theQueue.remove();               // 移除3个元素
    70            theQueue.remove();               // (10, 20, 30)
    71            theQueue.remove();
    72 
    73            theQueue.insert(50);             // 添加4个元素
    74            theQueue.insert(60);           
    75            theQueue.insert(70);
    76            theQueue.insert(90);
    77            while( !theQueue.isEmpty() )     // 遍历队列并移除所有元素
    78               {                            
    79               long n = theQueue.remove();   // (40, 50, 60, 70, 80)
    80               System.out.print(n);
    81               System.out.print(" ");
    82               }
    83            System.out.println("");
    84            }  
    85 }

    运行效果:

  • 相关阅读:
    大数斐波那契数列
    1、硬件IO口配置;
    1通过URL对象的openStream()方法能够得到指定资源的输入流。
    题意:
    一、作用:
    倒是在网上有看到显卡没装风尚导致该问题的,最后换了一个带风扇的显卡就解决的:
    Jeewx 捷微管家操作配置文档(开源版本号)
    工业控制系统USB存储设备可信管理方案的(ICICS2015)论文PPT:TMSUI: A Trust Management Scheme
    java.lang.ArithmeticException: Rounding necessary
    springcloud 熔断处理
  • 原文地址:https://www.cnblogs.com/oumyye/p/4199951.html
Copyright © 2011-2022 走看看