Skip to content

Commit

Permalink
Merge pull request tarunsinghofficial#1797 from Aryanshaw/main
Browse files Browse the repository at this point in the history
Queue implementation in java
  • Loading branch information
tarunsinghofficial authored Oct 20, 2021
2 parents 59e8dbe + a9ecb9e commit 4418b0a
Show file tree
Hide file tree
Showing 10 changed files with 449 additions and 0 deletions.
16 changes: 16 additions & 0 deletions Java_Programs_for_beginners/Queue/Deque.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package Ds.Queue;
import java.util.*;
public class Deque {

public static void main(String[] args) {
ArrayDeque<Integer> dq = new ArrayDeque();
dq.addFirst(12);
dq.addFirst(23);
dq.addFirst(42);
dq.pop();

System.out.println(dq.peek());

}

}
20 changes: 20 additions & 0 deletions Java_Programs_for_beginners/Queue/Runner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package Ds.Queue;

public class Runner {

public static void main(String[] args) {
arrayQueue q = new arrayQueue();
q.enQueue(5);
q.enQueue(4);
q.enQueue(3);
q.enQueue(7);
q.enQueue(6);
q.enQueue(1);
// q.enQueue(0);

q.deQueue();
q.show();
System.out.println("size is "+ q.getSize());
}

}
50 changes: 50 additions & 0 deletions Java_Programs_for_beginners/Queue/arrayQueue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package Ds.Queue;

public class arrayQueue {

int queue[] = new int [5];
int size;
int front;
int back;

public void enQueue(int data) {
if(!isFull()) {
queue[back] = data;
back = (back +1)%5;
size++;
}else {
System.out.println("Queue is full");
}
}
public int deQueue() {
int data = queue[front];
if(isEmpty()) {
front= (front + 1)%5;
size--;
}else {
System.out.println("queue is empty");
}
return data;
}

public void show() {
System.out.println("The number of elements are: ");
for(int i=0; i < size; i++) {
System.out.println(queue[(front+ i)%5]+ " ");
}
}
public int getSize() {
return size;
}
public boolean isEmpty() {
return getSize()==0;
}
public boolean isFull() {
return getSize()==5;
}
public static void main(String[] args) {


}

}
44 changes: 44 additions & 0 deletions Java_Programs_for_beginners/Queue/myDeque.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package Ds.Queue;

public class myDeque<E> {
Node head , tail;
public void addHead(E data) {
Node <E> toAdd = new Node(data);
if(head==null) {
head = tail = toAdd;
return;
}
head.next = toAdd;
toAdd.previous = head;
toAdd= head;

}
public E removeLast() {
if(head==null) {
return null;
}
Node<E> toRemove = tail;
tail = tail.next;
tail.previous = null;

if(tail == null) {
head = null;
}
return toRemove.data;
}

public static class Node<E>{
E data;
Node next , previous;

public Node(E data) {
this.data = data;
this.next = this.previous = null;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub

}

}
50 changes: 50 additions & 0 deletions Java_Programs_for_beginners/Queue/priorityQueue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package Ds.Queue;

import java.util.*;

public class priorityQueue {

public static void main(String[] args) {
// PriorityQueue <String> pq = new PriorityQueue();
// pq.add("orange");
// pq.add("apple");
// pq.add("hulalal");
// pq.add("hikli");
// pq.add("babul");
//
// System.out.println(pq);
// System.out.println(pq.remove());
// System.out.println(pq.remove());
// System.out.println(pq.remove());
//
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-- !=0) {
int n = sc.nextInt();
int k = sc.nextInt();
int a[] = new int [n];
for(int i = 0;i<n ; i++) {
a[i] = sc.nextInt();
}
PriorityQueue <Integer> pq = new PriorityQueue();
for(int i = 0;i<n ; i++) {
if(i<k) {
pq.add(a[i]);
}else {
if(pq.peek() < a[i]) {
pq.poll();
pq.add(a[i]);
}
}
}
ArrayList <Integer> ans = new ArrayList(pq);
Collections.sort(ans , Collections.reverseOrder());

for(int x: ans) {
System.out.print(x+" ");
}
System.out.println();
}
}

}
76 changes: 76 additions & 0 deletions Java_Programs_for_beginners/Queue/queue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package Ds.Queue;

import java.lang.reflect.Array;
import java.util.ArrayList;

public class queue {
private static final int n = 20;

int [] arr;
int front;
int back;
int size;

public queue () {
arr = new int [n];
front = -1;
back = -1;
}

void push(int x) {
if(back == n-1) {
System.out.println("Queue overflow");
return;
}
back++;
arr[back]= x;
size++;

if(front == -1) {
front++;
}
}
void pop() {
if(front == -1 || front> back) {
System.out.println("No elements in queue");
return;
}
size--;
front++;

}
public void show() {
System.out.println("The number of elements are: ");
for(int i=0; i < size; i++) {
System.out.println(arr[front+ i]+ " ");
}
}
int peek() {
if(front == -1 || front> back) {
System.out.println("No elements in queue");
return -1;
}
return arr[front];
}
boolean empty() {
if(front == -1 || front> back) {
return true;
}
return false;
}
public static void main (String [] args) {
queue q = new queue();
q.push(1);
q.push(2);
q.push(3);
q.push(4);

q.pop();
q.show();
// System.out.println(q.peek());
// q.pop();
}



}
31 changes: 31 additions & 0 deletions Java_Programs_for_beginners/Queue/queueLL.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package Ds.Queue;

import java.util.*;

import Ds.LinkedList.MyLinkedList.Node;

public class queueLL<E> {

private Node<E> head, rear;
public void enqueue(E e) {
Node<E> toAdd = new Node(e);
if(head==null) {
head=rear= toAdd;
return;
}
rear.next = toAdd;
rear = rear.next;
}
public E dequeue(E e) {
if(head==null) {
return null;
}
Node<E> temp = head;
head= head.next;
if(head==null) {
rear = null;
}
return temp.data;

}
}
51 changes: 51 additions & 0 deletions Java_Programs_for_beginners/Queue/queueStack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package Ds.Queue;

import java.util.Stack;

public class queueStack {

// public class que{
Stack<Integer> s1 = new Stack();
Stack<Integer> s2 = new Stack();

public void push(int x) {
s1.push(x);
}
public int pop() {
if(s1.empty() && s2.empty()) {
System.out.println("Queue is empty");
return -1;
}
if(s2.empty()) {
while(!s1.empty()) {
s2.push(s1.peek());
s1.pop();
}
}
int top = s2.peek();
s2.pop();
return top;
}
boolean isEmpty() {
if(s1.empty() && s2.empty()) {
return true;
}
return false;
}
// };
public static void main(String[] args) {
queueStack q = new queueStack();
// que i = new que();
q.push(1);
q.push(2);
q.push(3);
q.push(4);

System.out.println(q.pop());
System.out.println(q.pop());
System.out.println(q.pop());
System.out.println(q.pop());

System.out.println(q.pop());
}
}
Loading

0 comments on commit 4418b0a

Please sign in to comment.