GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — programming/problems/queue/CircularQueue.java at master · rp-code9/programming

Expand file tree

/

CircularQueue.java

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
79 lines (68 loc) · 1.93 KB
/

CircularQueue.java

Copy path

File metadata and controls

79 lines (68 loc) · 1.93 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import java.util.NoSuchElementException;
class CircularQueue {
private String[] queue;
private int n;
private int first;
private int last;
public CircularQueue() {
queue = new String[2];
n = 0;
first = 0;
last = 0;
}
public boolean isEmpty() {
return n == 0;
}
public int size() {
return n;
}
private void resize(int capacity) {
String[] copy = new String[capacity];
for (int i = 0; i < n; ++i) {
copy[i] = queue[(first + i) % queue.length];
}
queue = copy;
first = 0;
last = n;
}
public void enqueue(String s) {
if (n == queue.length) {
resize(2 * queue.length);
}
queue[last++] = s;
if (last == queue.length) {
last = 0;
}
n++;
}
public String dequeue() {
if(isEmpty()) {
throw new NoSuchElementException("Underflow");
}
String s = queue[first];
queue[first] = null;
first++;
n--;
if (first == queue.length) {
first = 0;
}
if (n > 0 && n == queue.length / 4) {
resize(queue.length / 2);
}
return s;
}
public static void main(String[] args) {
String[] string = {"a", "b", "c", "d", "e"};
CircularQueue queue = new CircularQueue();
System.out.println("Size : " + queue.size() + ", isEmpty: " + queue.isEmpty());
for (String s : string) {
queue.enqueue(s);
System.out.println("Size : " + queue.size() + ", isEmpty: " + queue.isEmpty());
}
while (!queue.isEmpty()) {
System.out.println("Deuque : " + queue.dequeue());
System.out.println("Size : " + queue.size() + ", isEmpty: " + queue.isEmpty());
}
System.out.println("Size : " + queue.size() + ", isEmpty: " + queue.isEmpty());
}
}
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.