-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue_using_array.java
More file actions
70 lines (68 loc) · 1.55 KB
/
Copy pathqueue_using_array.java
File metadata and controls
70 lines (68 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
public class queue_using_array
{
int size;
int[] arr;
int front = 0;
int rear = 0;
int currentSize = 0;
public queue_using_array(int size)
{
this.size=size;
this.arr = new int[size];
}
public boolean isEmpty()
{
if(front == rear) return true;
return false;
}
public boolean isFull()
{
if(currentSize == size) return true;
return false;
}
public void push(int element)
{
if(isFull()) System.out.println("Cannot push. Queue full");
else
{
arr[rear] = element;
System.out.println("Element added: " + arr[rear]);
rear++;
currentSize++;
}
}
public void pop()
{
if(isEmpty()) System.out.println("Queue is empty. Cannot pop");
else
{
System.out.println("Element removed: " + arr[front]);
front++;
currentSize--;
}
}
public void peek()
{
if(isEmpty()) System.out.println("Queue empty");
else System.out.println("Element at the front of queue: " + arr[front]);
}
public void size()
{
System.out.println("Current size of the queue: " + currentSize);
}
public static void main(String[] args)
{
queue_using_array q = new queue_using_array(3);
q.pop();
q.push(4);
q.push(2);
q.push(1);
q.push(6);
q.size();
q.pop();
q.pop();
q.peek();
q.pop();
q.size();
}
}