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(); }
    }
  • 相关阅读:
    爬虫_requests
    第十四周总结
    《三个和尚》观看感悟
    第十三周总结
    ThinkPHP
    ThinkPHP配置安装
    ThinkPHP_1
    构建之法阅读笔记六
    网络工程师必备学习内容!深度理解OSPF——OSPF是什么?为什么要用OSPF?
    网络工程师学习笔记——RIP路由汇总实验配置
  • 原文地址:https://www.cnblogs.com/xxlong/p/4980132.html
Copyright © 2011-2022 走看看