From 9fbeca46f70924e3e5451d20d3d8daf2c6d4b4e6 Mon Sep 17 00:00:00 2001 From: yashhh-23 Date: Tue, 2 Jun 2026 18:18:22 +0530 Subject: [PATCH] done Pre course exercise - 1 --- Exercise_1.java | 32 ++++++++++++++++++++++++++++---- Exercise_2.java | 27 +++++++++++++++++++++++---- Exercise_3.java | 30 ++++++++++++++++++++++++------ 3 files changed, 75 insertions(+), 14 deletions(-) diff --git a/Exercise_1.java b/Exercise_1.java index 314a3cb45..835392b27 100644 --- a/Exercise_1.java +++ b/Exercise_1.java @@ -5,31 +5,55 @@ class Stack { int top; int a[] = new int[MAX]; // Maximum size of Stack + //Time complexxity : O(1) for push, pop and peek operations + //Space complexity : O(n) where n is the number of elements in the stack + boolean isEmpty() { - //Write your code here + return top < 0; } Stack() { - //Initialize your constructor - } + top=-1; //Initialize your constructor + } 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; + return true; + } } int pop() { //If empty return 0 and print " Stack Underflow" //Write your code here + if(isEmpty()) { + System.out.println("Stack Underflow"); + return 0; + } + else { + return a[top--]; + } } int peek() { //Write your code here + if(isEmpty()) { + System.out.println("Stack Underflow"); + return 0; + } + else { + return a[top]; + } } } diff --git a/Exercise_2.java b/Exercise_2.java index 5a9c4868c..cb75d9a8f 100644 --- a/Exercise_2.java +++ b/Exercise_2.java @@ -1,4 +1,6 @@ -public class StackAsLinkedList { +//Time Complexity : O(1) for push, pop and peek operations +//Space Complexity : O(n) +public class Exercise_2 { StackNode root; @@ -8,7 +10,8 @@ static class StackNode { StackNode(int data) { - //Constructor here + this.data = data; + this.next = null; } } @@ -16,30 +19,46 @@ 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; } public int pop() { //If Stack Empty Return 0 and print "Stack Underflow" + if (isEmpty()) { + System.out.println("Stack Underflow"); + return 0; + } + int poppedData = root.data; + root = root.next; + return poppedData; //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. + //Write code to just return the topmost element without removing it. + if (isEmpty()) { + System.out.println("Stack Underflow"); + return 0; + } + 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..2130bc7a1 100644 --- a/Exercise_3.java +++ b/Exercise_3.java @@ -1,3 +1,5 @@ +//Time Complexity : O(n) +//Space Complexity : O(1) import java.io.*; // Java program to implement @@ -5,7 +7,7 @@ public class LinkedList { Node head; // head of list - + // Linked list Node. // This inner class is made static // so that main() can access it @@ -18,6 +20,8 @@ static class Node { Node(int d) { //Write your code here + this.data = d; + this.next = null; } } @@ -25,27 +29,41 @@ 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 - + if (list.head == null) { + list.head = newNode; + } else { // Else traverse till the last node // and insert the new_node there - + Node current = list.head; + while (current.next != null) { + current = current.next; + } // Insert the new_node at last node + current.next = newNode; + + } // Return the list by head - + return list; } // Method to print the LinkedList. public static void printList(LinkedList list) { // Traverse through the LinkedList - + Node current = list.head; + while (current != null) { + // Print the data at current node - + System.out.println(current.data); // Go to next node + current = current.next; } + System.out.println(); + } // Driver code public static void main(String[] args)