Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified .DS_Store
Binary file not shown.
4 changes: 2 additions & 2 deletions LinkedList/leanLL.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<index-1)
while(i<index-1) //at index-2 it will actually be at index-1 due to temp.next getting executed afterwards
{
temp = temp.next;
i++;
}

// By this time temp is at second last.

//Step3 - point new node to the index of temp.next
newNode.next = temp.next;
Expand Down
88 changes: 88 additions & 0 deletions Queue/circularQWithArr.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package Queue;

public class circularQWithArr {
static class Qu{
static int arr[];
static int size;
static int rear;
static int front; //keep track of front as well

Qu(int sizes){
this.arr = new int[sizes];
this.size = sizes;
this.rear = -1;
this.front = -1;
}

//isEmpty()
public static boolean isEmpty()
{
if(rear == -1 && front == -1)
{
System.out.println("Empty Queue");
return true;
}
return false;
}

//isFull
public static boolean isFull()
{
if((rear+1)%size == front) return true;
return false;
}

//add - O(1)
public static void add(int data)
{
if(isFull()) System.out.println("Queue is Full");
if(front == -1) front = 0; // For the very first element; we have moved the rear but we also have to bring front to 0 as it is -1;
rear = (rear+1)%size; //pehle rear ko update then add data
arr[rear] = data;
}

//remove - O(n)
public static int remove()
{
if(isEmpty())
{
System.out.println("Empty Queue");
return -1;
}
int removed = arr[front];
//if removing the very last element then make sure everything is reset
if(front == rear)
{
front = rear = -1;
}
else //just move the font one step ahead
{
front = (front+1)%size;
}
return removed;
}

//peek - O(1)
public static int peek()
{
if(isEmpty())
{
System.out.println("Queue is Empty");
return -1;
}
return arr[front];
}
}
public static void main(String[] args) {
Qu queue = new Qu(3);
queue.add(1); //1
queue.add(2); //1->2
queue.add(3); //1->2->3
queue.remove(); //2->3
queue.add(4); //2->3->4
while(!queue.isEmpty())
{
System.out.println(queue.remove());
}
}
}
21 changes: 21 additions & 0 deletions Queue/dequeJCF.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package Queue;
import java.util.*;
public class dequeJCF {
public static void main(String[] args) {
Deque<Integer> 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());
}
}
23 changes: 23 additions & 0 deletions Queue/generateBinary.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package Queue;

import java.util.*;

public class generateBinary {
public static void binary(int value)
{
Queue<String> 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);
}
}
2 changes: 1 addition & 1 deletion Queue/implementationArr.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
28 changes: 28 additions & 0 deletions Queue/interleaveq.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package Queue;
import java.util.*;
public class interleaveq {
public static void interleaving(Queue<Integer> first)
{
Queue<Integer> second = new ArrayDeque<>();
int size = first.size()/2;
for(int i=0; i<size; i++) // putting first half of queue in second
{
second.add(first.poll());
}
while(!second.isEmpty()) // adding the values of second in first then first in first.
{
first.add(second.poll());
first.add(first.poll());
}
}
public static void main(String[] args) {
Queue<Integer> qu = new ArrayDeque<>();
for(int i=1; i<=10; i++)
{
qu.add(i);
}
interleaving(qu);
System.out.println(qu);

}
}
84 changes: 84 additions & 0 deletions Queue/qqwithLL.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
}
27 changes: 27 additions & 0 deletions Queue/queuereversal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package Queue;

import java.util.*;

public class queuereversal {
public static void reverse(Queue<Integer> qu)
{
Stack<Integer> st = new Stack<>();
while(!qu.isEmpty())
{
st.push(qu.poll());
}
while(!st.isEmpty())
{
qu.add(st.pop());
}
}
public static void main(String[] args) {
Queue<Integer> qu = new ArrayDeque<>();
for(int i=1; i<=10; i++)
{
qu.add(i);
}
reverse(qu);
System.out.println(qu);
}
}
57 changes: 57 additions & 0 deletions Queue/reverseKElem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package Queue;

import java.util.*;

public class reverseKElem {
//using stack o(n) + s(qu.size)
public static void reversalStack(Queue<Integer> qu, int k)
{
Stack<Integer> st = new Stack<>();
int remainingS = qu.size()-k;
for(int i=0; i<k; i++) //put the first k element in stack
{
st.push(qu.poll());
}
while(!st.isEmpty()) // remove from stack which will reverse them and then push it onto the queue
{ //[60, 70, 80, 90, 100, 50, 40, 30, 20, 10]
qu.add(st.pop());
}
for(int i =0; i<remainingS; i++) //remove from start of the queue and put it back in the queue for the remaining elements
{
qu.add(qu.poll());
}
System.out.println(qu);
}

//using deque o(n) + s(qu.size)
public static void reversalDeque(Queue<Integer> qu, int k)
{
Deque<Integer> deq = new LinkedList<>();
for(int i=0; i<k; i++) //used add first method to put first k elements in reverse order in deq
{
deq.addFirst(qu.poll());
}
while(!qu.isEmpty())
{
deq.addLast(qu.poll());//used add last method to put the remaining elements in the natural order
}
System.out.println(deq);
}
public static void main(String[] args) {
Queue<Integer> queue = new LinkedList<Integer>();
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);

}
}
37 changes: 37 additions & 0 deletions Queue/streamOfChar.java
Original file line number Diff line number Diff line change
@@ -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<Character> q = new ArrayDeque<Character>();
for(int i=0; i<a.length(); i++)
{
char ch = a.charAt(i);
q.add(ch); //add in the queue
freq[ch-'a']++; //add the frequency
while(!q.isEmpty() && freq[q.peek()-'a']>1) //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<arr.length; i++)
{
System.out.print(arr[i]+" ");
}
System.out.println();
}
}
Loading