diff --git a/Exercise_1.java b/Exercise_1.java index 314a3cb45..eb1f9b5fd 100644 --- a/Exercise_1.java +++ b/Exercise_1.java @@ -1,35 +1,54 @@ class Stack { //Please read sample.java file before starting. //Kindly include Time and Space complexity at top of each file + +// Time Complexity :O(1) for all operations +// Space Complexity : O(n) for storing elements in array +// Did this code successfully run on Leetcode : NA + static final int MAX = 1000; int top; int a[] = new int[MAX]; // Maximum size of Stack boolean isEmpty() { - //Write your code here + return top == -1; } Stack() { //Initialize your constructor + this.top = -1; } boolean push(int x) { - //Check for stack Overflow - //Write your code here + if(top == a.length -1){ + return false; + } + else{ + a[++top] = x; + return true; + } + } int pop() { //If empty return 0 and print " Stack Underflow" + if(top == -1){ + System.out.println("Stack Underflow"); + return 0; + } + return a[top--]; //Write your code here } int peek() { //Write your code here + if(top == -1) return -1; + return a[top]; } } diff --git a/Exercise_2.java b/Exercise_2.java index 5a9c4868c..0f631eaab 100644 --- a/Exercise_2.java +++ b/Exercise_2.java @@ -1,4 +1,7 @@ -public class StackAsLinkedList { +//TC - O(1) for all operations +//SC - O(n) for storing elements in linked list + +public class Exercise_2 { StackNode root; @@ -8,7 +11,7 @@ static class StackNode { StackNode(int data) { - //Constructor here + this.data = data; } } @@ -16,30 +19,46 @@ static class StackNode { 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); + newNode.next = root; + root = newNode; } public int pop() { //If Stack Empty Return 0 and print "Stack Underflow" + if(root == null){ + System.out.println("Stack Underflow"); + return 0; + } //Write code to pop the topmost element of stack. - //Also return the popped element + //Also return the popped element + int val = root.data; + root = root.next; + return val; } public int peek() { //Write code to just return the topmost element without removing it. + 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(); + Exercise_2 sll = new Exercise_2(); sll.push(10); sll.push(20); diff --git a/Exercise_3.class b/Exercise_3.class new file mode 100644 index 000000000..15d9a55c7 Binary files /dev/null and b/Exercise_3.class differ diff --git a/Exercise_3.java b/Exercise_3.java index fb66d329d..5238b2e3e 100644 --- a/Exercise_3.java +++ b/Exercise_3.java @@ -2,7 +2,7 @@ // Java program to implement // a Singly Linked List -public class LinkedList { +public class Exercise_3 { Node head; // head of list @@ -12,17 +12,21 @@ public class LinkedList { static class Node { int data; - Node next; + Node next; + // Constructor - Node(int d) + Node(int data) { //Write your code here + this.data = data; + this.next = null; + } } // Method to insert a new node - public static LinkedList insert(LinkedList list, int data) + public static Exercise_3 insert(Exercise_3 list, int data) { // Create a new node with given data @@ -34,29 +38,47 @@ public static LinkedList insert(LinkedList list, int data) // Insert the new_node at last node // Return the list by head + Node newNode = new Node(data); + //LL is empty + if(list.head == null){ + list.head = newNode; + return list; + } + + // LL is not empty so traverse till last and then insert the element + Node temp = list.head; + + while(temp.next != null){ + temp = temp.next; + } + temp.next = newNode; + return list; } // Method to print the LinkedList. - public static void printList(LinkedList list) + public static void printList(Exercise_3 list) { // Traverse through the LinkedList - // Print the data at current node - - // Go to next node + // Go to next node + Node temp = list.head; + while(temp != null){ + System.out.print(+temp.data +" -> "); + temp = temp.next; + } + System.out.println("null"); } // Driver code public static void main(String[] args) { /* Start with the empty list. */ - LinkedList list = new LinkedList(); + Exercise_3 list = new Exercise_3(); // // ******INSERTION****** - // - + // Insert the values list = insert(list, 1); list = insert(list, 2);