Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Queue implementation using Array in java

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 510
    Comment on it

    Queue is a kind of Abstract data structure that holds items in same order. Queue follow FIFO mechanism for operations like insertion and deletion of items.

    Note :

    - One end is always used to insert data called enqueue.

    - Other end is always used to delete data called dequeue.

    - So Adding an item to the rear and deleting an item from front of queue.

    Time complexity always constant that is O(1).

    Implement queue using array :

    public class QueueOperations  {
    
        private int storageCapacity;
        private int[] queueArr;
        private int rear = -1;
        private int front = 0;
        private int size = 0;
    
        QueueOperations(int capacity){
            storageCapacity = capacity;
            queueArr = new int[storageCapacity];
        }
    
        public void addItem(int item){
    
            if (size == storageCapacity){
                Log.e(" overflow ::" ," queue overflow condition .");
            }else {
                rear++;
                if (rear == size - 1) {
                    rear = 0;
                    front = 0;
                }
                queueArr[rear] = item;
                size++;
            }
        }
    
        public void removeItem(){
    
            if (size == 0){
                Log.e(" underflow ::" ," queue underflow condition .");
            }else{
    
                front++;
                if (front == rear){
                    front = 0;
                    rear = 0;
                }
                size--;
            }
        }
    
        public void peek(){
    
            if (size == -1){
                Log.e(" queue empty"," queue empty");
            }else{
                Log.e(" queue size"," :: " + size);
                Log.e(" queue front at "," :: " + front);
                Log.e(" queue rear at"," :: " + rear);
                Log.e(" ----------------------- ","--------------- ");
    
                for (int i =front ; i<=rear;i++){
                    Log.e(" queue data"," :: " + queueArr[i]);
    
                }
            }
        }
    
    }

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: