-
Notifications
You must be signed in to change notification settings - Fork 17
/
QueuePractice.java
58 lines (49 loc) · 1.7 KB
/
QueuePractice.java
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
import java.util.LinkedList;
import java.util.Stack;
public class QueuePractice <D> {
//Creating a new LinkedList
LinkedList<D> queue = new LinkedList();
// Making a queue instance
public QueuePractice() {
queue = new LinkedList();
}
// Is our queue empty
public boolean isEmpty() {
return queue.isEmpty();
}
// What is the size of our queue
public int size() {
return queue.size();
}
// Enqueueing an item
public void enqueue(D n) {
queue.addLast(n);
}
// Dequeueing an item
public D dequeue() {
return queue.remove(0);
}
// Peek at the first item
public D peek() {
return queue.get(0);
}
public static void main(String[] args) {
// Creating A Stack. Only strings can be in this stack, but other data types can be added to stacks with proper notation/syntax
Stack<String> StackPractice = new Stack<>();
StackPractice.push("there");
StackPractice.push("hi");
System.out.println(StackPractice.pop() + " ");
System.out.println("Peek:" + StackPractice.peek());
System.out.println(StackPractice.pop() + "! ");
System.out.println("Size:" + StackPractice.size());
// Queues in the Main Method
QueuePractice numberQueue = new QueuePractice();
numberQueue.enqueue(5);
numberQueue.enqueue(7);
numberQueue.enqueue(6);
System.out.println("First out: " + numberQueue.dequeue());
System.out.println("Peek at second item: " + numberQueue.peek());
System.out.println("Second out: " + numberQueue.dequeue());
System.out.println("Third out: " + numberQueue.dequeue());
}
}