From d6423ef25ae84d877b95cd5bd517882f0bc5898f Mon Sep 17 00:00:00 2001 From: Divyansh Singh Date: Sun, 18 Jun 2023 00:21:21 -0500 Subject: [PATCH 1/8] Added implementation of Queue using Arrays, LL and JCF --- .DS_Store | Bin 10244 -> 10244 bytes Queue/circularQWithArr.java | 88 ++++++++++++++++++++++++++++++++++++ Queue/qqwithLL.java | 84 ++++++++++++++++++++++++++++++++++ Queue/usingJCF.java | 24 ++++++++++ 4 files changed, 196 insertions(+) create mode 100644 Queue/circularQWithArr.java create mode 100644 Queue/qqwithLL.java create mode 100644 Queue/usingJCF.java diff --git a/.DS_Store b/.DS_Store index e8fab9431267293315553dba5c560fab5de76612..602dc92dbc5e0590e18f051fa55d2e9f05bf4583 100644 GIT binary patch delta 113 zcmZn(XbITxUr2|QA&{YzAr*vk(hY-?^K%Or5CBOwH{WG)pri;llF(#7NpW5zenH0M f4I<)`BSm;O3kh#!-B@yfZ8N*VFP6=XBFxMH^<*3r delta 229 zcmZn(XbITxUug1W5#h~~gtxNR^D~q(mWCC#xLn=cGg91YuLq3q50^}9tq#Fh& z=jRqMAOHa!x%n2 + queue.add(3); //1->2->3 + queue.remove(); //2->3 + queue.add(4); //2->3->4 + while(!queue.isEmpty()) + { + System.out.println(queue.remove()); + } + } +} diff --git a/Queue/qqwithLL.java b/Queue/qqwithLL.java new file mode 100644 index 0000000..bf4495c --- /dev/null +++ b/Queue/qqwithLL.java @@ -0,0 +1,84 @@ +package Queue; + +import java.util.*; + +public class qqwithLL { + static class Node{ + int data; + Node next; + + public Node(int data) + { + this.data = data; + this.next = null; + } + } + static class queue{ + static Node head = null; + static Node tail = null; + //isEmpty() + public static boolean isEmpty() + { + if(head == null && tail == null) + { + return true; + } + return false; + } + + //add - O(1) + public static void add(int data) + { + Node newNode = new Node(data); + if(head == null) + { + head = tail = newNode; + return; + } + tail.next = newNode; + tail = newNode; + } + + //remove - O(1) + public static int remove() + { + if(isEmpty()) + { + System.out.println("Queue is Empty"); + return Integer.MIN_VALUE; + } + int val = head.data; + //single element + if(tail == head) + { + tail=head=null; + } + else{ // if many elements + head = head.next; + } + return val; + } + + //peek - O(1) + public static int peek() + { + if(isEmpty()) + { + System.out.println("Queue is Empty"); + return Integer.MIN_VALUE; + } + return head.data; + } + } + public static void main(String[] args) { + queue queue = new queue(); + queue.add(1); + queue.add(2); + queue.add(3); + queue.add(4); + while(!queue.isEmpty()) + { + System.out.println(queue.remove()); + } + } +} diff --git a/Queue/usingJCF.java b/Queue/usingJCF.java new file mode 100644 index 0000000..660ae07 --- /dev/null +++ b/Queue/usingJCF.java @@ -0,0 +1,24 @@ +package Queue; +import java.util.*; +public class usingJCF { + public static void main(String[] args) { + Queue queue = new LinkedList<>(); + Queue queue1 = new ArrayDeque<>(); + queue.add(1); + queue.add(2); + queue.add(3); + queue.add(4); + while(!queue.isEmpty()) + { + System.out.println(queue.remove()); + } + queue1.add(55); + queue1.add(66); + queue1.add(77); + queue1.add(88); + while(!queue1.isEmpty()) + { + System.out.println(queue1.remove()); + } + } +} From 0304351343ad300e3a245e0cba29337d28c919bd Mon Sep 17 00:00:00 2001 From: Divyansh Singh Date: Tue, 20 Jun 2023 12:31:07 -0500 Subject: [PATCH 2/8] Making stacks and queue's using each other --- Queue/usingTwoQueuePop.java | 108 +++++++++++++++++++++++++++++++++++ Queue/usingTwoStackPop.java | 78 +++++++++++++++++++++++++ Queue/usingTwoStackPush.java | 64 +++++++++++++++++++++ 3 files changed, 250 insertions(+) create mode 100644 Queue/usingTwoQueuePop.java create mode 100644 Queue/usingTwoStackPop.java create mode 100644 Queue/usingTwoStackPush.java diff --git a/Queue/usingTwoQueuePop.java b/Queue/usingTwoQueuePop.java new file mode 100644 index 0000000..816ff70 --- /dev/null +++ b/Queue/usingTwoQueuePop.java @@ -0,0 +1,108 @@ +package Queue; +import java.util.*; +public class usingTwoQueuePop { + //stack using two queue + static class stack + { + static Queue q1 = new ArrayDeque<>(); + static Queue q2 = new ArrayDeque<>(); + //isEmpty + public static boolean isEmpty() + { + return q1.isEmpty() && q2.isEmpty(); + } + //add + public static void add(int data) + { + //add in whichever queue is not empty + if(!q1.isEmpty()) + { + q1.add(data); + } + else{ + q2.add(data); + } + } + public static int removes() + { + if(isEmpty()) + { + System.out.println("Stack is Empty"); + return Integer.MIN_VALUE; + } + int top = -1; + //case 1 + if(!q1.isEmpty()) + { + while(!q1.isEmpty()) //while not empty consider the following + //if empty then top will have the answer + //otherwise add it to the q2 queue. + { + top = q1.remove(); + if(q1.isEmpty()) + { + break; + } + q2.add(top); + } + } + else //if q2 contains all the values + { + while(!q2.isEmpty()) //while not empty consider the following + //if empty then top will have the answer + //otherwise add it to the q1 queue. + { + top = q2.remove(); + if(q2.isEmpty()) + { + break; + } + q1.add(top); + } + } + return top; + } + public static int peek() + { + if(isEmpty()) + { + System.out.println("Stack is Empty"); + return Integer.MIN_VALUE; + } + int top = -1; + //case 1 + if(!q1.isEmpty()) + { + while(!q1.isEmpty()) //while not empty consider the following + //if empty then top will have the answer + //otherwise add it to the q2 queue. + { + top = q1.remove(); + q2.add(top); + } + } + else //if q2 contains all the values + { + while(!q2.isEmpty()) //while not empty consider the following + //if empty then top will have the answer + //otherwise add it to the q1 queue. + { + top = q2.remove(); + q1.add(top); + } + } + return top; + } + public static void main(String[] args) { + stack stack= new stack(); + stack.add(1); + stack.add(2); + stack.add(3); + stack.add(4); + while(!stack.isEmpty()) + { + System.out.println(stack.removes()); + } + } + } +} diff --git a/Queue/usingTwoStackPop.java b/Queue/usingTwoStackPop.java new file mode 100644 index 0000000..fcda807 --- /dev/null +++ b/Queue/usingTwoStackPop.java @@ -0,0 +1,78 @@ +package Queue; + +import java.util.Stack; + +public class usingTwoStackPop { + static class queue + { + static Stack st1 = new Stack<>(); + static Stack st2 = new Stack<>(); + //isEmpty - O(1) + public static boolean isEmpty() + { + return st1.isEmpty(); + } + //add - O(1) + public static void add(int data) + { + st1.push(data); + } + //remove - O(N) + public static int remove() + { + if(isEmpty()) + { + System.out.println("Queue is Empty"); + return Integer.MIN_VALUE; + } + //if stack 1 is not empty put everything in stack 2 + while(!st1.isEmpty()) + { + st2.push(st1.pop()); + } + //remove top element from stack 2 + int removed = st2.peek(); + st2.pop(); + //put all the elements of stack 2 back in stack 1 + while(!st2.isEmpty()) + { + st1.push(st2.pop()); + } + return removed; + } + //peek - O(N) + public static int peek() + { + if(isEmpty()) + { + System.out.println("Queue is Empty"); + return Integer.MIN_VALUE; + } + //if stack 1 is not empty put everything in stack 2 + while(!st1.isEmpty()) + { + st2.push(st1.pop()); + } + //remove top element from stack 2 + int peeked = st2.peek(); + //put all the elements of stack 2 back in stack 1 + while(!st2.isEmpty()) + { + st1.push(st2.pop()); + } + return peeked; + } + } + + public static void main(String[] args) { + queue queue = new queue(); + queue.add(1); + queue.add(2); + queue.add(3); + queue.add(4); + while(!queue.isEmpty()) + { + System.out.println(queue.remove()); + } + } +} diff --git a/Queue/usingTwoStackPush.java b/Queue/usingTwoStackPush.java new file mode 100644 index 0000000..9af5acd --- /dev/null +++ b/Queue/usingTwoStackPush.java @@ -0,0 +1,64 @@ +package Queue; + +import java.util.*; + +public class usingTwoStackPush { + static class queue + { + static Stack st1 = new Stack<>(); + static Stack st2 = new Stack<>(); + //isEmpty + public static boolean isEmpty() + { + return st1.isEmpty(); + } + //add + public static void add(int data) + { + //if stack 1 is not empty put everything in stack 2 + while(!st1.isEmpty()) + { + st2.push(st1.pop()); + } + //put new element in stack 1 + st1.push(data); + //put all the elements of stack 2 back in stack 1 + while(!st2.isEmpty()) + { + st1.push(st2.pop()); + } + } + //remove + public static int remove() + { + if(isEmpty()) + { + System.out.println("Queue is Empty"); + return Integer.MIN_VALUE; + } + int removed = st1.pop(); + return removed; + } + //peek + public static int peek() + { + if(isEmpty()) + { + System.out.println("Queue is Empty"); + } + return st1.peek(); + } + } + + public static void main(String[] args) { + queue queue = new queue(); + queue.add(1); + queue.add(2); + queue.add(3); + queue.add(4); + while(!queue.isEmpty()) + { + System.out.println(queue.remove()); + } + } +} From d32d9ced1d200bd695f621101d8b765bd0d24594 Mon Sep 17 00:00:00 2001 From: Divyansh Singh Date: Wed, 21 Jun 2023 22:54:11 -0500 Subject: [PATCH 3/8] Added more to the queue --- LinkedList/leanLL.java | 4 ++-- Queue/usingTwoQueuePop.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/LinkedList/leanLL.java b/LinkedList/leanLL.java index a4f200b..8519f57 100644 --- a/LinkedList/leanLL.java +++ b/LinkedList/leanLL.java @@ -89,12 +89,12 @@ public void addInMid(int index, int data) //step 1 reach the index before the one you want to add Node temp = head; int i = 0; - while(i Date: Sun, 25 Jun 2023 23:27:04 -0500 Subject: [PATCH 4/8] Non Repeating stream of char --- Queue/streamOfChar.java | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 Queue/streamOfChar.java diff --git a/Queue/streamOfChar.java b/Queue/streamOfChar.java new file mode 100644 index 0000000..dd239b7 --- /dev/null +++ b/Queue/streamOfChar.java @@ -0,0 +1,37 @@ +package Queue; +import java.util.*; +public class streamOfChar { + public static char[] nonRepeatCheck(String a) + { + char[] ans = new char[a.length()]; + int[] freq = new int[26]; + Queue q = new ArrayDeque(); + for(int i=0; i1) //while the peek is >1 keep removing + { + q.poll(); + } + if(q.isEmpty()) //if empty then no repeating char + { + ans[i] = '0'; + } + else{ //else it is the peek of the queue + ans[i] = q.peek(); + } + } + return ans; + } + public static void main(String[] args) { + String a = "aabccxb"; + char[] arr = nonRepeatCheck(a); + for(int i=0; i Date: Mon, 26 Jun 2023 23:24:06 -0500 Subject: [PATCH 5/8] Content completed --- Queue/dequeJCF.java | 21 +++++++++++++++++++++ Queue/interleaveq.java | 28 ++++++++++++++++++++++++++++ Queue/queuereversal.java | 27 +++++++++++++++++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 Queue/dequeJCF.java create mode 100644 Queue/interleaveq.java create mode 100644 Queue/queuereversal.java diff --git a/Queue/dequeJCF.java b/Queue/dequeJCF.java new file mode 100644 index 0000000..f6ad39f --- /dev/null +++ b/Queue/dequeJCF.java @@ -0,0 +1,21 @@ +package Queue; +import java.util.*; +public class dequeJCF { + public static void main(String[] args) { + Deque deque = new LinkedList<>(); + deque.addFirst(1); + deque.addFirst(2); + System.out.println(deque); + deque.removeFirst(); + System.out.println(deque); + deque.addLast(3); + System.out.println(deque); + deque.removeLast(); + System.out.println(deque); + deque.addLast(4); + deque.addFirst(5); + System.out.println(deque); + System.out.println(deque.getFirst()); + System.out.println(deque.getLast()); + } +} diff --git a/Queue/interleaveq.java b/Queue/interleaveq.java new file mode 100644 index 0000000..3c216f8 --- /dev/null +++ b/Queue/interleaveq.java @@ -0,0 +1,28 @@ +package Queue; +import java.util.*; +public class interleaveq { + public static void interleaving(Queue first) + { + Queue second = new ArrayDeque<>(); + int size = first.size()/2; + for(int i=0; i qu = new ArrayDeque<>(); + for(int i=1; i<=10; i++) + { + qu.add(i); + } + interleaving(qu); + System.out.println(qu); + + } +} diff --git a/Queue/queuereversal.java b/Queue/queuereversal.java new file mode 100644 index 0000000..6ee6fe6 --- /dev/null +++ b/Queue/queuereversal.java @@ -0,0 +1,27 @@ +package Queue; + +import java.util.*; + +public class queuereversal { + public static void reverse(Queue qu) + { + Stack st = new Stack<>(); + while(!qu.isEmpty()) + { + st.push(qu.poll()); + } + while(!st.isEmpty()) + { + qu.add(st.pop()); + } + } + public static void main(String[] args) { + Queue qu = new ArrayDeque<>(); + for(int i=1; i<=10; i++) + { + qu.add(i); + } + reverse(qu); + System.out.println(qu); + } +} From 13577a3253d508062dbe68e8a3be9ec233783cc9 Mon Sep 17 00:00:00 2001 From: Divyansh Singh Date: Thu, 29 Jun 2023 12:36:49 -0500 Subject: [PATCH 6/8] Added some comments --- Queue/implementationArr.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Queue/implementationArr.java b/Queue/implementationArr.java index 22516e0..de617fc 100644 --- a/Queue/implementationArr.java +++ b/Queue/implementationArr.java @@ -48,7 +48,7 @@ public static int remove() } //decrement rear rear--; - return front; + return front; //coz front wala element gayab hogaya naa } //peek - O(1) From 094222597e329a0de3caf8ea7fefaccbf02d7786 Mon Sep 17 00:00:00 2001 From: Divyansh Singh Date: Thu, 29 Jun 2023 22:14:59 -0500 Subject: [PATCH 7/8] Practice Question1 --- Queue/generateBinary.java | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Queue/generateBinary.java diff --git a/Queue/generateBinary.java b/Queue/generateBinary.java new file mode 100644 index 0000000..109e2d1 --- /dev/null +++ b/Queue/generateBinary.java @@ -0,0 +1,23 @@ +package Queue; + +import java.util.*; + +public class generateBinary { + public static void binary(int value) + { + Queue qu = new ArrayDeque<>(); + qu.add("1"); + while(value-- >0) + { + String s1 = qu.remove(); + System.out.println(s1); + String s2 = s1; + qu.add(s1+"0"); + qu.add(s2+"1"); + } + } + public static void main(String[] args) { + int value = 10; + binary(value); + } +} From 04867681f504880cb12f9213a639c86aac6e684b Mon Sep 17 00:00:00 2001 From: Divyansh Singh Date: Sun, 2 Jul 2023 13:08:01 -0500 Subject: [PATCH 8/8] Queue practice questions --- Queue/reverseKElem.java | 57 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 Queue/reverseKElem.java diff --git a/Queue/reverseKElem.java b/Queue/reverseKElem.java new file mode 100644 index 0000000..443bff7 --- /dev/null +++ b/Queue/reverseKElem.java @@ -0,0 +1,57 @@ +package Queue; + +import java.util.*; + +public class reverseKElem { + //using stack o(n) + s(qu.size) + public static void reversalStack(Queue qu, int k) + { + Stack st = new Stack<>(); + int remainingS = qu.size()-k; + for(int i=0; i qu, int k) + { + Deque deq = new LinkedList<>(); + for(int i=0; i queue = new LinkedList(); + queue.add(10); + queue.add(20); + queue.add(30); + queue.add(40); + queue.add(50); + queue.add(60); + queue.add(70); + queue.add(80); + queue.add(90); + queue.add(100); + int k = 5; + //reversalStack(queue, k); + reversalDeque(queue, k); + + } +}