├── .idea ├── .gitignore ├── vcs.xml ├── misc.xml ├── modules.xml ├── runConfigurations.xml └── uiDesigner.xml ├── out └── production │ └── Stacks And Queues │ ├── Stack_Implementation │ ├── StackMain.class │ ├── CustomStack.class │ ├── DynamicStack.class │ └── StackException.class │ └── Stack_Questions │ └── Previous_Smaller.class ├── src ├── Stack_Implementation │ ├── StackException.java │ ├── StackMain.java │ ├── DynamicStack.java │ ├── CustomStack.java │ └── Two_Stacks.java └── Stack_Questions │ ├── Delete_Middle_from_Stack.java │ ├── ReverseString.java │ ├── ParenthesisMatching.java │ └── Previous_Smaller.java └── Stacks And Queues.iml /.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | -------------------------------------------------------------------------------- /out/production/Stacks And Queues/Stack_Implementation/StackMain.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/Stacks_and_Queues/HEAD/out/production/Stacks And Queues/Stack_Implementation/StackMain.class -------------------------------------------------------------------------------- /out/production/Stacks And Queues/Stack_Implementation/CustomStack.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/Stacks_and_Queues/HEAD/out/production/Stacks And Queues/Stack_Implementation/CustomStack.class -------------------------------------------------------------------------------- /out/production/Stacks And Queues/Stack_Questions/Previous_Smaller.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/Stacks_and_Queues/HEAD/out/production/Stacks And Queues/Stack_Questions/Previous_Smaller.class -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /out/production/Stacks And Queues/Stack_Implementation/DynamicStack.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/Stacks_and_Queues/HEAD/out/production/Stacks And Queues/Stack_Implementation/DynamicStack.class -------------------------------------------------------------------------------- /out/production/Stacks And Queues/Stack_Implementation/StackException.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/Stacks_and_Queues/HEAD/out/production/Stacks And Queues/Stack_Implementation/StackException.class -------------------------------------------------------------------------------- /src/Stack_Implementation/StackException.java: -------------------------------------------------------------------------------- 1 | package Stack_Implementation; 2 | 3 | public class StackException extends Exception{ 4 | public StackException(String msg){ 5 | super(msg); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 9 | 10 | -------------------------------------------------------------------------------- /Stacks And Queues.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /src/Stack_Implementation/StackMain.java: -------------------------------------------------------------------------------- 1 | package Stack_Implementation; 2 | 3 | public class StackMain { 4 | public static void main(String[] args) throws StackException { 5 | CustomStack stack = new DynamicStack(5); 6 | stack.push(10); 7 | stack.push(20); 8 | stack.push(30); 9 | stack.push(40); 10 | stack.push(50); 11 | stack.push(60); 12 | 13 | // stack.pop(); 14 | // stack.pop(); 15 | // stack.pop(); 16 | // stack.pop(); 17 | // stack.pop(); 18 | // stack.pop(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/Stack_Questions/Delete_Middle_from_Stack.java: -------------------------------------------------------------------------------- 1 | package Stack_Questions; 2 | 3 | import java.util.Stack; 4 | 5 | public class Delete_Middle_from_Stack { 6 | public void deleteMid(Stack stack, int size) { 7 | solve(stack, 0, size); 8 | } 9 | 10 | private void solve(Stack stack, int count, int size) { 11 | if (count == size / 2) { 12 | stack.pop(); 13 | return; 14 | } 15 | int temp = stack.peek(); 16 | stack.pop(); 17 | solve(stack, count + 1, size); 18 | stack.push(temp); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/Stack_Implementation/DynamicStack.java: -------------------------------------------------------------------------------- 1 | package Stack_Implementation; 2 | 3 | // Growable stack using array 4 | public class DynamicStack extends CustomStack { 5 | public DynamicStack() { 6 | super(); 7 | } 8 | 9 | public DynamicStack(int size) { 10 | super(size); 11 | } 12 | 13 | @Override 14 | public boolean push(int item) { 15 | if (this.isFull()) { 16 | int[] temp = new int[data.length * 2]; 17 | for (int i = 0; i < data.length; i++) { 18 | temp[i] = data[i]; 19 | } 20 | data = temp; 21 | } 22 | return super.push(item); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/Stack_Questions/ReverseString.java: -------------------------------------------------------------------------------- 1 | package Stack_Questions; 2 | 3 | import java.util.Stack; 4 | 5 | public class ReverseString { 6 | // reverse a string using stack 7 | public static void main(String[] args) { 8 | String str = "Ayush"; 9 | reverse(str); 10 | } 11 | 12 | static void reverse(String str) { 13 | Stack stack = new Stack<>(); 14 | for (int i = 0; i < str.length(); i++) { 15 | stack.push(str.charAt(i)); 16 | } 17 | String temp = ""; 18 | while (!stack.isEmpty()) { 19 | char ch = stack.peek(); 20 | temp = temp + ch; 21 | stack.pop(); 22 | } 23 | System.out.println(temp); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/Stack_Questions/ParenthesisMatching.java: -------------------------------------------------------------------------------- 1 | package Stack_Questions; 2 | 3 | import java.util.Stack; 4 | 5 | public class ParenthesisMatching { 6 | 7 | public static void main(String[] args) { 8 | String str = "()[]{}" ; 9 | System.out.println(isValid(str)); 10 | } 11 | 12 | public static boolean isValid(String str) { 13 | Stack s = new Stack<>(); 14 | for (int i = 0; i < str.length(); i++) { 15 | char ch = str.charAt(i); 16 | if (isOpening(ch)) { 17 | s.push(ch); 18 | } else { 19 | if (s.isEmpty()) return false; 20 | else if (!isMatch( s.peek(), ch)) return false; 21 | else s.pop(); 22 | } 23 | } 24 | return s.isEmpty(); 25 | } 26 | 27 | private static boolean isOpening(char ch) { 28 | return ch == '(' || ch == '[' || ch == '{'; 29 | } 30 | 31 | private static boolean isMatch(char a, char b) { 32 | return (a == '(' && b == ')') || 33 | (a == '[' && b == ']') || 34 | (a == '{' && b == '}'); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/Stack_Implementation/CustomStack.java: -------------------------------------------------------------------------------- 1 | package Stack_Implementation; 2 | 3 | public class CustomStack { 4 | private static final int DEFAULT_SIZE = 10; 5 | protected int[] data; 6 | int top = -1; 7 | 8 | public CustomStack() { 9 | this(DEFAULT_SIZE); 10 | } 11 | 12 | public CustomStack(int size) { 13 | this.data = new int[size]; 14 | } 15 | 16 | public boolean isFull() { 17 | return top == data.length - 1; // ptr is at last index 18 | } 19 | 20 | public boolean isEmpty() { 21 | return top == -1; 22 | } 23 | 24 | public int size() { 25 | return top + 1; 26 | } 27 | 28 | public boolean push(int item) { 29 | if (isFull()) { 30 | System.out.println("Cannot push Stack is full !!"); 31 | } 32 | top++; 33 | data[top] = item; 34 | return true; 35 | } 36 | 37 | public int pop() throws StackException { 38 | if (isEmpty()) { 39 | throw new StackException("Connot pop from an empty stack!!"); 40 | } 41 | return data[top--]; 42 | } 43 | 44 | public int peek() throws StackException { 45 | if (isEmpty()) { 46 | throw new StackException("Cannot peek from an empty stack"); 47 | } 48 | return data[top]; 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /src/Stack_Implementation/Two_Stacks.java: -------------------------------------------------------------------------------- 1 | package Stack_Implementation; 2 | 3 | public class Two_Stacks { 4 | // question from code studio - 5 | // https://www.codingninjas.com/codestudio/problems/two-stacks_983634?leftPanelTab=1 6 | // create two stacks using single array 7 | int[] data; 8 | int size; 9 | int top1; 10 | int top2; 11 | 12 | // Initialize TwoStack. 13 | public Two_Stacks(int s) { 14 | this.size = s; 15 | this.data = new int[s]; 16 | top1 = -1; 17 | top2 = s; 18 | } 19 | 20 | // push from front of the array in stack 1 21 | public void push1(int item) { 22 | if (top2 - top1 > 1) { 23 | data[top1++] = item; 24 | } else { 25 | System.out.println("stack is full!!"); 26 | } 27 | } 28 | 29 | // push from backside of the array in stack 2 30 | public void push2(int item) { 31 | if (top2 - top1 > 1) { 32 | data[top2--] = item; 33 | } else { 34 | System.out.println("stack is full!!"); 35 | } 36 | } 37 | 38 | // pop from front of the array from stack 1 39 | public int pop1() { 40 | if (top1 > -1) return data[top1--]; 41 | else return -1; 42 | } 43 | 44 | // pop from backside of the array from stack 2 45 | public int pop2() { 46 | if (top2 < size) return data[top2++]; 47 | else return -1; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/Stack_Questions/Previous_Smaller.java: -------------------------------------------------------------------------------- 1 | package Stack_Questions; 2 | 3 | import java.util.Arrays; 4 | import java.util.Stack; 5 | 6 | //contains four sub-questions 7 | public class Previous_Smaller { 8 | public static void main(String[] args) { 9 | int[] arr = {4, 10, 5, 8, 20, 15, 3, 12}; 10 | previousSmaller(arr); 11 | System.out.println(); 12 | previousGreater(arr); 13 | System.out.println(); 14 | int[] arr2 = {3, 10, 5, 1, 15, 10, 7, 6}; 15 | System.out.println(Arrays.toString(nextGreater(arr2))); 16 | System.out.println(Arrays.toString(nextSmaller(arr2))); 17 | } 18 | 19 | //Q1. code for previous smaller element in an array 20 | static void previousSmaller(int[] arr) { 21 | Stack stack = new Stack<>(); 22 | for (int i = 0; i < arr.length; i++) { 23 | while (!stack.isEmpty() && stack.peek() >= arr[i]) { 24 | stack.pop(); 25 | } 26 | if (stack.isEmpty()) System.out.print("-1 "); 27 | else System.out.print(stack.peek() + " "); 28 | stack.push(arr[i]); 29 | } 30 | } 31 | 32 | //Q2. code for previous greater element in an array 33 | static void previousGreater(int[] arr) { 34 | Stack stack = new Stack<>(); 35 | for (int i = 0; i < arr.length; i++) { 36 | while (!stack.isEmpty() && stack.peek() <= arr[i]) { 37 | stack.pop(); 38 | } 39 | if (stack.isEmpty()) System.out.print("-1 "); 40 | else System.out.print(stack.peek() + " "); 41 | stack.push(arr[i]); 42 | } 43 | } 44 | 45 | //Q3. code for next smaller element in an array 46 | static int[] nextSmaller(int[] arr) { 47 | Stack stack = new Stack<>(); 48 | int[] temp = new int[arr.length]; 49 | for (int i = arr.length - 1; i >= 0; i--) { 50 | while (!stack.isEmpty() && stack.peek() >= arr[i]) { 51 | stack.pop(); 52 | } 53 | if (stack.isEmpty()) temp[i] = -1; 54 | else temp[i] = stack.peek(); 55 | stack.push(arr[i]); 56 | } 57 | return temp; 58 | } 59 | 60 | //Q4. code for next greater element in an array 61 | static int[] nextGreater(int[] arr) { 62 | Stack stack = new Stack<>(); 63 | int[] temp = new int[arr.length]; 64 | for (int i = arr.length - 1; i >= 0; i--) { 65 | while (!stack.isEmpty() && stack.peek() <= arr[i]) { 66 | stack.pop(); 67 | } 68 | if (stack.isEmpty()) temp[i] = -1; 69 | else temp[i] = stack.peek(); 70 | stack.push(arr[i]); 71 | } 72 | return temp; 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /.idea/uiDesigner.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | --------------------------------------------------------------------------------