From 2182e7dc2ee26185ece953a05b720767ea5eb4f2 Mon Sep 17 00:00:00 2001 From: tpawaiya Date: Wed, 11 Feb 2026 14:20:48 -0700 Subject: [PATCH] commit changes --- Exercise_1.class | Bin 0 -> 970 bytes Exercise_1.java | 42 +++++++++++++++------- LinkedList$Node.class | Bin 0 -> 375 bytes LinkedList.class | Bin 0 -> 1408 bytes Exercise_3.java => LinkedList.java | 38 +++++++++++--------- Main.class | Bin 0 -> 1090 bytes Stack.class | Bin 0 -> 1065 bytes StackAsLinkedList$StackNode.class | Bin 0 -> 418 bytes StackAsLinkedList.class | Bin 0 -> 1607 bytes Exercise_2.java => StackAsLinkedList.java | 32 +++++++++++------ 10 files changed, 74 insertions(+), 38 deletions(-) create mode 100644 Exercise_1.class create mode 100644 LinkedList$Node.class create mode 100644 LinkedList.class rename Exercise_3.java => LinkedList.java (67%) create mode 100644 Main.class create mode 100644 Stack.class create mode 100644 StackAsLinkedList$StackNode.class create mode 100644 StackAsLinkedList.class rename Exercise_2.java => StackAsLinkedList.java (56%) diff --git a/Exercise_1.class b/Exercise_1.class new file mode 100644 index 0000000000000000000000000000000000000000..ead77032d772bfeb9287109cb566365d88a0ac00 GIT binary patch literal 970 zcmaKrZEq4m5Xb*pXb;X)u#dJ{M6C*?E!O8s?F-RH64WL%Hq{qqIo5LJxV_vhjeHZo zhQ6RlP5c0UDB~>G1R98!+|JzI?C(D_H}~i7*>3=^v1MWcF#~Z6lbB*iAM+Dlbh&p_ z{7^p@wqlstc05PzFvN2Cj|LJ9lQqTdmI(tfy#gO$%AvVwf4D%3%092iMb(QG_o34ukpT zn+R+t6kj%}*0O8}hE&z@#QRRWE`ohtcWEcy<_>Y@bDzh7h*FcCz!tlX4o(e_8#*!S z*DEPir~>Z46RIg2VFGKEHOuZv-xrPS*Fd(jRI%MkO<}`C5f2P(>fStLSQ&gn$2*ZN zQH+`pwQHB#N(Nnq@542o2S#ddxz}(-xKfp})$vQC6%6fMIJn>S#hCEn9z;`B8vaD* zh%$HGnxjOC!K!$k2+A%GLlGKy!f6OMG)I0K) z$uhmOdJ6V~9FnJ1zXqNniF>$Dz79R4qx$grFBpfXNLCAJvj_8_ke-c9Mw_3wQs`m! z4A+1jZVZr11O>K`zzh0cN;L4>r2Z0X*g*_AdN+YQR>>wvaGmY~9?_FY-eXGTITrr` DcWLij literal 0 HcmV?d00001 diff --git a/Exercise_1.java b/Exercise_1.java index 314a3cb45..e6d953445 100644 --- a/Exercise_1.java +++ b/Exercise_1.java @@ -1,35 +1,53 @@ class Stack { - //Please read sample.java file before starting. - //Kindly include Time and Space complexity at top of each file + // Time Complexcity : + // push() - o(1) + // pop() - o(1) + // peek() - o(1) + // isEmpty() - o(1) + // Space Complexity : o(MAX) + static final int MAX = 1000; int top; int a[] = new int[MAX]; // Maximum size of Stack boolean isEmpty() - { - //Write your code here + { + return (top < 0); + } Stack() { - //Initialize your constructor + top = -1 ; } 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 (isEmpty()){ + System.out.println("Stack Underflow"); + return 0; + } + return a[top--]; } - + + int peek() { - //Write your code here + if (isEmpty()){ + System.out.println("Stack is Empty"); + return 0; + } + return a[top]; } } diff --git a/LinkedList$Node.class b/LinkedList$Node.class new file mode 100644 index 0000000000000000000000000000000000000000..e2ba79de8e372fd0b201edc1f62730c05b14617d GIT binary patch literal 375 zcmZ8cJx{|h6g)R+^Fe5m@=>4*14EmE4vZj{B1EJp4502#W1*DbO0kRjw-_oU27UlP z3h|r;#PII??DM^QU*8{}0B$hy(ShZ_4$wu9AzJcRKCQWYn%);nvCs^?t4db-n!y@R z<{n&l4*UQ+2pGbwlFy>dDy7fzRVf(kl55VuQlsn#2ocex6dQ7ivw!kSdK#ybInk{B zZTv*Oeknxr$cvi%{D;-LS%}-pG=IjOo1hGCE|k7oDed5h#J~OwK`NzaW;ItzD4JmM z;gnK!XmeAv0U=C8LPpg^Ogx~|1u2XsOy0qg(H2g^w%C1Zg?oep*n|;b43D}#_F+*O T>jNCNJj5}vPnIr*IDz{EVs%1t literal 0 HcmV?d00001 diff --git a/LinkedList.class b/LinkedList.class new file mode 100644 index 0000000000000000000000000000000000000000..cfb6cd685881995ee04248cf382f24a5dc6feab7 GIT binary patch literal 1408 zcmaJ>U2hXd6g?B$S=(7B!H@(JJ_1R=9|Wi28>f_FND1Azm5f4BAF7ExB8%-^%R52j ziNC==;1y745J-Kb62F8W(8s9Z%-WJ2CE{W4&d%I>?z!jA-7o)t{0G22JWOH$5ercp zF$@ZfK9kR7p&`Aeh2Pensj3zjTyi~E-xi2uvTGLN0wa#=)m6=L13g>zYbpr~Ndqt> z5Y4P)*Ahr!*usd7Q5+FS_WbFvp=3?qh|}8)L5c~Y9JO)GL_|IHLMJee2@A(72LZBcN$L_Y zCU4^+=Bcgg1*%Q|sZ5_9MW*km!^9~RY%JghfkcRJlxG|E#bno5XNleD^hcF9o9n9m zNUk@yld5Q0t*^>f_dD8@t|u^_`SUQE>{!L$Y**E?Yl8dQeaRFpFjQ87UR5Rm5)O)PU0urvGB7I{jR`l?|!-7bHA<%9d$c>Se8}ox3>gd z^~=|F?0`RzUag^mS;zP5o2}wO`h7e50v~O))FJNu0(5SCvA_3Sh>~nHDy~*V0ei*s zRJ+uWL7-TC)C_=yWr4W^r5(oD!UKWR-*@G`=#)pzQk+jf@Zl)YM!Dvs(k}Ar6yN47 zmfL}N%Z0#YT5}8rCd@74+PJ<%gu%e#CsFuyb$BS9yLL>CYH4 zuk!1?|K@p=&bb zGl)01OXd%fGj*LcS)Kf6OuoZdej8ICiHyH7!*$wuz4z7BG!WJ_&a#05?E;||ahzGE zaV^Z1z`!nI%reQNtK96A{8u+i{u5G9c93=Q(GSQu`B-QNLu-XL9@-DM)YI@B;>@>1 zTf#8^l*e!%lUQcXU#P=Fyd3Rh+9I`4kxoq)EeKBkytlKBHG~ F;v2>HJ!=2} literal 0 HcmV?d00001 diff --git a/Exercise_3.java b/LinkedList.java similarity index 67% rename from Exercise_3.java rename to LinkedList.java index fb66d329d..f4018cac2 100644 --- a/Exercise_3.java +++ b/LinkedList.java @@ -1,6 +1,3 @@ -import java.io.*; - -// Java program to implement // a Singly Linked List public class LinkedList { @@ -17,7 +14,9 @@ static class Node { // Constructor Node(int d) { - //Write your code here + data = d; + next = null; + } } @@ -25,28 +24,35 @@ static class Node { public static LinkedList insert(LinkedList list, int data) { // Create a new node with given data + Node new_node = 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.head == null){ + list.head = new_node; + return list; + } + Node current = list.head; + while (current.next != null) { + current = current.next; + } + current.next = new_node; + return list; + } - // 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 - + Node current = list.head; + while (current != null) { // Print the data at current node - + System.out.print(current.data + " "); // Go to next node - } - + current = current.next; + } + } + // Driver code public static void main(String[] args) { diff --git a/Main.class b/Main.class new file mode 100644 index 0000000000000000000000000000000000000000..1cef80aa164e7a0d40aab84c9c34c01263be6f05 GIT binary patch literal 1090 zcmaKr?M@Rx6o%iir3>q7E1!ajRY40CDt@6B!B{1dEJ{)WQGb|WJJO-soz3o)$enl_ z`U4V7yZ|rCcxIP`5*qi%%7a;P3`0Fs8LZI6;0FeB9$}KdL+Pz^ zNgmmnY6ynBD+BSq)7}x$Cg1VNGUzIg`&&Gc=KsW))lEsU>PN~hZiWVH0Tmk#ao zXqWtldTcW=+Ogc^4hUt}RVVVqsx;#Hx8FqM$yj_@m^Ud>5vVJI5KuaRmOsdio~pTiRyPqAp>nW^$~hN;unAcK9?5|y-^Y2jD7r&V;o@V&cQ zCyuGwJ03KA5l^{FwK`$-e6n4ZGY2;hLeay$yB_KLtae}a38Kb*zb>_)>NsmbAflSj z<5WzZk_eBcaFodc@|wmR=1Dq3QJXv-U?H(HJSOWyA^|q7 Js>E5r_&;7(21Wn? literal 0 HcmV?d00001 diff --git a/Stack.class b/Stack.class new file mode 100644 index 0000000000000000000000000000000000000000..ab0ea69bb0bbc6529efa9ca646e2a33574da72d1 GIT binary patch literal 1065 zcmZva+fEZv6o&sjGccu0DQ%&mMX1Q3t=a+}6O2(2lO{z>s*TnQ9m)uU?M%~75gx_F z8?U_KMkAOQ;{*62@)X8@PYW&T#q7hHwf=7%_RrtnegJrYTLVy_>d*}IpFA)#a3z_nrxamR+amnT}U7t1U|f)FT0H+1;?HQ?MQDQLDCY zHJ+I3Ra&A2w_;XHX2X{Ej#2Zr>?RW__}C?C`iE7se(X?hHMgjm%BRb0?JiTT%L%dR zd7A5%#i0d@ZmUtT9@>%A>g1pHeSR zrnJN|U!i9r>e2V$0mYRAh)*;tzG@U2&m&w15#q~1F)NDwnl=mcgk>Eq&U$rIch7X5-{ zX)44Bx2w3;$-tzLENm)sh{<ggnL5sF_w k!Ie9GO!VMpx;Pg54<}1bj+}XN7JE2}zc~Uo$Wn0g4`;xsl>h($ literal 0 HcmV?d00001 diff --git a/StackAsLinkedList$StackNode.class b/StackAsLinkedList$StackNode.class new file mode 100644 index 0000000000000000000000000000000000000000..b996e395f612935197c9b9792b9d9b99e84d3159 GIT binary patch literal 418 zcmaKo!A`HbJF`E(U*7=k(GB6i_2ES*p-gBj#hV!BqIelT&6aYm3FVut$n-72 z?e(VtDhPap5vqs?-H8_S)tySSVkH-8ru2oajMocE@D@S~0wt!a9wA19i-mk=*R%AW z)@#OkNq@>|WSwGuK#sSsGr4&dvz)!d$$Gn)%ZJQd+IwD?W)DJTER}v-EA8W$(A>ia zQBo9gGs=ZhQV{}+nDKGWLp%IQCU{v^mU2hXd6g^{avbM8M94F+jU14*cV}krJ@=eD_xitgr~d%Bi(e9mAgV(%FokJ> z)C=>aS?-$l^YVj(7qaCGOz&8><=+s9<_nMFh#{^cVPFP^zXh z^6vFq|0p2&G>$c7bbMl99UJdyY94vM?9s~^km9*|pw@EA5BrwwH~qdedzBcn0VThg4H{*eagiH{FKx51qCb!#!44WGUH| z?d-38rAvr{S=p66Y5VGG#N0Gkz#s6Vj=HK#Lm)S5V9S2#bYwZKe^{zr zv*kN|w#o0~H#Bq%Iovnxc2|12y5n>PZe>#J*vWf=`$w)k!+pG5;Yd}+uV)BRHM`xW z<;#kIQL}B?uXat(ldMrZD5V-29X|ZVb*`Y?9XKzHWppz|PP~t=WuNp;fMzgFTQezJ~rUbnQ=Q(O3Ks84Fp!cRZOw zWagHNX4?;=D~R$3EWbtaN#+D|jctYV2I=DV8|b6dX=rp?K^&_LUc(YHgy6Ugfu1p+ zQnXb(#<@R`{2Qz3Pmggv`by!w#^*c5aFHf#X2frent?2I)?|(|KaU(UY|?H8oTCtm zE2n6fXqRbJX;wd!XoMYdE)jd1*cXXi9%06D12>15){y-5w}?GCL8)F!C62LON-YL< zgO++q$0(oTD}b`X?Kh|RHrS5pw83n^RW)d fQRQluXZN|g8`xd!@hr`=B#|EQd5E8}kJbMHS)*9) literal 0 HcmV?d00001 diff --git a/Exercise_2.java b/StackAsLinkedList.java similarity index 56% rename from Exercise_2.java rename to StackAsLinkedList.java index 5a9c4868c..cbc09e0dd 100644 --- a/Exercise_2.java +++ b/StackAsLinkedList.java @@ -8,33 +8,45 @@ static class StackNode { 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. + StackNode newNode = new StackNode(data); + newNode.next = root; + root = newNode; } 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 - } - + if (isEmpty()){ + System.out.println("Stack is Empty"); + return 0; + } + int popped = root.data; + root = root.next; + return popped; + } + public int peek() { - //Write code to just return the topmost element without removing it. - } - + if (isEmpty()){ + System.out.println("Stack is Empty"); + return 0; + } + return root.data; + } + //Driver code public static void main(String[] args) {