zoukankan      html  css  js  c++  java
  • 四、队列

    1、循环队列操作(数组实现)

    // to run this program: C>java QueueApp
    class Queue { private int maxSize; private long[] queArray; private int front; private int rear; public Queue(int s) { maxSize = s; queArray = new long[maxSize]; front = rear = 0; } public boolean insert(long j) { if(isFull()) return false; else
    {   queArray[rear] = j;    rear=(rear+1)%maxSize;    return true;    } } public Long remove() { if(isEmpty()) return null; else
    {   long value = queArray[front]; front = (front+1)%maxSize;   return value;   } } public Long peekFront() {
        if(!isEmpty())   
    return queArray[front];
        else
          return null;
    }
    public boolean isEmpty() { return (front==rear); } public boolean isFull() {  return (front==(rear+1)%maxSize); } public int size() {    return (rear-front+maxSize)%maxSize; } } // end class Queue
    //===========================================
    class QueueApp { public static void main(String[] args) { Queue theQueue = new Queue(10); theQueue.insert(10); theQueue.insert(20); theQueue.insert(30); theQueue.insert(40); theQueue.remove(); theQueue.remove(); theQueue.remove(); theQueue.insert(50); theQueue.insert(60); theQueue.insert(70); theQueue.insert(80); while( !theQueue.isEmpty() ) { long n = theQueue.remove(); System.out.print(n); System.out.print(" "); }  System.out.println(""); } }

    2、队列的链表实现

    // to run this program: C>java LinkQueueApp
    class Link { public long dData; public Link next; public Link(long d) { dData = d; } public void displayLink() { System.out.print(dData + " "); } }
    //===========================================
    class FirstLastList { private Link first; private Link last; public FirstLastList() { first = null; last = null; } public boolean isEmpty() { return first==null; } public void insertLast(long dd) { Link newLink = new Link(dd); if( !isEmpty() ) {
          last.next = newLink;
          last = newLink;
        } else   
        {
          first = newLink;
          last = newLink;
        } } public Long deleteFirst() {     if(!isEmpty)
        {
          Link temp = first;
          if(first.next == null)
          {
            first = null;
            last = null;
          }
          else
            first = first.next;
          return temp.dData;
        }
        else
          return null; } public void displayList() { Link current = first; while(current != null) { current.displayLink(); current = current.next; } System.out.println(""); } } // end class FirstLastList
    //=======================================
    class LinkQueue { private FirstLastList theList; public LinkQueue() { theList = new FirstLastList(); } public boolean isEmpty() { return theList.isEmpty(); } public void insert(long j) { theList.insertLast(j); } public Long remove() { return theList.deleteFirst(); } public void displayQueue() { System.out.print("Queue (front-->rear): "); theList.displayList(); } }
    //============================================
    class LinkQueueApp { public static void main(String[] args) { LinkQueue theQueue = new LinkQueue(); theQueue.insert(20); theQueue.insert(40); theQueue.displayQueue(); theQueue.insert(60); theQueue.insert(80); theQueue.displayQueue(); theQueue.remove(); theQueue.remove(); theQueue.displayQueue(); }
    }
  • 相关阅读:
    java反射详解 (转至 http://www.cnblogs.com/rollenholt/archive/2011/09/02/2163758.html)
    DexClassLoader
    防止 apk反编译 jocky-- java混淆代码 (转至:http://my.oschina.net/f839903061/blog/72554)
    Android APK反编译详解(附图) (转至 http://blog.csdn.net/ithomer/article/details/6727581)
    双卡手机怎么指定SIM卡打电话
    android设备休眠
    GSON使用笔记(3) -- 如何反序列化出List
    Notepad++ 更换主题
    如何才能成为一个成功的项目经理
    项目经理是干出来的,不是学出来的;是带出来的,不是教出来的
  • 原文地址:https://www.cnblogs.com/xxlong/p/4980132.html
Copyright © 2011-2022 走看看