forked from tarunsinghofficial/HacktoberFest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request tarunsinghofficial#1797 from Aryanshaw/main
Queue implementation in java
- Loading branch information
Showing
10 changed files
with
449 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
|
||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
Oops, something went wrong.