Skip to content

Instantly share code, notes, and snippets.

@Ram-1234
Created January 1, 2021 19:52
Show Gist options
  • Save Ram-1234/a6ff857a5175935bfda8975669c8d9f3 to your computer and use it in GitHub Desktop.
Save Ram-1234/a6ff857a5175935bfda8975669c8d9f3 to your computer and use it in GitHub Desktop.
QUEUE OPERATION
### queue operation in java
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Queue
{
LinkedList queue;
public Queue(){
queue=new LinkedList();
}
public boolean isEmpty(){
return queue.isEmpty();
}
public int size(){
return (int) queue.size();
}
public int enqueue(int k){
queue.add(k);
return k;
// else
// System.out.println("Stack overFlow");
}
public int dequeue(){
if((int)queue.size()==0)
return 0;
else
return (int)queue.remove(0);
}
public int top(){
return (int)queue.get(0);
}
public static void main (String[] args) throws java.lang.Exception
{
Queue numberQueue = new Queue();
System.out.println("Size of Queue:"+numberQueue.size());
System.out.println("Queue is empty or not: "+numberQueue.isEmpty());
System.out.println("pushed element is :"+numberQueue.enqueue(5));
//numberStack.push(5);
System.out.println("Size of Queue:"+numberQueue.size());
System.out.println("top of Queue:"+numberQueue.top());
//numberStack.push(8);
System.out.println("Queue is empty or not: "+numberQueue.isEmpty());
System.out.println("pushed element is :"+numberQueue.enqueue(8));
System.out.println("top of Queue:"+numberQueue.top());
System.out.println("Size of Queue:"+numberQueue.size());
System.out.println("dequeue operation performed: "+numberQueue.dequeue());
System.out.println("dequeue operation performed: "+numberQueue.dequeue());
System.out.println("dequeue operation performed: "+numberQueue.dequeue());
System.out.println("Queue is empty or not: "+numberQueue.isEmpty());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment