线性表之队列的实现

java 代码
 
  1. /** 
  2.  * Queue.java 
  3.  * 线性表之队列 
  4.  * 队列有如下特点: 
  5.  * 先进先出 
  6.  * 即,从尾部添加(push)新数据 
  7.  *    从头部取出(pop)数据 
  8.  */  
  9.   
  10.   
  11. /** 
  12.  * 队列(Queue)也是一种运算受限的线性表。 
  13.  * 它只允许在表的一端进行插入,而在另一端进行删除。 
  14.  * 允许删除的一端称为队头(front),允许插入的一端称为队尾(rear)。 
  15.  */  
  16. package line;  
  17.   
  18.   
  19. /** 
  20.  * @author sunxboy 
  21.  * 9:59:59 AM  May 22, 2007 
  22.  */  
  23. public class Queue {  
  24.   
  25.     int data[];  
  26.     int maxSize;  
  27.     int size;  
  28.     int front;  //允许删除的一端  
  29.     int rear;   //允许插入的一端  
  30.     public Queue(int maxSize) {  
  31.         this.maxSize = maxSize;  
  32.         this.data = new int[maxSize];  
  33.         size= 0;  
  34.         rear = 0;  
  35.         front =0;  
  36.     }  
  37.       
  38.     public boolean isEmpty() {  
  39.         return size==0;  
  40.     }  
  41.       
  42.     public boolean isFull() {  
  43.         return size==maxSize;  
  44.     }  
  45.       
  46.     /** 
  47.      * 循环队列 
  48.      * @param data 
  49.      * @return 
  50.      */  
  51.     public boolean push(int data)throws Exception {  
  52.           
  53.         if(isFull())  
  54.             throw new Exception("队列已满!");  
  55.         size++;  
  56.         this.data[rear]= data;  
  57.               
  58. //          if(rear+1==maxSize)  
  59. //                 rear=0;  
  60. //           else  
  61. //               rear++;  
  62.   
  63.             rear=(rear+1)%maxSize;  
  64.           
  65.         return true;  
  66.   
  67.     }  
  68.       
  69.     public int pop() throws Exception{  
  70.         int temp;  
  71.         if(isEmpty())  
  72.             throw new Exception("队列是空的.");  
  73.         temp = this.data[this.front];  
  74.   
  75.         this.size--;  
  76. //      if(front+1==maxSize)  
  77. //             front=0;  
  78. //       else  
  79. //            front++;  
  80.         front=(front+1)%maxSize;  
  81.         return temp;  
  82.     }  
  83.       
  84.       
  85.     public static void main(String[] args) throws Exception{  
  86.         Queue queue=new Queue(10);  
  87.         queue.push(1);  
  88.         queue.pop();  
  89. //      queue.pop();  
  90.         queue.push(2);  
  91.         queue.push(3);  
  92.         queue.push(4);  
  93.         queue.push(5);  
  94.         queue.push(6);  
  95.         queue.push(7);  
  96.         queue.push(8);  
  97.         queue.push(9);  
  98.         queue.push(10);  
  99. //      queue.push(11);  
  100.         while(!queue.isEmpty())  
  101.         {  
  102.             System.out.println(queue.pop());  
  103.         }  
  104.           
  105.     }  
  106. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值