diff --git a/Exercise_1.java b/Exercise_1.java index 314a3cb45..2a7fcd769 100644 --- a/Exercise_1.java +++ b/Exercise_1.java @@ -1,36 +1,56 @@ -class Stack { +class Stack { //Please read sample.java file before starting. - //Kindly include Time and Space complexity at top of each file - static final int MAX = 1000; - int top; - int a[] = new int[MAX]; // Maximum size of Stack - - boolean isEmpty() - { - //Write your code here - } + //Kindly include Time and Space complexity at top of each file + // Time Complexity : + // Push (O(1)), Pop(O(1)), Peek(O(1)) + // Space Complexity : + // O(1), No additional space used, just the array to keep the values + // Did this code successfully run on Leetcode : + // Didn't find exact problem on leetcode + // Any problem you faced while coding this : + // No + // Approach : + // Insert value to/remove value from the end of the array using top as pointer. - Stack() - { - //Initialize your constructor - } - - boolean push(int x) - { + static final int MAX = 1000; + int top; + int[] a = new int[MAX]; // Maximum size of Stack + + boolean isEmpty() { + return top == 0; + } + + Stack() { + //Initialize your constructor + } + + boolean push(int x) { //Check for stack Overflow //Write your code here - } - - int pop() - { + if (top >= MAX) { + return false; + } + a[top++] = x; + return true; + } + + int pop() { //If empty return 0 and print " Stack Underflow" //Write your code here - } - - int peek() - { + if (top == 0) { + System.out.println("Stack Underflow"); + return 0; + } + int topMost = a[top - 1]; + top--; + return topMost; + } + + int peek() { //Write your code here - } + if (top == 0 || top == MAX) return 0; + return a[top - 1]; + } } // Driver code @@ -41,6 +61,6 @@ public static void main(String args[]) s.push(10); s.push(20); s.push(30); - System.out.println(s.pop() + " Popped from stack"); - } + System.out.println(s.pop() + " Popped from stack"); + } } diff --git a/Exercise_2.java b/Exercise_2.java index 5a9c4868c..d4f47142e 100644 --- a/Exercise_2.java +++ b/Exercise_2.java @@ -1,52 +1,80 @@ -public class StackAsLinkedList { - - StackNode root; - - static class StackNode { - int data; - StackNode next; - - StackNode(int data) - { - //Constructor here - } - } - - - public boolean isEmpty() - { - //Write your code here for the condition if stack is empty. - } - - public void push(int data) - { - //Write code to push data to the stack. - } - - public int pop() - { - //If Stack Empty Return 0 and print "Stack Underflow" - //Write code to pop the topmost element of stack. - //Also return the popped element - } - - public int peek() - { - //Write code to just return the topmost element without removing it. - } - - //Driver code - public static void main(String[] args) - { - - StackAsLinkedList sll = new StackAsLinkedList(); - - sll.push(10); - sll.push(20); - sll.push(30); - - System.out.println(sll.pop() + " popped from stack"); - - System.out.println("Top element is " + sll.peek()); - } -} +class StackAsLinkedList { + + // Time Complexity : + // Push (O(1)), Pop (O(1)) + // Space Complexity : + // Push (O(1)), Pop (O(1)), No additional space needed + // Did this code successfully run on Leetcode : + // Yes + // Any problem you faced while coding this : + // Didn't find exact problem on leetcode + // Approach : + // Keep reference of root node as the top of the stack. Create new node and make it the new top/root node. + StackNode root; + + static class StackNode { + int data; + StackNode next; + + StackNode(int data) + { + this.data = data; + this.next = null; + } + } + + + public boolean isEmpty() + { + return root == null; + } + + public void push(int data) + { + StackNode newNode = new StackNode(data); + if(root == null){ + root = newNode; + return ; + } + newNode.next = root; + root = newNode; + } + + public int pop() + { //If Stack Empty Return 0 and print "Stack Underflow" + //Write code to pop the topmost element of stack. + //Also return the popped element + if(root == null){ + System.out.println("Stack Underflow"); + return 0; + } + StackNode top = root; + root = root.next; + return top.data; + } + + public int peek() + { + if(root == null) { + System.out.println("Stack is Empty"); + return -1; + } + + return root.data; + } + + //Driver code + public static void main(String[] args) + { + + StackAsLinkedList sll = new StackAsLinkedList(); + + sll.push(10); + sll.push(20); + sll.push(30); + + System.out.println(sll.pop() + " popped from stack"); + + System.out.println("Top element is " + sll.peek()); + } +} diff --git a/Exercise_3.java b/Exercise_3.java index fb66d329d..cb3296682 100644 --- a/Exercise_3.java +++ b/Exercise_3.java @@ -1,50 +1,66 @@ import java.io.*; // Java program to implement -// a Singly Linked List -public class LinkedList { - +// a Singly Linked List + +// Time Complexity : +// Insert - O(1) +// Space Complexity : +// O(1) +// Did this code successfully run on Leetcode : +// Didn't find exact problem on leetcode +// Any problem you faced while coding this : +// No +class LinkedList { + Node head; // head of list - + Node tail; // Linked list Node. // This inner class is made static // so that main() can access it - static class Node { - - int data; - Node next; - + static class Node { + + int data; + Node next; + // Constructor - Node(int d) - { - //Write your code here - } - } - - // Method to insert a new node - public static LinkedList insert(LinkedList list, int data) - { - // Create a new node with given data - - // If the Linked List is empty, - // then make the new node as head - - // Else traverse till the last node - // and insert the new_node there + Node(int d) + { + this.data = d; + this.next = null; + } + } - // Insert the new_node at last node - // Return the list by head - - } - - // Method to print the LinkedList. - public static void printList(LinkedList list) - { - // Traverse through the LinkedList - - // Print the data at current node - - // Go to next node + // Method to insert a new node + public static LinkedList insert(LinkedList list, int data) + { + // Create a new node with given data + Node pointer = list.head; + Node newNode = new Node(data); + // If the Linked List is empty, + // then make the new node as head + if(pointer == null){ + list.head = newNode; + list.tail = newNode; + return list; + } + // Else insert the new node at the tail end of linked list + pointer = list.tail; + pointer.next = newNode; + list.tail = newNode; + return list; + } + + // Method to print the LinkedList. + public static void printList(LinkedList list) + { // Traverse through the LinkedList + // Print the data at current node + // Go to next node + Node pointer = list.head; + while(pointer != null){ + System.out.print(pointer.data+"\t"); + pointer = pointer.next; + } } // Driver code @@ -63,7 +79,7 @@ public static void main(String[] args) list = insert(list, 3); list = insert(list, 4); list = insert(list, 5); - + // Print the LinkedList printList(list); }