Repository navigation
Expand file tree
/
Copy pathQueueImplUsingArray.java
More file actions
130 lines (107 loc) · 2.38 KB
/
Copy pathQueueImplUsingArray.java
File metadata and controls
130 lines (107 loc) · 2.38 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package com.learnds;
//Queue implementation using Arrays
/*
* Whenever we dequeue or remove elements from queue, we shift remaining elements towards front by 1.
*/
class QueueAr{/*Remember we should name the class as Queue
BUT inside package name conflicts occurs*/
int arr[];
int rear, front;//always people in front of queue gets popped. And people gets added at end of queue based on capacity
int capacity;
//int size; NOT NEEDED
QueueAr(int capacity){
this.capacity = capacity;
front=0;
rear=-1;
arr= new int[capacity];
}
void insert(int num){//add , enqueue or whatever term
if(isFull()){
System.out.println("Queue is full");
return;
}
rear++;
arr[rear]=num;
}
void peek(){
if(isEmpty()){
System.out.println("Queue is empty");
return;
}
//System.out.println("Value of front: "+front); It will be 0 since we shift elements by 1
System.out.println("Front element : "+arr[front]);
}
void pop(){//remove, dequeue or whatever term
int num=-1;
int i = front;
if(isEmpty()){
System.out.println("Queue is empty");
return;
}
num=arr[front];
for(i = front; i < rear; i++){
arr[i]=arr[i+1];
}
arr[i]=0;
rear--;
//rear--; or adjust by moving all rear by 1
System.out.println("Popped : "+num);
}
boolean isFull(){
if(rear+1 == capacity){
return true;
}
return false;
}
boolean isEmpty(){
if(rear<front){//or front > rear
return true;
}
return false;
}
void print(){
if(isEmpty()){
System.out.println("Queue is empty");
return;
}
int i =0;
for(i = front; i <=rear;i++){
System.out.print(arr[i]+" ");
}
System.out.println();
}
}
class QueueImplUsingArray{
public static void main(String args[]){
QueueAr q = new QueueAr(5);
System.out.println("Inside main");
q.insert(203);
q.insert(201);
q.insert(211);
q.insert(206);
q.insert(205);
q.peek();
q.print();
System.out.println("Added : ");
q.pop();
q.pop();
q.pop();
q.peek();
q.pop();
q.pop();
q.pop();
q.insert(1);
q.insert(11);
//System.out.println(q.peek());
q.print();
q.insert(2503);
q.insert(1503);
q.insert(503);
q.print();
q.pop();
q.insert(1509);
q.print();
q.insert(1511);
q.insert(9999); //should fail
}
}