- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.java
More file actions
Latest commit
95 lines (87 loc) · 1.92 KB
/
Copy pathqueue.java
File metadata and controls
95 lines (87 loc) · 1.92 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//implementing queue by array
//deletion takes place from the front
classQueue
{
intfront, rear, size;
intlength;
intarray[];
Queue(intlength)
{
this.length = length;
front = this.size=0;
rear= length-1;
array = newint[this.length];
}
//checking if Queue doesn't have more space left.
booleanisfull(Queueq)
{
return(q.size==q.length);
}
//checking if the queue is Empty.
booleanisempty(Queueq)
{
return(q.size==0);
}
//enqueue function to insert the elemnrs in the queue
voidenqueue(intelement)
{
if(isfull(this))
return;
this.rear = (this.rear + 1)%this.length;
this.array[this.rear] = element;
this.size = this.size + 1;
System.out.println(element+ " enqueued to queue");
}
//dequeue function queue to delete an element
intdequeue()
{
if(isempty(this))
returnInteger.MIN_VALUE;
intelement = this.array[this.front];
this.front = (this.front+1)%this.length;
this.size = this.size=1;
returnelement;
}
//getting the front of the queue
intfront()
{
if(isempty(this))
{
returnInteger.MIN_VALUE;
}
returnthis.array[this.front];
}
//getting the rear of the queue
intrear()
{
if(isempty(this))
{
returnInteger.MIN_VALUE;
}
returnthis.array[this.rear];
}
}
publicclassMainc
{
publicstaticvoidmain(Stringargs[])
{
Queueq = newQueue(500);
q.enqueue(1);
q.enqueue(2);
q.enqueue(3);
q.enqueue(4);
q.enqueue(5);
q.enqueue(6);
q.enqueue(7);
q.enqueue(8);
q.enqueue(9);
System.out.println("## BEFORE ##");
System.out.println( q.front()+ "Front element");
System.out.println("Rear element"+q.rear());
System.out.println("## AFTER ##");
q.dequeue();
q.dequeue();
System.out.println("Front element"+q.front());
System.out.println("Rear element"+q.rear());
}
}