Skip to content

Commit 7146741

Browse files
author
phyex0
committed
stack updated. queue added.
1 parent 68d90f1 commit 7146741

File tree

2 files changed

+53
-10
lines changed

2 files changed

+53
-10
lines changed
Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,55 @@
11
package com.company.DataStructures;
2-
2+
//Todo: Add comments and explanations.
33
public 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
}

src/com/company/Main.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
package com.company;
22

3+
import com.company.DataStructures.Queue;
34
import com.company.DataStructures.Stack;
45

56
public 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
}

0 commit comments

Comments
 (0)