diff --git a/Exercise_1.java b/Exercise_1.java index 314a3cb45..e0352f8dd 100644 --- a/Exercise_1.java +++ b/Exercise_1.java @@ -1,46 +1,85 @@ -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 - } - - Stack() - { - //Initialize your constructor - } - - boolean push(int x) - { + //Kindly include Time and Space complexity at top of each file + // Time complexity is O(1) for each operation and space complexity is O(N) + + static final int MAX = 1000; + int top; + int a[] = new int[MAX]; // Maximum size of Stack + + boolean isEmpty() + { + //Write your code here + return (top < 0); + } + + Stack() + { + //Initialize your constructor + top = -1; + } + + boolean push(int x) + { //Check for stack Overflow //Write your code here - } - - int pop() - { + + if(top >= (MAX -1)) { + System.out.println("stack full"); + + return false; + } + + a[++top] = x; + System.out.println("pushed to stack"); + + return true; + } + + int pop() + { //If empty return 0 and print " Stack Underflow" //Write your code here - } - - int peek() - { - //Write your code here - } -} - + + if(top < 0) { + System.out.println("stack empty"); + + return 0; + } + + int element = a[top--]; + + return element; + } + + int peek() + { + if(top < 0) { + System.out.println("stack empty"); + + return 0; + } + + return a[top]; + } +} + // Driver code -class Main { - public static void main(String args[]) - { - Stack s = new Stack(); - s.push(10); - s.push(20); - s.push(30); - System.out.println(s.pop() + " Popped from stack"); - } +class Main { + public static void main(String args[]) + { + Stack s = new Stack(); + s.push(10); + System.out.println(s.peek()); + s.push(20); + System.out.println(s.peek()); + s.push(30); + System.out.println(s.peek()); + System.out.println(s.pop() + " Popped from stack"); + System.out.println(s.peek()); + System.out.println(s.isEmpty()); + System.out.println(s.pop() + " Popped from stack"); + System.out.println(s.pop() + " Popped from stack"); + System.out.println(s.isEmpty()); + } } diff --git a/Exercise_2.java b/Exercise_2.java index 5a9c4868c..4e7ac9ce4 100644 --- a/Exercise_2.java +++ b/Exercise_2.java @@ -1,4 +1,6 @@ -public class StackAsLinkedList { +// Space complexity is O(N) +// Time complexity is O(1) for each operation as we are inserting at the start and removing from the start and we have reference of the root node. +class StackAsLinkedList { StackNode root; @@ -8,31 +10,56 @@ static class StackNode { StackNode(int data) { - //Constructor here + //Constructor here + this.data = data; + this.next = null; } } public boolean isEmpty() { - //Write your code here for the condition if stack is empty. + //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. + //Write code to push data to the stack. + StackNode newNode = new StackNode(data); + newNode.next = root; + root = newNode; + System.out.println("data pushed to stack " + data); } 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 + //Also return the popped element + + if (isEmpty()) { + return 0; + } + + int poppedElement = root.data; + root = root.next; + + return poppedElement; } public int peek() { //Write code to just return the topmost element without removing it. + + if(isEmpty()) { + System.out.println("stack is empty"); + + return 0; + } + + return root.data; } //Driver code diff --git a/Exercise_3.java b/Exercise_3.java index fb66d329d..4153babdc 100644 --- a/Exercise_3.java +++ b/Exercise_3.java @@ -1,8 +1,11 @@ +// Space complexity is O(n) +// Time complexity O(1) best case and worst case or avergae O(N) + import java.io.*; // Java program to implement // a Singly Linked List -public class LinkedList { +class LinkedList { Node head; // head of list @@ -17,7 +20,9 @@ static class Node { // Constructor Node(int d) { - //Write your code here + //Write your code here + this.data = d; + this.next = null; } } @@ -34,7 +39,26 @@ 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); + +// if the LL is empty + if (list.head == null) { + list.head = newNode; + + return list; + } + + Node last = list.head; + +// traverse to the last + while (last.next != null) { + last = last.next; + } + + last.next = newNode; + + return list; } // Method to print the LinkedList. @@ -44,7 +68,14 @@ public static void printList(LinkedList list) // Print the data at current node - // Go to next node + // Go to next node + + Node current = list.head; + + while (current != null) { + System.out.print(current.data + " "); + current = current.next; + } } // Driver code @@ -65,6 +96,11 @@ public static void main(String[] args) list = insert(list, 5); // Print the LinkedList - printList(list); + printList(list); + + list = insert(list, 10); + System.out.println("\n"); + printList(list); + } -} \ No newline at end of file +}