From fa4d5fe7b35a62110dbdba01e36f4f90d76697f9 Mon Sep 17 00:00:00 2001 From: Arkadyuti Date: Tue, 12 Dec 2017 17:42:10 +0530 Subject: [PATCH 1/7] Stack and Queue added to new folder Data Structures --- .../Data Structures/Queue/queue.class | Bin 0 -> 2277 bytes .../Data Structures/Queue/queue.java | 136 ++++++++++++++++++ .../Data Structures/Stack/stack.class | Bin 0 -> 2336 bytes .../Data Structures/Stack/stack.java | 129 +++++++++++++++++ 4 files changed, 265 insertions(+) create mode 100644 Competitive Coding/Data Structures/Queue/queue.class create mode 100644 Competitive Coding/Data Structures/Queue/queue.java create mode 100644 Competitive Coding/Data Structures/Stack/stack.class create mode 100644 Competitive Coding/Data Structures/Stack/stack.java diff --git a/Competitive Coding/Data Structures/Queue/queue.class b/Competitive Coding/Data Structures/Queue/queue.class new file mode 100644 index 0000000000000000000000000000000000000000..d4831508bf10c0cd02e8bf6c7b2209419e44b903 GIT binary patch literal 2277 zcmaKtTT>KA6vzLw_Z^3;E+UIK7&W-c;ssH;XcR$Lxw?1(qH)-5U}TwDc4tw{rIHF| zp7NNgyrjxX9yXPiRIG?8P4WTqIr5wjU|EuX&yE7oN>OvV_sr>Ye*N#$!(XqS{RyBG zODbA$EQ%P8OVcUM32C~bIEhmcoL13|o(OvD8UH~9=OQ>SUH$b?a{$xsBt5sb*`s5E2JjH?X48bLaY2?ffM0^f8>L2$NUTMiwZ2MWR{a44D& zgo047X_?L${%%Vhyw0KD&eB~oWLjdRv^XOQ6UI!Ao~pECWEO^vy!Sg~T18P{&QMn5 zgu@7bn@kYSn#Fw1SYfI~!?YAMv`r7)HI|KzoMFv%q@99k&GpDgHEov)88K+e^y*Sc zltfYnYS@p%8unqYhE{3blV%U}Fs^C1jvET9$7QrWx-1H_Is0B1H#JORirkeK*Q{*i zje^?qw(S(1f{`B<&b*y1)`Tz}!G{`d;r5$MW>Fs)9-Ej{uy-IQ7KP;$b$eEK=7sKp zb*rv|dV72O8C}Cin2O+zf@m)tdPUGWcatT@%ypzQhGmI@1_Lv4HyH)-0m~5uH=z`M zOL7faP=cUaNyZ;gkW}3T-RjY#o^q8|k0tdBuGH#r&MGRaok@M*zR6>VR~G1or87wG zKzD4ta!48yIH2K>l(k)&Sy`g#2Ag6|&&=CqMlg*FXw31@S}EJck&VvW6&Y68-?lP_bHjAzIV(B_&r0S>9eg_*MRlw38Yod3*^e8vDJx&vpl$SM$h-N4 z+tZe~@1*1fVvox!Bq(_prtBfAux~>v(^|F{gxBR2&_N^P*oBoaY~Gb2+g>Q;6=*5h zApJR`SQMoHcC!**j!L^;GOTP)6!%pY_Po8tO`a_aCsy*}9pRhrV&!0aHtXi4=r?k? zwCRYRFbKQ|s6M=gNEw~<`F)SjDBp9eP531+O`wdb#3a3eJCdHHG9*5_f!TsEGdB{fiFOas zTXBMw&k`(!!!BM3fyU;j+z#)w$MiLsh>ELT?FO&5ixqXTPLt3p=}$hR44_+v%TP)A zMplo~FDd>1W$U6?rw~UUI!M)xbGV2>jN`J)7KA^3NTTseB{by)TEc9CBzNHU7!TQx5L0~h9JP~=QTG(}L$b_vmN~SB9evLcn{0lH zh7sxA>3O>XvA|DU1zh1H)vxq6(8YNlp9?a)@pX8xJr-QUuJZaH?Q3W%udi{4m)C#x z|AyxB`j0YpH)G4|-*IWBYe(3lz&!iag5DJATz!A6VQC0w(Lt! "); + + for(int i = front; i <= rear; i++) + System.out.print(q[i]+"\t"); + + System.out.println("<--- rear"); + + } + } //End display() + + + + + public static void main(String args[]) + { //Begin main() + + Scanner sc = new Scanner(System.in); + System.out.println("Enter the size of the queue"); + int n = sc.nextInt(); + + queue q1 = new queue(n); + + int choice; + do + { //Begin do + System.out.println("\nEnter:\n1. Insert\n2. Delete\n3. Display\n4. Exit"); + choice = sc.nextInt(); + switch(choice) + { //Begin switch + case 1: System.out.println("\nEnter an element to insert"); + int element = sc.nextInt(); + q1.insert(element); + break; + + case 2: q1.delete(); + break; + + case 3: q1.display(); + break; + + case 4: System.exit(0); + break; + + default: System.out.println("Wrong choice. Enter again!"); + } //End switch + } //End do + while(true); + } //End main() +} //End class diff --git a/Competitive Coding/Data Structures/Stack/stack.class b/Competitive Coding/Data Structures/Stack/stack.class new file mode 100644 index 0000000000000000000000000000000000000000..5a35d9f535006c1dca0cf26acbf28f17c07e9af9 GIT binary patch literal 2336 zcmaJ?ZBr9h6n<_J*sunKU?LLK1+`*qK=G{wu?55ui+~M6sa9Q*6;_hnG`m5u?X(}5 z@ndKF)R|6aI-SmBru|f>Vym70fc}zx>JM-n)93ESCRC@Hx#!+J=brPN*PB0IJoy8_ zNvx~TaIyt)oRZ|UBxhRi7TykHSj7m=hA|rD)v++fqY!dGAI78hLPKaeCN$-E?2RO&2*v8doffuL&_1w>jPY11-h%7ta4G_Nn`$!X2FdUoxSUaWnGie+b&mW$GI zu;0@y8U{a?vc?5d0;rAz z7?2kucmt^jdeIlb0Z9(xkbIuYirW|FtP%>>U>8tEzR>xVE?3l z*U{`-n!9TF{9&pVQ>oNBErJ}bhha!}QUV=b%w^Zi4`#BuWf`RiZec|R$HXNjE!Qad zW>}JKCL=I$TcF9qmG=(|45^;NuzGwzn`7$LA?|ijJ;9BBt0xDv^M2kn_0)hidDnCW z_V`MgZf%Q`>Jc2p;RxQ8c^;5tP?9x?)V)};trac1YMWVumOMZ86_&D>8Lby`cKNoE zB}fA+v*EZ#f&E~YDTw)1H|@bWmfU6G4ZScT(COLO&Su*b<)u9PCD#ACc2SWBO-Iv2 z&~hxBYveT38Ez2*y9@f7F=1O--CZ=@Ri2nS%Vm?t*w(LFqth~JUgj9*#V!0kbSIt3&&?t~CtysogH*GO`&V-ydgN0CF$&M=t9IU6$wASr4qc#q{ z?-%r}YnL_@P`!EDw%5uWr$}11&O}~!9D@OU-I$!34ZpdkbSsxPoP)mN$g5E|aJDJV zZxoH!TyGp&zt$s-sjQip(DV6>=^7&n$^wU8iSKp33GDm-dwe+KTtJYE&j2apOW^H% z47fLQaWMF7_Ve^9Z_2yJl8+&hfky}=6ORx~CVxTjcl8lM!O z|Nfae0UROiLzq-W2u*0g4z!^aU8H?zr@W2x5k-P`{;BGKoNz-9iaWEksZ{>NUS zKZ~9>I}qx8j)-`MZrKLMmd&>C7(rb#x1{(Wi(n^*qMz z8OiOcaZiV0p&zLQEO5zE$?_iH8s-|~n&O(3Dtlh4G^Jwny}MHVEwzP2rTUOtPo?@3 zx4o6>55ZrtuTuS1s_d6JsXm|uzl9eN "+s[top]); + } //End peep() + + /***** Function to display the elements of stack *****/ + void display() + { //Begin display() + + if(top == -1) + { //Begin if + System.out.println("No elements in stack"); + } //End if + + else + { //Begin else + + System.out.print("Elements of the stack: top---> "); + for(int i=top; i>=0; i--) + { + System.out.print(s[i]+"\t"); + } + System.out.println(); + } //End else + } //End display() + + + /***** main fucntion *****/ + public static void main(String args[]) + { //Begin main() + + Scanner sc = new Scanner(System.in); + System.out.println("Enter the size of the stack"); + int n = sc.nextInt(); + + stack st = new stack(n); + + int choice; + do + { //Begin do + System.out.println("\nEnter:\n1. Push\n2. Pop\n3. Peep\n4. Display \n5. Exit"); + choice = sc.nextInt(); + switch(choice) + { //Begin switch + case 1: System.out.println("\nEnter an element to push"); + int element = sc.nextInt(); + st.push(element); + break; + + case 2: st.pop(); + break; + + case 3: st.peep(); + break; + + case 4: st.display(); + break; + + case 5: System.exit(0); + break; + + default: System.out.println("Wrong choice. Enter again!"); + } //End switch + } //End do + while(true); + } //End main() +} //End class From 9121ab6fcb69b8ac150a1098a3def1894c4c9a3d Mon Sep 17 00:00:00 2001 From: Arkadyuti Paul Date: Thu, 14 Dec 2017 16:21:44 +0530 Subject: [PATCH 2/7] Update queue.java --- Competitive Coding/Data Structures/Queue/queue.java | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/Competitive Coding/Data Structures/Queue/queue.java b/Competitive Coding/Data Structures/Queue/queue.java index d2f22e5ad..38ddbfeab 100644 --- a/Competitive Coding/Data Structures/Queue/queue.java +++ b/Competitive Coding/Data Structures/Queue/queue.java @@ -1,7 +1,7 @@ import java.util.*; -class queue +class Queue { - int q[], front, rear, size; + private int q[], front, rear, size; /***** Constructor to initialize queue *****/ @@ -125,12 +125,10 @@ public static void main(String args[]) case 3: q1.display(); break; - case 4: System.exit(0); + default: System.out.println("Wrong choice. Exitting."); break; - - default: System.out.println("Wrong choice. Enter again!"); } //End switch } //End do - while(true); + while(choice >= 1 && choice <= 4); } //End main() } //End class From 8e22687af9d00222290d54302b8169c20cdfa23f Mon Sep 17 00:00:00 2001 From: Arkadyuti Paul Date: Thu, 14 Dec 2017 17:16:47 +0530 Subject: [PATCH 3/7] Update queue.java --- Competitive Coding/Data Structures/Queue/queue.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Competitive Coding/Data Structures/Queue/queue.java b/Competitive Coding/Data Structures/Queue/queue.java index 38ddbfeab..35e9dcb1f 100644 --- a/Competitive Coding/Data Structures/Queue/queue.java +++ b/Competitive Coding/Data Structures/Queue/queue.java @@ -1,3 +1,4 @@ +package Data_Structures; import java.util.*; class Queue { @@ -19,7 +20,7 @@ class Queue /***** Fucntion to insert an item to queue *****/ - void insert(int item) + private void insert(int item) { //Begin insert() if(rear == (size-1)) //Checks if rear pointer has reached the last limit (Queue is completely full or not) { @@ -47,7 +48,7 @@ void insert(int item) /***** Fucntion to delete an item from queue *****/ - void delete() + private void delete() { //Begin delete() @@ -76,7 +77,7 @@ void delete() /***** Function to display the elements of the queue *****/ - void display() + private void display() { //Begin display() if(front == - 1 && rear == -1) { From e91f5f19642bcd536031c514d6c84a7c619f09f2 Mon Sep 17 00:00:00 2001 From: Arkadyuti Paul Date: Thu, 14 Dec 2017 17:19:15 +0530 Subject: [PATCH 4/7] Update stack.java --- .../Data Structures/Stack/stack.java | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/Competitive Coding/Data Structures/Stack/stack.java b/Competitive Coding/Data Structures/Stack/stack.java index 2fc9b8c91..0193a6f2c 100644 --- a/Competitive Coding/Data Structures/Stack/stack.java +++ b/Competitive Coding/Data Structures/Stack/stack.java @@ -1,3 +1,4 @@ +package Data_Structures import java.util.*; class stack { //Begin class @@ -20,7 +21,7 @@ class stack /***** Fucntion to insert/push item to stack *****/ - void push(int item) + private void push(int item) { //Begin push() if(top == (size-1)) //Checks if stack is full @@ -39,7 +40,7 @@ void push(int item) /***** Function to delete/pop an item (topmost element) from stack *****/ - void pop() + private void pop() { //Begin pop() if(top == -1) //Checks if stack is empty @@ -56,7 +57,7 @@ void pop() /***** Function to peep (show the topmost element in stack) *****/ - void peep() + private void peep() { //Begin peep() if(top == -1) System.out.println("No element to peep"); @@ -65,8 +66,10 @@ void peep() System.out.println("Peep: top ---> "+s[top]); } //End peep() + + /***** Function to display the elements of stack *****/ - void display() + private void display() { //Begin display() if(top == -1) @@ -118,12 +121,10 @@ public static void main(String args[]) case 4: st.display(); break; - case 5: System.exit(0); + default: System.out.println("Wrong choice. Exitting!"); break; - - default: System.out.println("Wrong choice. Enter again!"); } //End switch } //End do - while(true); + while(choice >= 1 && choice <= 4); } //End main() } //End class From e47349ed7eb9ba7a35ed3ceb0b1be72bf80f9a12 Mon Sep 17 00:00:00 2001 From: Arkadyuti Paul Date: Thu, 14 Dec 2017 17:20:34 +0530 Subject: [PATCH 5/7] Update queue.java --- Competitive Coding/Data Structures/Queue/queue.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Competitive Coding/Data Structures/Queue/queue.java b/Competitive Coding/Data Structures/Queue/queue.java index 35e9dcb1f..48b33fccb 100644 --- a/Competitive Coding/Data Structures/Queue/queue.java +++ b/Competitive Coding/Data Structures/Queue/queue.java @@ -111,7 +111,7 @@ public static void main(String args[]) int choice; do { //Begin do - System.out.println("\nEnter:\n1. Insert\n2. Delete\n3. Display\n4. Exit"); + System.out.println("\nEnter:\n1. Insert\n2. Delete\n3. Display"); choice = sc.nextInt(); switch(choice) { //Begin switch @@ -130,6 +130,6 @@ public static void main(String args[]) break; } //End switch } //End do - while(choice >= 1 && choice <= 4); + while(choice >= 1 && choice <= 3); } //End main() } //End class From f2e21baae84b62a7d5cb5c4f0836e512b01c4f9b Mon Sep 17 00:00:00 2001 From: Arkadyuti Paul Date: Thu, 14 Dec 2017 17:21:41 +0530 Subject: [PATCH 6/7] Update stack.java --- Competitive Coding/Data Structures/Stack/stack.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Competitive Coding/Data Structures/Stack/stack.java b/Competitive Coding/Data Structures/Stack/stack.java index 0193a6f2c..cbb27d6f0 100644 --- a/Competitive Coding/Data Structures/Stack/stack.java +++ b/Competitive Coding/Data Structures/Stack/stack.java @@ -103,7 +103,7 @@ public static void main(String args[]) int choice; do { //Begin do - System.out.println("\nEnter:\n1. Push\n2. Pop\n3. Peep\n4. Display \n5. Exit"); + System.out.println("\nEnter:\n1. Push\n2. Pop\n3. Peep\n4. Display"); choice = sc.nextInt(); switch(choice) { //Begin switch From 9e1ed0657b336563877b994d779bd4d51dae26ad Mon Sep 17 00:00:00 2001 From: Arkadyuti Paul Date: Tue, 19 Dec 2017 14:00:56 +0530 Subject: [PATCH 7/7] Update queue.java --- .../Data Structures/Queue/queue.java | 41 +++++++++---------- 1 file changed, 19 insertions(+), 22 deletions(-) diff --git a/Competitive Coding/Data Structures/Queue/queue.java b/Competitive Coding/Data Structures/Queue/queue.java index 48b33fccb..6b20f68ec 100644 --- a/Competitive Coding/Data Structures/Queue/queue.java +++ b/Competitive Coding/Data Structures/Queue/queue.java @@ -27,18 +27,15 @@ private void insert(int item) System.out.println("Queue Overflow"); } - else + elseif(front == -1 && rear == -1) //Checks if queue is empty to insert the very first element { - if(front == -1 && rear == -1) //Checks if queue is empty to insert the very first element - { - front = 0; rear = 0; - } - - else - { - rear+=1; - } + front = 0; rear = 0; + q[rear] = item; + } + else + { + rear+=1; q[rear] = item; } @@ -57,20 +54,20 @@ private void delete() System.out.println("Queue Underflow"); } - else - { - System.out.println("Deleted element:\t"+q[front]); + - if(front == rear) //Checks if only one element is present - { - front = -1; rear = -1; - } - else - { - front += 1; - } - } + elseif(front == rear) //Checks if only one element is present + { + System.out.println("Deleted element:\t"+q[front]); + front = -1; rear = -1; + } + + else + { + System.out.println("Deleted element:\t"+q[front]); + front += 1; + } } //End delete()