Advertisement
FahimFaisal

ListQueue

Mar 15th, 2020
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.10 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package QueueClassPractice;
  7.  
  8. /**
  9.  *
  10.  * @author acer
  11.  */
  12. public class ListQueue {
  13.     Node f;
  14.     Node r;
  15.     int size;
  16.    
  17.     public ListQueue(){
  18.         f=null;
  19.         r=null;
  20.         size=0;
  21.     }
  22.    
  23.        
  24. // The number of items in the queue
  25.     public int size(){
  26.         return size;
  27.     }
  28. // Returns true if the queue is empty
  29.     public boolean isEmpty(){
  30.         if(f==null)
  31.             return true;
  32.         return false;
  33.     }
  34. // Adds the new item on the back of the queue, throwing the
  35. // QueueOverflowException if the queue is at maximum capacity. It
  36. // does not throw an exception for an "unbounded" queue, which
  37. // dynamically adjusts capacity as needed.
  38.     public void enqueue(Object obj) throws QueueOverflowException{
  39.         Node n=new Node(obj,null);
  40.         if(f==null){
  41.            
  42.             f=n;
  43.             r=n;
  44.         }
  45.         else{
  46.             r.next=n;
  47.             r=n;
  48.         }
  49.             size++;
  50.     }
  51. // Removes the item in the front of the queue, throwing the
  52. // QueueUnderflowException if the queue is empty.
  53.     public Object dequeue() throws QueueUnderflowException{
  54.         Node rn=null;
  55.         Object val=null;
  56.         if(size==0)
  57.             throw new QueueUnderflowException();
  58.         else if(r==f){
  59.             val=f.element;
  60.             rn=f;
  61.             r=null;
  62.             f=null;
  63.         }
  64.         else{
  65.             val=f.element;
  66.             rn=f;
  67.             f=f.next;
  68.         }
  69.         rn.element=null;
  70.         rn.next=null;
  71.         rn=null;
  72.         size--;
  73.         return val;
  74.     }
  75. // Peeks at the item in the front of the queue, throwing
  76. // QueueUnderflowException if the queue is empty.
  77.     public Object peek() throws QueueUnderflowException{
  78.         if(size==0)
  79.             throw new QueueUnderflowException();
  80.         return f.element;
  81.     }
  82. // Returns a textual representation of items in the queue, in the
  83. // format "[ x y z ]", where x and z are items in the front and
  84. // back of the queue respectively.
  85.     public String toString(){
  86.         String str="";
  87.         for(Node n=f;n!=null;n=n.next){
  88.             str+=n.element+" ";
  89.         }
  90.         return "[ "+str+" ]";
  91.     }
  92. // Returns an array with items in the queue, with the item in the
  93. // front of the queue in the first slot, and back in the last slot.
  94.     public Object[] toArray(){
  95.         Object[]temp=new Object[size()];
  96.         int i=0;
  97.         for(Node n=f;n!=null;n=n.next){
  98.             temp[i]=n.element;
  99.             i++;
  100.         }
  101.         return temp;
  102.     }
  103. // Searches for the given item in the queue, returning the
  104. // offset from the front of the queue if item is found, or -1
  105. // otherwise.
  106.     public int search(Object obj){
  107.         int count=0;
  108.         for(Node n=f;n!=null;n=n.next){
  109.             if(obj.equals(n.element)){
  110.                 return count;
  111.             }
  112.             count++;
  113.         }
  114.         return -1;
  115.     }
  116.    
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement