From 463aa79eafaedc3c0cd2a09112be8cb9dd247130 Mon Sep 17 00:00:00 2001 From: Sanjoli Date: Mon, 1 Jun 2026 19:32:28 -0500 Subject: [PATCH 1/2] Done Exercise 1 and 2 --- Exercise_1.java | 95 +++++++++++++++++++++------------------ Exercise_2.java | 115 ++++++++++++++++++++++++++---------------------- 2 files changed, 114 insertions(+), 96 deletions(-) diff --git a/Exercise_1.java b/Exercise_1.java index 314a3cb45..571a3d2cf 100644 --- a/Exercise_1.java +++ b/Exercise_1.java @@ -1,46 +1,53 @@ -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 - } +class Stack { + // Time Complexity : O(1) + // Space Complexity : O(n) (n= MAX) + // Did this code successfully run on Leetcode : Yes (after updating the Class name) + // Any problem you faced while coding this : No - Stack() - { - //Initialize your constructor - } - - boolean push(int x) - { - //Check for stack Overflow - //Write your code here - } - - int pop() - { - //If empty return 0 and print " Stack Underflow" - //Write your code here - } - - 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"); - } + static final int MAX = 1000; + int top; + int a[] = new int[MAX]; // Maximum size of Stack + + boolean isEmpty() { + return top < 0; + } + + Stack() { + top = -1; + a = new int[MAX]; + } + + boolean push(int x) { + if (top < MAX - 1) { + a[++top] = x; + return true; + } + System.out.println("Stack Overflow"); + return false; + } + + int pop() { + if (top == -1) { + System.out.println("Stack Underflow"); + return 0; + } + return a[top--]; + } + + int peek() { + if (isEmpty()) { + return 0; + } + return a[top]; + } +} + +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"); + } } diff --git a/Exercise_2.java b/Exercise_2.java index 5a9c4868c..e5b009438 100644 --- a/Exercise_2.java +++ b/Exercise_2.java @@ -1,52 +1,63 @@ -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()); - } -} +public class StackAsLinkedList { + + // Time Complexity : O(1) + // Space Complexity : O(n) + // Did this code successfully run on Leetcode : Yes + // Any problem you faced while coding this : No + + StackNode root; + + static class StackNode { + int data; + StackNode next; + + StackNode(int data) { + this.data = data; + } + } + + public boolean isEmpty() { + return root == null; + } + + public void push(int data) { + if (isEmpty()) { + root = new StackNode(data); + return; + } + StackNode node = new StackNode(data); + node.next = root; + root = node; + } + + public int pop() { + if (root == null) { + System.out.println("Stack Underflow"); + return 0; + } + StackNode node = root; + root = root.next; + return node.data; + } + + public int peek() { + if (root != null) { + return root.data; + } + return 0; + } + + // 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()); + } +} From 8b543f94a034478ed8a79a1d40bddf033bd22f89 Mon Sep 17 00:00:00 2001 From: Sanjoli Date: Mon, 1 Jun 2026 19:48:36 -0500 Subject: [PATCH 2/2] Done Exercise 3 --- Exercise_3.java | 130 +++++++++++++++++++++++------------------------- 1 file changed, 62 insertions(+), 68 deletions(-) diff --git a/Exercise_3.java b/Exercise_3.java index fb66d329d..610db9ab6 100644 --- a/Exercise_3.java +++ b/Exercise_3.java @@ -1,70 +1,64 @@ -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 +import java.io.*; - // 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) - { - /* 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); - } +public class MyLinkedList { + + // Time Complexity : O(n) + // Space Complexity : O(n) + // Did this code successfully run on Leetcode : Yes + // Any problem you faced while coding this : No + + Node head; + + static class Node { + + int data; + Node next; + + // Constructor + Node(int d) { + this.data = d; + } + } + + // Method to insert a new node + public static MyLinkedList insert(MyLinkedList list, int data) { + // Create a new node with given data + Node node = new Node(data); + if (list.head == null) { + list.head = node; + } else { + Node n = list.head; + while (n.next != null) { + n = n.next; + } + n.next = node; + } + + return list; + } + + // Method to print the LinkedList. + public static void printList(MyLinkedList list) { + Node n = list.head; + while (n != null) { + System.out.println(n.data); + n = n.next; + } + } + + // Driver code + public static void main(String[] args) { + /* Start with the empty list. */ + MyLinkedList list = new MyLinkedList(); + + // 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