-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedQueue.java
68 lines (52 loc) · 1.44 KB
/
LinkedQueue.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
package projectCode20280;
public class LinkedQueue<E> implements Queue<E> {
//Linked list representation of a queue
SinglyLinkedList<E> list= new SinglyLinkedList<E>();
public LinkedQueue() {
}
public static void main(String[] args) {
LinkedQueue<Integer> que= new LinkedQueue<Integer>();
for(int i=0;i<5;i++)
que.enqueue(i);
System.out.println(que);
System.out.println("Size : "+que.size());
System.out.println(que);
System.out.println("Top of queue : "+que.first());
System.out.println(que);
System.out.println("Dequeue : "+que.dequeue());
System.out.println(que);
System.out.println("Dequeue again : "+que.dequeue());
System.out.println(que);
System.out.println("Size : "+que.size());
}
public String toString() //This function acts a "toString" for objects.
{
String str="[";
for(int i=0;i<list.size();i++) {
str+=list.get(i);
if(i<list.size()-1)
str+=", ";
}
return str+"]";
}
@Override
public int size() {
return list.size();
}
@Override
public boolean isEmpty() {
return list.isEmpty() ;
}
@Override
public void enqueue(E e) { //adds to the end of the queue
list.addLast(e);
}
@Override
public E first() { //returns first element
return list.get(0);
}
@Override
public E dequeue() {
return list.removeFirst(); //removes first element in queue
}
}