File tree Expand file tree Collapse file tree 2 files changed +53
-10
lines changed Expand file tree Collapse file tree 2 files changed +53
-10
lines changed Original file line number Diff line number Diff line change 11package com .company .DataStructures ;
2-
2+ //Todo: Add comments and explanations.
33public class Queue {
44 private int [] arr ;
5- private int front ;
6- private int rear ;
5+ private int front = 0 ;
6+ private int rear =- 1 ;
77 private int size ;
8-
8+ private int count = 0 ;
99
1010 public Queue (int size ) {
1111 this .size = size ;
1212 arr = new int [this .size ];
1313 }
14+
15+ private boolean isEmpty (){
16+ if (count ==0 ){
17+ System .out .println ("Queue is empty!" );
18+ return true ;
19+ }
20+ return false ;
21+ }
22+
23+ private boolean isFull (){
24+ if (count ==size ){
25+ System .out .println ("Queue is full!" );
26+ return true ;
27+ }
28+ return false ;
29+ }
30+
31+ public void enqueue (int val ){
32+ if (!isFull ()){
33+ arr [++rear % size ]=val ;
34+ count ++;
35+ }
36+
37+
38+ }
39+
40+ public int peek (){
41+ if (!isEmpty ())
42+ return arr [rear ];
43+ return -1 ;
44+ }
45+
46+ public int dequeue (){
47+ if (!isEmpty ()){
48+ count --;
49+ return arr [front ++ % size ];
50+ }
51+ return -1 ;
52+ }
53+
54+
1455}
Original file line number Diff line number Diff line change 11package com .company ;
22
3+ import com .company .DataStructures .Queue ;
34import com .company .DataStructures .Stack ;
45
56public class Main {
67
78 public static void main (String [] args ) {
89// write your code here
9- Stack myStack = new Stack (5 );
10+
11+ Queue myQueue = new Queue (5 );
1012
1113 for (int i =0 ;i <5 ;i ++){
12- myStack . push (i *10 );
13- System .out .println (myStack .peek ());
14+ myQueue . enqueue (i *10 );
15+ System .out .println (myQueue .peek ());
1416 }
15- myStack . push (20 );
17+ myQueue . enqueue (20 );
1618
1719 for (int i =0 ;i <5 ;i ++)
18- System .out .println (myStack . pop ());
19- System .out .println (myStack . pop ());
20+ System .out .println (myQueue . dequeue ());
21+ System .out .println (myQueue . dequeue ());
2022
2123
2224 }
You can’t perform that action at this time.
0 commit comments