From 6ad5a4507036e468e3fc8b1f76139001da14e6c3 Mon Sep 17 00:00:00 2001 From: Dainish-Chhaya Date: Sun, 9 Aug 2026 20:48:34 -0600 Subject: [PATCH] Pre course 1 submission --- Exercise_1.java | 25 ++++++++++++++++++++++--- Exercise_2.java | 27 +++++++++++++++++++++++---- Exercise_3.class | Bin 0 -> 1453 bytes Exercise_3.java | 44 +++++++++++++++++++++++++++++++++----------- 4 files changed, 78 insertions(+), 18 deletions(-) create mode 100644 Exercise_3.class diff --git a/Exercise_1.java b/Exercise_1.java index 314a3cb45..eb1f9b5fd 100644 --- a/Exercise_1.java +++ b/Exercise_1.java @@ -1,35 +1,54 @@ class Stack { //Please read sample.java file before starting. //Kindly include Time and Space complexity at top of each file + +// Time Complexity :O(1) for all operations +// Space Complexity : O(n) for storing elements in array +// Did this code successfully run on Leetcode : NA + static final int MAX = 1000; int top; int a[] = new int[MAX]; // Maximum size of Stack boolean isEmpty() { - //Write your code here + return top == -1; } Stack() { //Initialize your constructor + this.top = -1; } boolean push(int x) { - //Check for stack Overflow - //Write your code here + if(top == a.length -1){ + return false; + } + else{ + a[++top] = x; + return true; + } + } int pop() { //If empty return 0 and print " Stack Underflow" + if(top == -1){ + System.out.println("Stack Underflow"); + return 0; + } + return a[top--]; //Write your code here } int peek() { //Write your code here + if(top == -1) return -1; + return a[top]; } } diff --git a/Exercise_2.java b/Exercise_2.java index 5a9c4868c..0f631eaab 100644 --- a/Exercise_2.java +++ b/Exercise_2.java @@ -1,4 +1,7 @@ -public class StackAsLinkedList { +//TC - O(1) for all operations +//SC - O(n) for storing elements in linked list + +public class Exercise_2 { StackNode root; @@ -8,7 +11,7 @@ static class StackNode { StackNode(int data) { - //Constructor here + this.data = data; } } @@ -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(root == null){ + System.out.println("Stack Underflow"); + return 0; + } //Write code to pop the topmost element of stack. - //Also return the popped element + //Also return the popped element + 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){ + System.out.println("stack is empty"); + return -1; + } + 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.class b/Exercise_3.class new file mode 100644 index 0000000000000000000000000000000000000000..15d9a55c762c6aa77d86b3554874232f00f3911d GIT binary patch literal 1453 zcmaJ>U2hXd6g?B$S=(91!88OMpecmZ9|@*}(ht+L#3hh+Z7Ml6qCB99Jt9liyVmX+ zk*EHR{sUfVl@bI}AF0$|(jU;rsPxR*k{u=L!```b=iYPgJ#%OO`uEKr0B+-c8WV^a zh?_`YQeft(d?qW7be~l2Z$DM-P+;tw2+YZ#@)y0O_QE3=R zYk(<%cww{HN+E-316dO@I3YwiW1>arrQ&jM$ysA$`({NUSe*bIFoyAj-Z6Blria2**+0kN55(+eg(!X<&Z zuH02M&uz=_fgSEp3qt9J0Rh>=05c+jfVTUjn#Llo7+5m#F$w~wPe>||=;;zP#BqRP zD~Xcst?xJ*Y}v$$Mlzxp$4!&9m=@Pee1hv_-gX1!6K%dQ##WWZjksf38Qd`ODQ*g+ zA|%TW7?V8`Q*1GDjd#TZ%XU?x-`!UJmfUt|$uvXR-mS~ta3AkV+Z8xdcyv+|yw9fB z_uFd2*1==La8*+xFx5~&SXVj$$(&jDyf6rT*{iE?$Lj=1+$K+AVP$P0JBeBv-{Ove z4Sg$j1r|pGXuHq6T~!&-Ke(t3*$zE_PvDnveus_&{Cm>vI4W4QJa4z(s~+!Q%yJ}f zYp)d~1|(>U_$SpK*7At3FGV4j_J|BJeq-KBf^VJ!sUm>(7WW7`Xlx z#>4por0S)8n13Q&TK*ko>80K^R`!v5hbg@@UcCQjg-5y4OGZsZxJyI?ij-xRvy53z z_#AVb$0{!42CX-7joH4SR~E5D{l1Mu#3`p4#33FQ(;=YltEjVD<#)*chS~By=HK8$ z;1w=Y&si_t|4nnQ_zGp++n|O#yVJk61l9>z!&wg40ya><-6(Gg6NgAJW1h!fqB_j@ zc$l&L7MX_!xN4Q-uTineiAYXH(uia-lCQBk;`0)c%vq;wVj5fgiT#K?TFmL%^q!9oM){sYRcM@Rqw literal 0 HcmV?d00001 diff --git a/Exercise_3.java b/Exercise_3.java index fb66d329d..5238b2e3e 100644 --- a/Exercise_3.java +++ b/Exercise_3.java @@ -2,7 +2,7 @@ // Java program to implement // a Singly Linked List -public class LinkedList { +public class Exercise_3 { Node head; // head of list @@ -12,17 +12,21 @@ public class LinkedList { static class Node { int data; - Node next; + Node next; + // Constructor - Node(int d) + Node(int data) { //Write your code here + this.data = data; + this.next = null; + } } // Method to insert a new node - public static LinkedList insert(LinkedList list, int data) + public static Exercise_3 insert(Exercise_3 list, int data) { // Create a new node with given data @@ -34,29 +38,47 @@ public static LinkedList insert(LinkedList list, int data) // Insert the new_node at last node // Return the list by head + Node newNode = new Node(data); + //LL is empty + if(list.head == null){ + list.head = newNode; + return list; + } + + // LL is not empty so traverse till last and then insert the element + Node temp = list.head; + + while(temp.next != null){ + temp = temp.next; + } + temp.next = newNode; + return list; } // Method to print the LinkedList. - public static void printList(LinkedList list) + public static void printList(Exercise_3 list) { // Traverse through the LinkedList - // Print the data at current node - - // Go to next node + // Go to next node + Node temp = list.head; + while(temp != null){ + System.out.print(+temp.data +" -> "); + temp = temp.next; + } + System.out.println("null"); } // Driver code public static void main(String[] args) { /* Start with the empty list. */ - LinkedList list = new LinkedList(); + Exercise_3 list = new Exercise_3(); // // ******INSERTION****** - // - + // Insert the values list = insert(list, 1); list = insert(list, 2);