From 3b2294d332ebf489434e9619588ef5e7a614a7ff Mon Sep 17 00:00:00 2001 From: Mrunali Vaidya Date: Tue, 21 Apr 2026 17:59:54 +0000 Subject: [PATCH] Complete Java exercises --- Exercise_1.java | 29 ++++++++- Exercise_2$StackNode.class | Bin 0 -> 390 bytes Exercise_2.class | Bin 0 -> 1580 bytes Exercise_2.java | 116 ++++++++++++++++++--------------- Exercise_3$Node.class | Bin 0 -> 375 bytes Exercise_3.class | Bin 0 -> 1408 bytes Exercise_3.java | 129 ++++++++++++++++++------------------- Main.class | Bin 0 -> 964 bytes Stack.class | Bin 0 -> 906 bytes 9 files changed, 153 insertions(+), 121 deletions(-) create mode 100644 Exercise_2$StackNode.class create mode 100644 Exercise_2.class create mode 100644 Exercise_3$Node.class create mode 100644 Exercise_3.class create mode 100644 Main.class create mode 100644 Stack.class diff --git a/Exercise_1.java b/Exercise_1.java index 314a3cb45..fbb51546f 100644 --- a/Exercise_1.java +++ b/Exercise_1.java @@ -1,6 +1,12 @@ class Stack { //Please read sample.java file before starting. //Kindly include Time and Space complexity at top of each file + //timecomplexity + //isEmpty() ->O(1) + //push()->O(1) + //pop()->O(1) + //peek()->O(1) + //Spece Complexity: O(MAX) static final int MAX = 1000; int top; int a[] = new int[MAX]; // Maximum size of Stack @@ -8,28 +14,49 @@ class Stack { boolean isEmpty() { //Write your code here + return(top < 0); } Stack() { //Initialize your constructor + top = -1; //stack is empty } boolean push(int x) { //Check for stack Overflow //Write your code here + //push : first increment top then store value + if(top >= MAX-1){ + System.out.println("Stack is 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$StackNode.class b/Exercise_2$StackNode.class new file mode 100644 index 0000000000000000000000000000000000000000..0760c5ea349586f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 100644 --- a/Exercise_2.java +++ b/Exercise_2.java @@ -1,52 +1,64 @@ -public class StackAsLinkedList { - - StackNode root; - - static class StackNode { - int data; - StackNode next; - - StackNode(int data) - { - //Constructor here - } - } - - - public boolean isEmpty() - { - //Write your code here for the condition if stack is empty. - } - - public void push(int data) - { - //Write code to push data to the stack. - } - - 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 - } - - public int peek() - { - //Write code to just return the topmost element without removing it. - } - - //Driver code - public static void main(String[] args) - { - - StackAsLinkedList sll = new StackAsLinkedList(); - - sll.push(10); - sll.push(20); - sll.push(30); - - System.out.println(sll.pop() + " popped from stack"); - - System.out.println("Top element is " + sll.peek()); - } -} +public class Exercise_2 { + + // Time Complexity: + // isEmpty() -> O(1) + // push() -> O(1) + // pop() -> O(1) + // peek() -> O(1) + // + // Space Complexity: + // O(n) + + StackNode root; + + static class StackNode { + int data; + StackNode next; + + StackNode(int data) { + this.data = data; + this.next = null; + } + } + + public boolean isEmpty() { + return root == null; + } + + public void push(int data) { + StackNode newNode = new StackNode(data); + newNode.next = root; + root = newNode; + } + + public int pop() { + if (isEmpty()) { + System.out.println("Stack Underflow"); + return 0; + } + + int popped = root.data; + root = root.next; + return popped; + } + + public int peek() { + if (isEmpty()) { + System.out.println("Stack Underflow"); + return 0; + } + + return root.data; + } + + public static void main(String[] args) { + Exercise_2 sll = new Exercise_2(); + + sll.push(10); + sll.push(20); + sll.push(30); + + System.out.println(sll.pop() + " popped from stack"); + System.out.println("Top element is " + sll.peek()); + } +} \ No newline at end of file diff --git a/Exercise_3$Node.class b/Exercise_3$Node.class new file mode 100644 index 0000000000000000000000000000000000000000..7b630f6853f9e502b2351a5fcd3743a8c9570090 GIT binary patch literal 375 zcmY*UyH3ME5S+7}_<_L*NqD89ph#ShA_WDACIVq8G6g6tvYiuh6l}?MqWl&`ghasy z@KK04Q$!Ryvv)T$d;9(K`32w#eFqJ6Z5Ze&I5Kf;;zYohM^z*s0=AjlRHQJUO93~? zGFeQPQCZ4Tpgq0Y$RbHg`7*vxife)9O`4_EEiHp!I3rDJr#DSA8Lr-9Sv*Jal3C7E zzA6%VpQ?km3%XoHYYw+USym5uS(!L>;o{6i6Q&Cbd;jG1T!EHyjh0dNI(m#3GU1<{ zU8aCO7|w9cB^%saV=;^fl}8jT7yIM`c4I1(#`8ZQ{Qef2FSgL%YoSLxWY8aWV53V@ W4?0@B)6nLt;tm|L!$^a@4eJ*fnL(@o literal 0 HcmV?d00001 diff --git a/Exercise_3.class b/Exercise_3.class new file mode 100644 index 0000000000000000000000000000000000000000..500bada3046da3b17adff1b31f065f73c6ca7b02 GIT binary patch literal 1408 zcmaJ>U2hXd6g}gtz42y=AvBOC6bP8sv6GmP(g23E#Sk#sxRs3fP#@659+4&XuH~H| z^3=c4e~{OOGVjZC1?q zL8N+GV5TrsrBa(4UI`p)XxZFam)(9^w%hXq?fDTibB8ixw2t%xfm4MaychDNEVpIw zyxh=Cd0sK+P9xmzHPxDL7~}@z7frOlSY1VWT^W{8cNe z4-ZPx^vZAx`X#Ee-ER0=RczD*E*%l{A#MU^KOTo!er4nQ00kco6s<*{V@Es5)n|O0 zvvhGE;ukucY}!n`=^$%HhrS8@5hF|nmfs@vbY>qT>%~1-e`2gS{~N4boH5J#!XDD^ zFlJu$-S_`2@GM>YnQ5uG@CA(0pJw4RI8O8_oTELDJekhpB9{2u_>>V7unsU~BXt0a zcKlyLXUz!r$vk1b6ys(Ms8@Q2>0gm4?ZJLy0Dec7e%jl8|F_YUK%YXIeu;L0r4|WY zCh$@W#~;-J(gaGFnXVYg&-y5(w{V{BW6UdAui<*7bZj%RwPQOP+t)ZY=wTxDD*3GN z3a+u`*D2rzF)I{#3q`C_)Y+KdIqoJ`lVV$pd?U|6j@fr`4|mCOj{Bn|R^(IqAN8p^ A)&Kwi literal 0 HcmV?d00001 diff --git a/Exercise_3.java b/Exercise_3.java index fb66d329d..6fd518b64 100644 --- a/Exercise_3.java +++ b/Exercise_3.java @@ -1,70 +1,63 @@ -import java.io.*; - -// Java program to implement -// a Singly Linked List -public class LinkedList { - - Node head; // head of list - - // Linked list Node. - // This inner class is made static - // so that main() can access it - static class Node { - - int data; - Node next; - - // Constructor - Node(int d) - { - //Write your code here - } - } - - // Method to insert a new node - public static LinkedList insert(LinkedList list, int data) - { - // Create a new node with given 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 +import java.io.*; - // Insert the new_node at last node - // Return the list by head - - } - - // Method to print the LinkedList. - public static void printList(LinkedList list) - { - // Traverse through the LinkedList - - // Print the data at current node - - // Go to next node - } - - // Driver code - public static void main(String[] args) - { - /* Start with the empty list. */ - LinkedList list = new LinkedList(); - - // - // ******INSERTION****** - // - - // Insert the values - list = insert(list, 1); - list = insert(list, 2); - list = insert(list, 3); - list = insert(list, 4); - list = insert(list, 5); - - // Print the LinkedList - printList(list); - } +// Time Complexity: +// insert() -> O(n) +// printList() -> O(n) +// +// Space Complexity: +// O(n) + +public class Exercise_3 { + + Node head; // head of list + + static class Node { + int data; + Node next; + + Node(int d) { + this.data = d; + this.next = null; + } + } + + // Method to insert a new node + public static Exercise_3 insert(Exercise_3 list, int data) { + Node new_node = new Node(data); + + if (list.head == null) { + list.head = new_node; + } else { + Node last = list.head; + while (last.next != null) { + last = last.next; + } + last.next = new_node; + } + + return list; + } + + // Method to print the LinkedList + public static void printList(Exercise_3 list) { + Node currNode = list.head; + + while (currNode != null) { + System.out.print(currNode.data + " "); + currNode = currNode.next; + } + } + + // Driver code + public static void main(String[] args) { + Exercise_3 list = new Exercise_3(); + + list = insert(list, 1); + list = insert(list, 2); + list = insert(list, 3); + list = insert(list, 4); + list = insert(list, 5); + + printList(list); + } } \ No newline at end of file diff --git a/Main.class b/Main.class new file mode 100644 index 0000000000000000000000000000000000000000..838c9b12bf7afe5eddbb82c09341c870cee4c937 GIT binary patch literal 964 zcmaJ=?M@Rx6g|_gE$eD4rJyLaYSESg6~9ocs5BCr6eTHvpg+vC9qH2Doz3o)*f;St z^hZfB@d11&Ma%`WwJHp6k$&(lDbVgjBGkr!b4Tz`Hh0 zDK~X2Afw@yhAczurRkVzgCU$R>@h?tvLzT2HPaCvx^_c&yS!o1PQ>P>!!VowTsz{& zykv3buvAx`=^T~=pk9|ZFTSj;2H6HK!C;8i6>oM7+{PUZcMasQ zXy6|1GbA^qRKD`KyDd~(w){j4`8Wy&mQd8NY+wbeBw~9YA~{m{42KDBhH%9a64Hzuq;Q>R6Wp||OidOckC+(~s z2xpkHc}G;F)8uO3RBiGo;3%Jnsqo}rh-aYWd_4xu4XRr-&vbqgRoK(87IglEAvfy8 zbdF_5oXvbFVv9GG^m-cB=^$%TcDmGsQFR>QRV?oNf|Pw9uW-gO9P)eav@GH0hQRWq zkg=U}z}=oJE=V808N+iakI&?cqQWh!ZYojM@Qh(`qQ8r@F=YOqhb*S(+6PaFKDupS zxJ=#xSw=RxbOQE+9EK~j#s-@&2$VpQ{2=v!paFR07eWUo2-k{9y^qL#F*!3dnZW!+ zwAe@N6dKS+d_*}6d#$)!$+nn+W47#X^|IW3oCGe2AxbgzNNz5DfG5 G-S`Ke*X<1e literal 0 HcmV?d00001 diff --git a/Stack.class b/Stack.class new file mode 100644 index 0000000000000000000000000000000000000000..22ffb777e235eec76ddf4679e7a9fcba44c0d573 GIT binary patch literal 906 zcmZuvQBTug6g^+tt*ms84MAX{69u-a8zUw>V2p-fBmrkKLl7Udj880VZL@VKKgGls zpM2p(BbXTD5Acu3uP~mkn-U@qy_rhu^&Y)gU6s^`nc-PV?DKXtd7MAG%hZS1YNVLwma^?Wb77tgJ8F6?$*^C+$; z7ClrVWTb)-O1URs)q`%kAs>2*EVKMpwi{kYzNnUW-F??a2DXh$7!yduLNc6Fd2IdU4Oe*78p{*m8R?ORBBthvJstu>Ia=j zw&*eFvM{n5H+w_o!kCPUgBay|0@~nx z)ytg`R?`b?Q^>IM6`GUPPrh46FdXd&;uB%IWz4}~%rGWtD$6{_d>j*fk_}cgBD2au zR{!+@>CZ^ylE*OhLt=*Wj0NN6CRy_}<_Pr~Us}=g3+m Q>pWTSoU#h~SIfWp2Zj}smjD0& literal 0 HcmV?d00001