Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit bc2981f

Browse files
Added Queue Topic
1 parent 0ad3592 commit bc2981f

File tree

2 files changed

+165
-0
lines changed

2 files changed

+165
-0
lines changed

‎Queue/Queue.java

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
public class Queue {
2+
public class Node {
3+
4+
public String data;
5+
private Node next;
6+
private Node previous;
7+
8+
public Node(String data) {
9+
this.data = data;
10+
this.next = null;
11+
}
12+
13+
public void setNextNode(Node node) {
14+
this.next = node;
15+
}
16+
17+
public void setPreviousNode(Node node) {
18+
this.previous = node;
19+
}
20+
21+
public Node getNextNode() {
22+
return this.next;
23+
}
24+
25+
public Node getPreviousNode() {
26+
return this.previous;
27+
}
28+
29+
}
30+
31+
public class LinkedList {
32+
33+
public Node head;
34+
35+
public LinkedList() {
36+
this.head = null;
37+
}
38+
39+
public void addToHead(String data) {
40+
Node newHead = new Node(data);
41+
Node currentHead = this.head;
42+
this.head = newHead;
43+
if (currentHead != null) {
44+
this.head.setNextNode(currentHead);
45+
}
46+
}
47+
48+
public void addToTail(String data) {
49+
Node tail = this.head;
50+
if (tail == null) {
51+
this.head = new Node(data);
52+
} else {
53+
while (tail.getNextNode() != null) {
54+
tail = tail.getNextNode();
55+
}
56+
tail.setNextNode(new Node(data));
57+
}
58+
}
59+
60+
public String removeHead() {
61+
Node removedHead = this.head;
62+
if (removedHead == null) {
63+
return null;
64+
}
65+
this.head = removedHead.getNextNode();
66+
return removedHead.data;
67+
}
68+
69+
public String toString() {
70+
Node currentNode = this.head;
71+
String output = "<head> ";
72+
while (currentNode != null) {
73+
output += currentNode.data + " ";
74+
currentNode = currentNode.getNextNode();
75+
}
76+
output += "<tail>";
77+
return output;
78+
}
79+
80+
}
81+
82+
public LinkedList queue;
83+
public int size;
84+
static final int DEFAULT_MAX_SIZE = Integer.MAX_VALUE;
85+
public int maxSize;
86+
87+
public Queue() {
88+
this(DEFAULT_MAX_SIZE);
89+
}
90+
91+
public Queue(int maxSize) {
92+
this.queue = new LinkedList();
93+
this.size = 0;
94+
this.maxSize = maxSize;
95+
}
96+
97+
public boolean hasSpace() {
98+
return this.size < this.maxSize;
99+
}
100+
101+
public boolean isEmpty() {
102+
return this.size == 0;
103+
}
104+
105+
public void enqueue(String data) {
106+
if (this.hasSpace()) {
107+
this.queue.addToTail(data);
108+
this.size++;
109+
System.out.println("Added " + data + "! Queue size is now " + this.size);
110+
} else {
111+
throw new Error("Queue is full!");
112+
}
113+
}
114+
115+
public String dequeue() {
116+
if (!this.isEmpty()) {
117+
String data = this.queue.removeHead();
118+
this.size--;
119+
System.out.println("Removed " + data + "! Queue size is now " + this.size);
120+
return data;
121+
} else {
122+
throw new Error("Queue is empty!");
123+
}
124+
}
125+
126+
public String peek() {
127+
if (this.isEmpty()) {
128+
return null;
129+
} else {
130+
return this.queue.head.data;
131+
}
132+
}
133+
134+
public static void main(String[] args) {
135+
Queue boundedQueue = new Queue(4);
136+
boundedQueue.enqueue("pizza");
137+
boundedQueue.dequeue();
138+
}
139+
}

‎Queue/readme.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Queues
2+
A queue is a data structure which contains an ordered set of data.
3+
4+
Queues provide _three_ methods for interaction:
5+
6+
1. **Enqueue** - Adds data to the "back" or end of the queue
7+
2. **Dequeue** - Provides and removes data from the "front" or beginning of the queue
8+
3. **Peek** - Reveals data from the "front" of the queue without removing it
9+
10+
Queues are a **FIFO** structure which means _First In First Out_.
11+
12+
## Implementation
13+
Queues can be implemented using a linked list as the underlying data structure.
14+
15+
The front of the queue is equivalent to the head node of a linked list and the back of the queue is equivalent to the tail node.
16+
17+
Since operations are only allowed affecting the front or back of the queue, any traversal or modification to other nodes within the linked list is not allowed.
18+
19+
Since both ends of the queue must be accessible, a reference to both the head node and the tail node must be maintained.
20+
21+
If a queue has a _limit on the amount of data_ that can be placed into it, it is considered a **bounded queue**.
22+
23+
Similar to stacks, attempting to enqueue data onto an _already full queue_ will result in a **queue overflow**.
24+
25+
If we attempt to dequeue data from _an empty queue_, it will result in a **queue underflow**.
26+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /