From 9539070bf338bc4fdd9debf253b5ac982bfdf408 Mon Sep 17 00:00:00 2001 From: Priya Gupta Date: Mon, 29 Dec 2025 14:58:07 -0800 Subject: [PATCH] PreCourse-1 done --- Exercise_1.java | 27 +++++++++++++++++++++++++++ Exercise_2.java | 38 ++++++++++++++++++++++++++++++-------- Exercise_3.java | 30 ++++++++++++++++++++++++++++-- 3 files changed, 85 insertions(+), 10 deletions(-) diff --git a/Exercise_1.java b/Exercise_1.java index 314a3cb45..534107367 100644 --- a/Exercise_1.java +++ b/Exercise_1.java @@ -1,3 +1,7 @@ +/** + * Time Complexity : O(1) for all operations (push, pop, peek, isEmpty) + * Space Complexity : O(N) where N is maximum size of the array (MAX) + */ class Stack { //Please read sample.java file before starting. //Kindly include Time and Space complexity at top of each file @@ -8,28 +12,51 @@ class Stack { boolean isEmpty() { //Write your code here + // A stack is empty if top is at its intial value + return top<0; } Stack() { //Initialize your constructor + this.top = -1; } boolean push(int x) { //Check for stack Overflow //Write your code here + if(top>=MAX){ + System.out.println("Stack Overflow"); + return false; + } + else{ + a[++top] = x; + return true; + } } int pop() { //If empty return 0 and print " Stack Underflow" //Write your code here + if(top==-1) { + System.out.print("Stack Underflow"); + return 0; + } + int element = a[top--]; + return element; } int peek() { //Write your code here + if(top==-1){ + System.out.println("Stack is Empty"); + return 0; + } + else + return a[top]; } } diff --git a/Exercise_2.java b/Exercise_2.java index 5a9c4868c..6b3a8f40e 100644 --- a/Exercise_2.java +++ b/Exercise_2.java @@ -1,38 +1,60 @@ +/** + * Time Complexity: O(1) for all operations (push, pop, peek, isEmpty) as we only manipulate the head pointer. + * Space Complexity: O(N) where N is the number of elements in the stack. + */ public 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() { - //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); + if(root==null){ + root = newNode; + }else{ + newNode.next = root; + root = newNode; + } } public int pop() { - //If Stack Empty Return 0 and print "Stack Underflow" + //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.print("Stack Underflow"); + return 0; + } + int poppedValue = root.data; + root = root.next; + return poppedValue; } public int peek() { //Write code to just return the topmost element without removing it. + if(root!=null) + return root.data; + else + return -1; } //Driver code diff --git a/Exercise_3.java b/Exercise_3.java index fb66d329d..11b231399 100644 --- a/Exercise_3.java +++ b/Exercise_3.java @@ -1,3 +1,10 @@ +/** + * Insert: Time Complexity: O(n) because we must traverse the entire list to find the end. + * Space Complexity: O(1) + * + * Print:Time Complexity: O(n) where n is the number of nodes. + * Space Complexity: O(1). + */ import java.io.*; // Java program to implement @@ -17,7 +24,8 @@ static class Node { // Constructor Node(int d) { - //Write your code here + this.data = d; + this.next = null; } } @@ -34,7 +42,17 @@ public static LinkedList insert(LinkedList list, int data) // Insert the new_node at last node // Return the list by head - + Node node = new Node(data); + if(list.head==null){ + list.head = node; + }else{ + Node last = list.head; + while(last.next!=null){ + last = last.next; + } + last.next = node; + } + return list; } // Method to print the LinkedList. @@ -45,6 +63,14 @@ public static void printList(LinkedList list) // Print the data at current node // Go to next node + Node currNode = list.head; + while(currNode!=null){ + Node curr = list.head; + + System.out.println(head.data); + head = head.next; + + } } // Driver code