zoukankan      html  css  js  c++  java
  • Queue的java实现

     1 package com.liu.queue;
     2 
     3 public class QueueApp {
     4     public static void main(String args[])
     5     {
     6         Queue theQueue = new Queue(5);
     7         
     8         theQueue.insert(10);
     9         theQueue.insert(20);
    10         theQueue.insert(30);
    11         //theQueue.insert(40);
    12         
    13         theQueue.remove();
    14         theQueue.remove();
    15         theQueue.remove();
    16         
    17         theQueue.insert(50);
    18         theQueue.insert(60);
    19         theQueue.insert(70);
    20         theQueue.insert(80);
    21         
    22         while(!theQueue.isEmpty())
    23         {
    24             long n = theQueue.remove();
    25             System.out.print(n);
    26             System.out.print(" ");
    27         }
    28         System.out.println("");
    29     }
    30 }
    31 
    32 class Queue
    33 {
    34     private int maxsize;
    35     private long[] queArray;
    36     private int front;
    37     private int rear;
    38     private int nItems;
    39     
    40     public Queue(int s)
    41     {
    42         maxsize = s;
    43         queArray = new long[maxsize];
    44         front = 0;
    45         rear = -1;
    46         nItems = 0;
    47     }
    48     
    49     public void insert(long j)
    50     {
    51         if(rear == maxsize-1)
    52             rear = -1;//如果尾指针已经到了最后,则循环
    53         queArray[++rear] = j;
    54         nItems++;
    55     }
    56     
    57     public long remove()
    58     {
    59         long temp = queArray[front++];
    60         if(front == maxsize)
    61             front = 0;
    62         nItems--;
    63         return temp;
    64     }
    65     
    66     public long peekFront()
    67     {
    68         return queArray[front];
    69     }
    70     
    71     public boolean isEmpty()
    72     {
    73         return (nItems == 0);
    74     }
    75     
    76     public boolean isFull()
    77     {
    78         return (nItems == maxsize);
    79     }
    80     
    81     public int size()
    82     {
    83         return nItems;
    84     }
    85     
    86     
    87 }
  • 相关阅读:
    Python 字典方法(.get .item)
    Python格式化输出
    R sprintf函数
    r 中sub() gsub()等匹配与替换函数
    R read.csv数据框
    C#中使用ref、out、params例子
    C#中的三种委托方式:Func委托,Action委托,Predicate委托
    tfs强制撤销解锁命令
    Json序列化与反序列化
    XPath语法在C#中使用XPath示例第二讲
  • 原文地址:https://www.cnblogs.com/speaklessdomore/p/3665806.html
Copyright © 2011-2022 走看看