From 99eefe6858478759942955ade97e61609d97f9c7 Mon Sep 17 00:00:00 2001 From: Sarvani Baru Date: Tue, 10 Feb 2026 18:26:36 -0800 Subject: [PATCH] Done Precourse - 1 --- Exercise_1.java | 25 ++++++++++++++++++++++--- Exercise_2.java | 33 +++++++++++++++++++++++++++------ Exercise_3.java | 43 ++++++++++++++++++++++++++++++++++--------- 3 files changed, 83 insertions(+), 18 deletions(-) diff --git a/Exercise_1.java b/Exercise_1.java index 314a3cb45..b0e75d7f9 100644 --- a/Exercise_1.java +++ b/Exercise_1.java @@ -1,4 +1,9 @@ -class Stack { +//T.C - O(1) , S.C - O(n) +// Your code here along with comments explaining your approach +/* +* I take a top variable which keeps track of the top element of stack and I increment and decrement it depending +* upon push or pop operation*/ +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; @@ -7,29 +12,43 @@ class Stack { boolean isEmpty() { - //Write your code here + //Write your code here + return top == 0; } Stack() { - //Initialize your constructor + //Initialize your constructor + top = 0; } boolean push(int x) { //Check for stack Overflow //Write your code here + if(top == MAX) + return false; + a[top++] = x; + return true; } int pop() { //If empty return 0 and print " Stack Underflow" //Write your code here + if(top == 0) { + System.out.println("Stack underflow"); + return 0; + } + return a[--top]; } int peek() { //Write your code here + if(top == 0) + return 0; + return a[top - 1]; } } diff --git a/Exercise_2.java b/Exercise_2.java index 5a9c4868c..a8cd7050b 100644 --- a/Exercise_2.java +++ b/Exercise_2.java @@ -1,6 +1,11 @@ -public class StackAsLinkedList { +//T.C - O(1) , S.C - O(n) +// Your code here along with comments explaining your approach +/* + * I take root node as top node which keeps track of the top element of stack and I append it to next nodes depending + * upon push or pop operation*/ +class StackAsLinkedList { - StackNode root; + StackNode root; static class StackNode { int data; @@ -8,31 +13,47 @@ 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 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 + //Also return the popped element + if(root == null) { + System.out.println("Stack underflow"); + return 0; + } + 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) + return 0; + return root.data; } //Driver code diff --git a/Exercise_3.java b/Exercise_3.java index fb66d329d..e9804efe6 100644 --- a/Exercise_3.java +++ b/Exercise_3.java @@ -1,8 +1,16 @@ -import java.io.*; +//T.C - O(1) for insertion at head and O(n) for insertion at the end. For traversal, O(n) as we iterate +// through all the nodes for printing, S.C - O(n) +// Your code here along with comments explaining your approach +/* + * I initialize the constructor with the incoming parameter and insert new node by checking if head is null and making the + * new node as head if null. If not, I safely append it by traversing till the end of the list. Similarly, I print + * the data values of each node by traversing them. + * */ +import java.io.*; // Java program to implement // a Singly Linked List -public class LinkedList { +class LinkedList { Node head; // head of list @@ -17,24 +25,36 @@ static class Node { // Constructor Node(int d) { - //Write your code here + //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 + // Create a new node with given data + Node newNode = new Node(data); // If the Linked List is empty, - // then make the new node as head + // then make the new node as head + if(list.head == null) + list.head = newNode; + // Else traverse till the last node - // and insert the new_node there + // and insert the new_node there + else { + Node temp = list.head; + while(temp.next != null) + temp = temp.next; + temp.next = newNode; + } // Insert the new_node at last node - // Return the list by head - + // Return the list by head + return list; } // Method to print the LinkedList. @@ -44,7 +64,12 @@ public static void printList(LinkedList list) // Print the data at current node - // Go to next node + // Go to next node + Node temp = list.head; + while(temp != null) { + System.out.println(temp.data); + temp = temp.next; + } } // Driver code