diff --git a/JAVA/Queue/Deque.java b/JAVA/Queue/Deque.java new file mode 100644 index 00000000..5a64a6b4 --- /dev/null +++ b/JAVA/Queue/Deque.java @@ -0,0 +1,16 @@ +package Ds.Queue; +import java.util.*; +public class Deque { + + public static void main(String[] args) { + ArrayDeque dq = new ArrayDeque(); + dq.addFirst(12); + dq.addFirst(23); + dq.addFirst(42); + dq.pop(); + + System.out.println(dq.peek()); + + } + +} diff --git a/JAVA/Queue/Runner.java b/JAVA/Queue/Runner.java new file mode 100644 index 00000000..5a376d6d --- /dev/null +++ b/JAVA/Queue/Runner.java @@ -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()); + } + +} diff --git a/JAVA/Queue/arrayQueue.java b/JAVA/Queue/arrayQueue.java new file mode 100644 index 00000000..4345db41 --- /dev/null +++ b/JAVA/Queue/arrayQueue.java @@ -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) { + + + } + +} diff --git a/JAVA/Queue/myDeque.java b/JAVA/Queue/myDeque.java new file mode 100644 index 00000000..eca5ee69 --- /dev/null +++ b/JAVA/Queue/myDeque.java @@ -0,0 +1,44 @@ +package Ds.Queue; + +public class myDeque { + Node head , tail; + public void addHead(E data) { + Node 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 toRemove = tail; + tail = tail.next; + tail.previous = null; + + if(tail == null) { + head = null; + } + return toRemove.data; + } + + public static class Node{ + 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 + + } + +} diff --git a/JAVA/Queue/priorityQueue.java b/JAVA/Queue/priorityQueue.java new file mode 100644 index 00000000..5482c608 --- /dev/null +++ b/JAVA/Queue/priorityQueue.java @@ -0,0 +1,50 @@ +package Ds.Queue; + +import java.util.*; + +public class priorityQueue { + + public static void main(String[] args) { +// PriorityQueue 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 pq = new PriorityQueue(); + for(int i = 0;i ans = new ArrayList(pq); + Collections.sort(ans , Collections.reverseOrder()); + + for(int x: ans) { + System.out.print(x+" "); + } + System.out.println(); + } + } + +} diff --git a/JAVA/Queue/queue.java b/JAVA/Queue/queue.java new file mode 100644 index 00000000..0effecdf --- /dev/null +++ b/JAVA/Queue/queue.java @@ -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(); + } + + + +} diff --git a/JAVA/Queue/queueLL.java b/JAVA/Queue/queueLL.java new file mode 100644 index 00000000..2ff74ad8 --- /dev/null +++ b/JAVA/Queue/queueLL.java @@ -0,0 +1,31 @@ +package Ds.Queue; + +import java.util.*; + +import Ds.LinkedList.MyLinkedList.Node; + +public class queueLL { + + private Node head, rear; + public void enqueue(E e) { + Node 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 temp = head; + head= head.next; + if(head==null) { + rear = null; + } + return temp.data; + + } +} diff --git a/JAVA/Queue/queueStack.java b/JAVA/Queue/queueStack.java new file mode 100644 index 00000000..dc9cb739 --- /dev/null +++ b/JAVA/Queue/queueStack.java @@ -0,0 +1,51 @@ +package Ds.Queue; + +import java.util.Stack; + +public class queueStack { + +// public class que{ + Stack s1 = new Stack(); + Stack 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()); + } +} diff --git a/JAVA/Queue/queuesLL2.java b/JAVA/Queue/queuesLL2.java new file mode 100644 index 00000000..86d88a0c --- /dev/null +++ b/JAVA/Queue/queuesLL2.java @@ -0,0 +1,69 @@ +package Ds.Queue; +import java.util.*; + +import Ds.LinkedList.MyLinkedList.Node; +public class queuesLL2 { + + class node{ + public int data; + public Node next; + + node(int val){ + data = val; + next=null; + } + } + class queue{ + Node front; + Node back; + + public queue(){ + front = null; + back = null; + } + void push(int x) { + + Node n = new Node(x); + if(front==null) { + back=n; + front=n; + return; + } + back.next = n; + back=n; + } + void pop() { + if(front==null) { + System.out.println("queue underflow"); + return; + } + Node toDelete = front; + front= front.next; + } + int peek() { + if(front==null) { + System.out.println("No element in queue"); + return -1; + } + return (int) front.data; + } + boolean empty() { + if(front==null) { + return true; + } + return false; + } + + } + public static void main(String[] args) { +// queue q = new queue(); +// q.push(1); +// q.push(2); +// q.push(3); +// +// System.out.println(q.peek()); +// q.pop(); + + } + +} diff --git a/JAVA/Queue/slidingWindow.java b/JAVA/Queue/slidingWindow.java new file mode 100644 index 00000000..ab724add --- /dev/null +++ b/JAVA/Queue/slidingWindow.java @@ -0,0 +1,42 @@ +package Ds.Queue; +import java.util.*; +import java.util.Deque; +//import java.util.concurrent.DelayQueue; +public class slidingWindow { + public static void main(String[] args) { + Solution sol = new Solution(); + int [] a = {4,3,1,2,5,3,4,7,1,9}; + int[] ans = sol.maxSlidingWindow(a, 4); + for(int x: ans) { + System.out.print(x+" "); + } + } + static class Solution{ + public int[] maxSlidingWindow(int[] a , int k) { + int n = a.length; + if(n==0) return a; + Deque dq = new LinkedList (); + int ans[] = new int[n-k+1]; + int i = 0; + for(;i< k;i++) { + while(!dq.isEmpty()&& a[dq.peekLast()] <= a[i] ) { + dq.removeLast(); + } + dq.addLast(i); + } + for(; i