Skip to content

Commit 089df8b

Browse files
authored
Update BasicQueue.java
1 parent d5fbe87 commit 089df8b

1 file changed

Lines changed: 29 additions & 19 deletions

File tree

Data Structure/Queue/Basic Queue/BasicQueue.java

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
/*Queue is an abstract data structure, somewhat similar to Stacks.
2+
Unlike stacks, a queue is open at both its ends. One end is always
3+
used to insert data (enqueue) and the other is used to remove data
4+
(dequeue). Queue follows First-In-First-Out methodology, i.e., the
5+
data item stored first will be accessed first.*/
6+
7+
// this is the implementation of a basic queue
8+
19
import java.util.Arrays;
210
import java.util.Scanner;
311
class QueUe{
@@ -8,42 +16,43 @@ class QueUe{
816

917
QueUe(int capacity){
1018
size=capacity;
11-
Qarray = new int[capacity];
12-
rear=-1;
13-
front=-1;
14-
}
19+
Qarray = new int[capacity];//creating a queue as a array
20+
rear=-1;// initializing rear
21+
front=-1;//initializing front
22+
//queue is empty at first
23+
}
1524

1625
void display(){
17-
System.out.println(Arrays.toString(Qarray));
26+
System.out.println(Arrays.toString(Qarray)); //print the queue
1827
}
1928

2029
void value(){
21-
System.out.println("front:"+front+"\t rare:"+rear);
30+
System.out.println("front:"+front+"\t rare:"+rear); //to see the current rear & front
2231
}
2332

2433
void peek()
2534
{
2635
if (isEmpty())
2736
{
28-
System.out.println("Queue empty!");
37+
System.out.println("Queue empty!");//if queue empty-> no element to peek
2938
return;
3039
}
31-
System.out.println("peek value is "+Qarray[front]);
40+
System.out.println("peek value is "+Qarray[front]);//peeking the front value
3241
}
3342

3443
void enqueue(int val){
3544
if (isFull())
3645
{
3746
System.out.println("queue is full!");
38-
return;
47+
return; //can not enter element if queue is full
3948
}
4049
if(rear==-1){
41-
front++;
50+
front++; //for 1st element put in queue from queue were empty
4251
}
4352
System.out.println("Adding " + val);
4453

45-
rear++ ;
46-
Qarray[rear] = val;
54+
rear++ ; //adding new element in rear -> rear++
55+
Qarray[rear] = val; //putting the value
4756
}
4857

4958
void dequeue()
@@ -52,26 +61,26 @@ void dequeue()
5261
if (isEmpty())
5362
{
5463
System.out.println("queue is empty");
55-
return;
64+
return; // if queue is empty -> no element to delete
5665
}
5766

5867
System.out.println("Deleting " + Qarray[front]);
5968
if(front==rear){
6069
front=-1;
61-
rear=-1;//last element
62-
return;
70+
rear=-1;//when only 1 element was in queue , now deleted
71+
return; //now queue is empty
6372
}
64-
front++;
73+
front++; //deleting element-> front ++
6574
}
6675

6776
Boolean isFull()
6877
{
69-
return (rear==size-1);
78+
return (rear==size-1); // check if queue is full
7079
}
7180

7281
Boolean isEmpty()
7382
{
74-
return (front==-1);
83+
return (front==-1); // check if queue is empty
7584
}
7685
}
7786
class BasicQueue {
@@ -80,8 +89,9 @@ public static void main(String[] args)
8089
{
8190
QueUe que = new QueUe(4);
8291

92+
//just some random queue function sequence
8393
que.enqueue(5);
84-
que.peek();
94+
que.peek();
8595
que.dequeue();
8696
que.peek();
8797
que.enqueue(20);

0 commit comments

Comments
 (0)