From a42422b61a6e8f4c09f9dbe33c70c189903fc710 Mon Sep 17 00:00:00 2001 From: abhayb94 Date: Fri, 26 Dec 2025 13:06:43 -0500 Subject: [PATCH 1/4] Implemented stack using fixed size array --- Exercise_1.java | 106 +++++++++++++++++++++++++++++++----------------- 1 file changed, 68 insertions(+), 38 deletions(-) diff --git a/Exercise_1.java b/Exercise_1.java index 314a3cb45..95131d4af 100644 --- a/Exercise_1.java +++ b/Exercise_1.java @@ -1,46 +1,76 @@ -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 + static final int MAX = 1000; + int top; + int a[] = new int[MAX]; // Maximum size of Stack - Stack() - { - //Initialize your constructor - } - - boolean push(int x) - { + boolean isEmpty() + { + //Write your code here + if(a.length == 0 || a == null){ + return true; + }else{ + return false; + } + } + + Stack() + { + //Initialize your constructor + top = -1; + } + + + boolean push(int x) + { //Check for stack Overflow + if(top < MAX - 1){ + top += 1; + a[top]= x; + return true; + }else{ + System.out.println("***** Stack Overflow *****"); + return false; + } //Write your code here - } - - int pop() - { + } + + int pop() + { //If empty return 0 and print " Stack Underflow" + if(top < 0){ + System.out.print("***** Stack Underflow : "); + return 0; + }else{ + top -= 1; + return a[top+1]; + } //Write your code here - } - - int peek() - { + } + + int peek() + { //Write your code here - } -} - -// 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"); - } + if(top == -1){ + return 0; + }else{ + 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"); + System.out.println(s.pop() + " Popped from stack"); + System.out.println(s.pop() + " Popped from stack"); + System.out.println(s.pop() + " Popped from stack"); + } } From 964b3af9cef13701ee827be7638e56751dda39f2 Mon Sep 17 00:00:00 2001 From: abhayb94 Date: Fri, 26 Dec 2025 13:22:20 -0500 Subject: [PATCH 2/4] Implemented stack using LinkedList --- Exercise_2.java | 125 +++++++++++++++++++++++++++++------------------- 1 file changed, 75 insertions(+), 50 deletions(-) diff --git a/Exercise_2.java b/Exercise_2.java index 5a9c4868c..45ed49779 100644 --- a/Exercise_2.java +++ b/Exercise_2.java @@ -1,52 +1,77 @@ -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" +class StackAsLinkedList { + + StackNode root; + + static class StackNode { + int data; + StackNode next; + + StackNode(int data) { + //Constructor here + this.data = data; + this.next = null; + } + } + + + public boolean isEmpty() { + if (root == null) { + return true; + } else { + return false; + } + //Write your code here for the condition if stack is empty. + } + + public void push(int data) { + //Write code to push data to the stack. + if (root == null) { + root = new StackNode(data); + return; + } + StackNode node = new StackNode(data); + node.next = root; + root = node; + + } + + 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() - { + //Also return the popped element + if (root == null) { + System.out.print("***** Stack Underflow : "); + return 0; + } + int popped = root.data; + root = root.next; + return popped; + } + + 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()); - } -} + if (root == null) { + return 0; + } + return root.data; + } +} +class Main { + 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(sll.pop() + " popped from stack"); + System.out.println(sll.pop() + " popped from stack"); + System.out.println(sll.pop() + " popped from stack"); + + + System.out.println("Top element is " + sll.peek()); + } +} \ No newline at end of file From 28e6759ed3a7f83710e4ab193d39edebb5c88115 Mon Sep 17 00:00:00 2001 From: abhayb94 Date: Fri, 26 Dec 2025 13:38:15 -0500 Subject: [PATCH 3/4] Implemented LinkedList --- Exercise_3.java | 152 ++++++++++++++++++++++++++---------------------- 1 file changed, 84 insertions(+), 68 deletions(-) diff --git a/Exercise_3.java b/Exercise_3.java index fb66d329d..dc3d2b41e 100644 --- a/Exercise_3.java +++ b/Exercise_3.java @@ -1,70 +1,86 @@ -import java.io.*; - -// Java program to implement -// a Singly Linked List -public class LinkedList { - - Node head; // head of list - - // Linked list Node. - // This inner class is made static - // so that main() can access it - 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 +class LinkedList { - // 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 - } - - // Driver code - public static void main(String[] args) - { + Node head; // head of list + + // Linked list Node. + // This inner class is made static + // so that main() can access it + static class Node { + + int data; + Node next; + + // Constructor + Node(int d) + { + //Write your code here + this.data = d; + this.next = null; + } + } + + // Method to insert a new node + public static LinkedList insert(LinkedList list, int data) + { + // Create a new node with given data + Node node = new Node(data); + + // If the Linked List is empty, + if(list.head == null){ + list.head = node; + return list; + } + // then make the new node as head + Node currentNode = list.head; + while(currentNode.next != null){ + currentNode = currentNode.next; + } + currentNode.next = node; + return list; + + // Else traverse till the last node + // and insert the new_node there + + // 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 + if(list.head == null){ + System.out.println("***** List is empty *****"); + } + Node currentNode = list.head; + while (currentNode != null) { + // Print the data at current node + System.out.println(currentNode.data); + currentNode = currentNode.next; + + // Go to next node + } + } +} +public class Main{ + // Driver code + public static void main(String[] args) + { /* Start with the empty list. */ - LinkedList list = new LinkedList(); - - // - // ******INSERTION****** - // - - // Insert the values - list = insert(list, 1); - list = insert(list, 2); - list = insert(list, 3); - list = insert(list, 4); - list = insert(list, 5); - - // Print the LinkedList - printList(list); - } -} \ No newline at end of file + LinkedList list = new LinkedList(); + + // + // ******INSERTION****** + // + list.printList(list); + // Insert the values + list = list.insert(list, 1); + list = list.insert(list, 2); + list = list.insert(list, 3); + list = list.insert(list, 4); + list = list.insert(list, 5); + + // Print the LinkedList + list.printList(list); + } +} From 93220a8ff2362859798f30e674b8542fd4b153f1 Mon Sep 17 00:00:00 2001 From: abhayb94 Date: Fri, 26 Dec 2025 14:04:54 -0500 Subject: [PATCH 4/4] Added time and space complexity --- Exercise_1.java | 12 ++++++++++++ Exercise_2.java | 10 ++++++++++ Exercise_3.java | 6 ++++++ 3 files changed, 28 insertions(+) diff --git a/Exercise_1.java b/Exercise_1.java index 95131d4af..7b3f89ec9 100644 --- a/Exercise_1.java +++ b/Exercise_1.java @@ -7,6 +7,9 @@ class Stack { boolean isEmpty() { + // Time Complexity : O(1) + // Space Complexity : O(1) + //Write your code here if(a.length == 0 || a == null){ return true; @@ -24,6 +27,9 @@ boolean isEmpty() boolean push(int x) { + // Time Complexity : O(1) + // Space Complexity : O(1) + //Check for stack Overflow if(top < MAX - 1){ top += 1; @@ -38,6 +44,9 @@ boolean push(int x) int pop() { + // Time Complexity : O(1) + // Space Complexity : O(1) + //If empty return 0 and print " Stack Underflow" if(top < 0){ System.out.print("***** Stack Underflow : "); @@ -51,6 +60,9 @@ int pop() int peek() { + // Time Complexity : O(1) + // Space Complexity : O(1) + //Write your code here if(top == -1){ return 0; diff --git a/Exercise_2.java b/Exercise_2.java index 45ed49779..14822675c 100644 --- a/Exercise_2.java +++ b/Exercise_2.java @@ -15,6 +15,9 @@ static class StackNode { public boolean isEmpty() { + // Time Complexity : O(1) + // Space Complexity : O(1) + if (root == null) { return true; } else { @@ -24,6 +27,9 @@ public boolean isEmpty() { } public void push(int data) { + // Time Complexity : O(1) + // Space Complexity : O(1) + //Write code to push data to the stack. if (root == null) { root = new StackNode(data); @@ -39,6 +45,10 @@ 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 + + // Time Complexity : O(1) + // Space Complexity : O(1) + if (root == null) { System.out.print("***** Stack Underflow : "); return 0; diff --git a/Exercise_3.java b/Exercise_3.java index dc3d2b41e..7c2d185fc 100644 --- a/Exercise_3.java +++ b/Exercise_3.java @@ -22,6 +22,9 @@ static class Node { // Method to insert a new node public static LinkedList insert(LinkedList list, int data) { + // Time complexity: O(n) + // Space complexity: O(1) + // Create a new node with given data Node node = new Node(data); @@ -48,6 +51,9 @@ public static LinkedList insert(LinkedList list, int data) // Method to print the LinkedList. public static void printList(LinkedList list) { + // Time Complexity : O(n) + // Space Complexity : O(1) + // Traverse through the LinkedList if(list.head == null){ System.out.println("***** List is empty *****");