From 006b925dc181d2cfcd89b14e48976ae83ca89bab Mon Sep 17 00:00:00 2001 From: Simranb10 Date: Sun, 14 Jun 2026 17:40:26 -0400 Subject: [PATCH] Precourse-1 solution --- Exercise_1.java | 86 ++++++++++++++++++-------------- Exercise_2.java | 39 +++++++++++---- Exercise_3.java | 129 ++++++++++++++++++++++++------------------------ 3 files changed, 143 insertions(+), 111 deletions(-) diff --git a/Exercise_1.java b/Exercise_1.java index 314a3cb45..f1dfa0d43 100644 --- a/Exercise_1.java +++ b/Exercise_1.java @@ -1,36 +1,50 @@ -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 - } +//Time Complexity: O(1) +//Space Complexity: O(1) - 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 - } +class Stack { + + static final int MAX = 1000; + int top; + int a[] = new int[MAX]; // Maximum size of Stack + boolean isEmpty() + { + return top < 0; + } + + Stack() + { + this.top = -1; + } + + boolean push(int x) + { + if(top >= MAX - 1) { + System.out.println("Stack Overflow"); + return false; + } else { + a[++top] = x; + return true; + } + } + + int pop() + { + if(isEmpty()) { + System.out.println("Stack Underflow"); + return 0; + } else { + return a[top--]; + } + } + + int peek() { + if (isEmpty()) { + System.out.println("Stack Underflow"); + return 0; + } else { + return a[top]; + } + } } // Driver code @@ -38,9 +52,9 @@ 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"); + s.push(10); + s.push(20); + s.push(30); + System.out.println(s.peek() + " Peeked from stack"); } } diff --git a/Exercise_2.java b/Exercise_2.java index 5a9c4868c..da889fa79 100644 --- a/Exercise_2.java +++ b/Exercise_2.java @@ -1,45 +1,64 @@ -public class StackAsLinkedList { - +//Time Complexity : O(1) +//Space Complexity : O(n) +public class Exercise_2 { + StackNode root; - static class StackNode { + static class StackNode { int data; StackNode next; StackNode(int data) { - //Constructor here + this.data = data; } } 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; } 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()) { + System.out.println("Stack Underflow"); + return 0; + } else { + int popped = root.data; + root = root.next; + return popped; + } } public int peek() - { - //Write code to just return the topmost element without removing it. + { + if (isEmpty()) { + System.out.println("Stack Underflow"); + return 0; + } else { + 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.java b/Exercise_3.java index fb66d329d..35a7bc203 100644 --- a/Exercise_3.java +++ b/Exercise_3.java @@ -1,70 +1,69 @@ -import java.io.*; +//Time Complexity : O(n) +//Space Complexity : O(n) +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 +// a Singly Linked List +public class Exercise_3 { - // 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) + { + this.data = d; + } + } + + // Method to insert a new node + public static Exercise_3 insert(Exercise_3 list, int data) + { + Node node = new Node(data); + if(list.head == null) { + list.head = node; + } else { + Node current = list.head; + while(current.next != null) { + current = current.next; + } + current.next = node; + } + return list; + } + + public static void printList(Exercise_3 list) + { + for(Node current = list.head; current != null; current = current.next) { + System.out.println(current.data); + } + } + + // 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); - } + Exercise_3 list = new Exercise_3(); + + // + // ******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