From a9fe9c409fe5e86e06f70b68822d2bafaf8ef8de Mon Sep 17 00:00:00 2001 From: Indra Date: Tue, 7 Apr 2026 19:05:26 -0500 Subject: [PATCH] precourse-1 solution --- Exercise_1.java | 32 +++++++++++++++++++++++++++----- Exercise_2.java | 25 +++++++++++++++++++++++-- Exercise_3.java | 27 +++++++++++++++++++++------ 3 files changed, 71 insertions(+), 13 deletions(-) diff --git a/Exercise_1.java b/Exercise_1.java index 314a3cb45..d599c1be2 100644 --- a/Exercise_1.java +++ b/Exercise_1.java @@ -1,3 +1,6 @@ +// Time Complexity : O(1) for push, pop, peek, and isEmpty +// Space Complexity : O(N) where N is the MAX size of the stack + class Stack { //Please read sample.java file before starting. //Kindly include Time and Space complexity at top of each file @@ -7,29 +10,48 @@ class 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 + if (top >= (MAX - 1)) { + System.out.println("Stack Overflow"); + return false; + } else { + a[++top] = x; + System.out.println(x + " pushed into stack"); + 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; + } else { + int x = a[top--]; + return x; + } } int peek() { - //Write your code here + if (top < 0) { + System.out.println("Stack Underflow"); + return 0; + } else { + int x = a[top]; + return x; + } } } diff --git a/Exercise_2.java b/Exercise_2.java index 5a9c4868c..11f537198 100644 --- a/Exercise_2.java +++ b/Exercise_2.java @@ -1,4 +1,4 @@ -public class StackAsLinkedList { +class StackAsLinkedList { StackNode root; @@ -8,7 +8,9 @@ static class StackNode { StackNode(int data) { - //Constructor here + //Constructor here + this.data = data; + this.next = null; } } @@ -16,11 +18,16 @@ static class StackNode { public boolean isEmpty() { //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. + StackNode newNode = new StackNode(data); + newNode.next = root; + root = newNode; + System.out.println(data + " pushed to stack"); } public int pop() @@ -28,11 +35,25 @@ 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 + if (root == null) { + 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 (root == null) { + System.out.println("Stack Underflow"); + return 0; + } else { + return root.data; + } } //Driver code diff --git a/Exercise_3.java b/Exercise_3.java index fb66d329d..c8e6dda29 100644 --- a/Exercise_3.java +++ b/Exercise_3.java @@ -1,8 +1,7 @@ -import java.io.*; - + // Java program to implement // a Singly Linked List -public class LinkedList { +class LinkedList { Node head; // head of list @@ -17,7 +16,8 @@ static class Node { // Constructor Node(int d) { - //Write your code here + this.data = d; + this.next = null; } } @@ -25,7 +25,8 @@ static class Node { public static LinkedList insert(LinkedList list, int 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 @@ -33,8 +34,17 @@ public static LinkedList insert(LinkedList list, int data) // and insert the new_node there // Insert the new_node at last node + if (list.head == null) { + list.head = newNode; + } else { + Node last = list.head; + while (last.next != null) { + last = last.next; + } + last.next = newNode; + } // Return the list by head - + return list; } // Method to print the LinkedList. @@ -45,6 +55,11 @@ public static void printList(LinkedList list) // Print the data at current node // Go to next node + Node current = list.head; + while (current != null) { + System.out.println(current.data); + current = current.next; + } } // Driver code