From 52b7dffff0e89cd9bcf64b7fe62bc015c098e674 Mon Sep 17 00:00:00 2001 From: namratha Date: Mon, 1 Jun 2026 22:57:41 -0500 Subject: [PATCH] preCourse1 --- Exercise_1.java | 36 ++++++++++++++++++++++++++---------- Exercise_2.java | 35 ++++++++++++++++++++++++++++++----- Exercise_3.java | 32 ++++++++++++++++++++++---------- 3 files changed, 78 insertions(+), 25 deletions(-) diff --git a/Exercise_1.java b/Exercise_1.java index 314a3cb45..3dd3cfa24 100644 --- a/Exercise_1.java +++ b/Exercise_1.java @@ -1,40 +1,56 @@ -class Stack { +/* + Time complexity isEmpty => O(1), push => O(1), pop => O(1) + */ + + +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 top; + int size = 0; int a[] = new int[MAX]; // Maximum size of Stack boolean isEmpty() { - //Write your code here + return top == 0; } Stack() { - //Initialize your constructor + top = 0 ; + size = 0; } boolean push(int x) - { - //Check for stack Overflow - //Write your code here + { + if (top >= MAX -1) { + System.out.println("Stack Overflow"); + return false; + } + a[top++] = x; + return true; } int pop() { //If empty return 0 and print " Stack Underflow" //Write your code here + if(top == 0) { + print(" Stack Underflow"); + return 0; + } + return a[top--]; } int peek() - { - //Write your code here + { + return a[top]; } } // Driver code -class Main { +public class Exercise_1 { public static void main(String args[]) { Stack s = new Stack(); diff --git a/Exercise_2.java b/Exercise_2.java index 5a9c4868c..6746e6741 100644 --- a/Exercise_2.java +++ b/Exercise_2.java @@ -8,31 +8,56 @@ static class StackNode { StackNode(int data) { - //Constructor here + StackNode node = new StackNode(); + node.data = data; + node.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. + StackNode node = new StackNode(data); + + if (root == null) { + root = node; + } + else { + 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; + } + else { + 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; + } + else { + return root.data; + } } //Driver code diff --git a/Exercise_3.java b/Exercise_3.java index fb66d329d..46086a039 100644 --- a/Exercise_3.java +++ b/Exercise_3.java @@ -1,7 +1,7 @@ import java.io.*; -// Java program to implement -// a Singly Linked List +// Time complexity: insert => O(n) where n is the size of the elements +// Space complexity: insert => O(1) public class LinkedList { Node head; // head of list @@ -17,7 +17,11 @@ static class Node { // Constructor Node(int d) { - //Write your code here + //Write your code here + Node a = new Node(); + a.data = d; + a.next = null; + } } @@ -25,16 +29,24 @@ static class Node { public static LinkedList insert(LinkedList list, int data) { // Create a new node with given data - + Node a = new Node(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 + if(list == null) { + head = a; + } + else { + Node curr = head; - // Insert the new_node at last node - // Return the list by head - + while (curr.next != null) { + curr = curr.next; + } + curr.next = a; + return head; + } } // Method to print the LinkedList.