Queue data structure implementation in java

What is Queue?

Queues are dynamic collections which have some concept of order. This can be either based on order of entry into the queue - giving us First-In-First-Out (FIFO) or Last-In-First-Out (LIFO) queues. Both of these can be built with linked lists: the simplest "add-to-head" implementation of a linked list gives LIFO behaviour. A minor modification - adding a tail pointer and adjusting the addition method implementation - will produce a FIFO queue.

Queue Implementation

package queue

import java.util.Scanner;

public class Queue {

int[] arr = new int[10];
int a = -1;
int b=0;
public void enqueue(int i){

if(a==9){

System.out.println("Queue is full");

}

else
arr[++a]=i;

}
public void dequeue(){

if(a>b){

for(b=0; b

arr[b]=arr[b+1];

}
a--;
b=0;
}

}

public void display(){

for(int i=0; i<=9; i++){

System.out.println("values at index "+i+":"+arr[i]);

}

}

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
Queue q = new Queue();
int var = -1;
while( var !=0 ){

System.out.println("1. Insert item in queue");
System.out.println("2. Delete item from queue");
System.out.println("3. Display Queue");
System.out.println("0. Exit");
var = sc.nextInt();
switch(var){

case 1:
System.out.print("Enter item in Queue: ");
int pushItem = sc.nextInt();
q.enqueue(pushItem);
break;
case 2:

q.dequeue();
break;
case 3:
q.display();
break;
}
}

}
}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>