• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

dream311

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

四、队列

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(); }
}

 

posted on 2015-11-20 11:16  dream311  阅读(147)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3