-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueue.java
94 lines (75 loc) · 1.76 KB
/
Queue.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
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
package com.Shubhra;
// QUEUE is Fist in First out. Its like a line in ticket counter.
// That means when a new guy comes, he stands in the end(tail) of the line.
// On the other hand, the guy who is in the front(head) of the line gets the ticket first
// and then leave the line.
// We can use our linkedlist concept to implement this. All we need is the InsertLast
// method from our linkedlist to Push items in our queue , and RemoveFirst Method from
// Linkedlist to Pop items.
public class Queue<T>
{
Node head,tail; // get track of head and tail
int size; // size of the queue
// we use this subclass(class inside class) so we dont have to cast T explicitly.
public class Node
{
public Node next;
public T value;
public Node(T value)
{
this.value = value;
}
}
public Queue()
{
head = tail = null;
size = 0;
}
// Similar to Insert Last in linkedList
public void Push(T data)
{
Node n = new Node(data);
if(isEmpty())
{
head = tail = n;
return;
}
tail.next = n;
n.next = null;
tail = n;
}
public T Pop()
{
if(isEmpty())
{
return null;
}
Node n = head;
head = n.next;
return n.value;
}
public T Peek()
{
if(isEmpty())
{
System.out.print("Stack Underflow");
return null;
}
return head.value;
}
public int Size()
{
return size;
}
public boolean isEmpty()
{
return head==null;
}
public void ShowAll()
{
while (!isEmpty())
{
System.out.println(Pop());
}
}
}