diff --git a/Exercise_1.java b/Exercise_1.java index 314a3cb45..819c284ad 100644 --- a/Exercise_1.java +++ b/Exercise_1.java @@ -1,3 +1,11 @@ +// Time Complexity : O(1) +// Space Complexity : O(n) +// Did this code successfully run on Leetcode : yes - implement stack using queues +// Any problem you faced while coding this : + + +// Your code here along with comments explaining your approach + class Stack { //Please read sample.java file before starting. //Kindly include Time and Space complexity at top of each file @@ -8,28 +16,53 @@ class Stack { boolean isEmpty() { //Write your code here + if(a.length == 0) + { + return true; + } + return false; } Stack() { //Initialize your constructor + top = -1; } boolean push(int x) { //Check for stack Overflow //Write your code here + if(top == MAX - 1) + { + System.out.println("Stack overflow"); + return false; + } + a[++top] = x; + return true; } int pop() { //If empty return 0 and print " Stack Underflow" //Write your code here + if(isEmpty()) + { + System.out.println("Stack underflow"); + return 0; + } + return a[top--]; } int peek() { //Write your code here + if(isEmpty()) + { + System.out.println("Stack is empty"); + return -1; + } + return a[top]; } } diff --git a/Exercise_2.java b/Exercise_2.java deleted file mode 100644 index 5a9c4868c..000000000 --- a/Exercise_2.java +++ /dev/null @@ -1,52 +0,0 @@ -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()); - } -} diff --git a/Exercise_3.java b/LinkedList.java similarity index 57% rename from Exercise_3.java rename to LinkedList.java index fb66d329d..01ed33c2b 100644 --- a/Exercise_3.java +++ b/LinkedList.java @@ -1,3 +1,11 @@ +// Time Complexity :O(1) for insert and O(n) for iterate +// Space Complexity : O(n) +// Did this code successfully run on Leetcode : +// Any problem you faced while coding this : + + +// Your code here along with comments explaining your approach + import java.io.*; // Java program to implement @@ -5,6 +13,13 @@ public class LinkedList { Node head; // head of list + int size; + + LinkedList() + { + this.head = null; + this.size = 0; + } // Linked list Node. // This inner class is made static @@ -18,6 +33,8 @@ static class Node { Node(int d) { //Write your code here + this.data = d; + this.next = null; } } @@ -25,6 +42,7 @@ static class Node { public static LinkedList insert(LinkedList list, int data) { // Create a new node with given data + Node newNode = new Node(data); // If the Linked List is empty, // then make the new node as head @@ -34,7 +52,21 @@ public static LinkedList insert(LinkedList list, int data) // Insert the new_node at last node // Return the list by head - + + if(list.size == 0) + { + list.head = newNode; + list.size++; + return list; + } + Node curr = list.head; + while(curr.next != null) + { + curr = curr.next; + } + curr.next = newNode; + list.size++; + return list; } // Method to print the LinkedList. @@ -44,7 +76,18 @@ public static void printList(LinkedList list) // Print the data at current node - // Go to next node + // Go to next node + if(list.size == 0) + { + System.out.println("List is empty"); + return; + } + Node curr = list.head; + while(curr != null) + { + System.out.println(curr.data); + curr = curr.next; + } } // Driver code diff --git a/StackAsLinkedList.java b/StackAsLinkedList.java new file mode 100644 index 000000000..f8a6a7991 --- /dev/null +++ b/StackAsLinkedList.java @@ -0,0 +1,102 @@ +// Time Complexity : O(1) for push, isEmpty, and peek and O(n) for pop +// Space Complexity : O(n) +// Did this code successfully run on Leetcode :yes - implement stack using queues +// Any problem you faced while coding this : + + +// Your code here along with comments explaining your approach +public class StackAsLinkedList { + + StackNode root; + StackNode top; + + StackAsLinkedList() + { + this.root = null; + this.top = null; + } + + static class StackNode { + int data; + StackNode next; + + StackNode(int data) + { + //Constructor here + this.data = data; + next = null; + } + } + + + public boolean isEmpty() + { + //Write your code here for the condition if stack is empty. + return root == null; + } + + public void push(int data) + { + //Write code to push data to the stack. + StackNode newNode = new StackNode(data); + if(null == root) + { + root = newNode; + top = newNode; + } + else + { + top.next = newNode; + top = 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(isEmpty()) + { + System.out.println("Stack Underflow"); + return 0; + } + int topElement = top.data; + StackNode curr = root; + while(curr.next != top) + { + curr = curr.next; + } + top = curr; + top.next = null; + + return topElement; + } + + public int peek() + { + //Write code to just return the topmost element without removing it. + if(isEmpty()) + { + System.out.println("Stack is empty"); + return -1; + } + return top.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()); + } +}