-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueueList.java
More file actions
57 lines (46 loc) · 1.13 KB
/
QueueList.java
File metadata and controls
57 lines (46 loc) · 1.13 KB
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
package com.company;
import java.util.NoSuchElementException;
public class QueueList {
private Node head;
private Node tail;
private static class Node {
private Node next;
private Object data;
public Node(Object data) {
this.data = data;
}
@Override
public String toString() {
return this.data.toString();
}
}
public void enQueue(Object element){
if(head == null){
tail = new Node(element);
head=tail;
}
else {
tail.next = new Node(element);
tail=tail.next;
}
}
public Object deQueue() {
if (head==null) throw new NoSuchElementException();
Node holder = head;
head = head.next;
return holder.data;
}
public void reverse(){
Node previous = null;
Node current = head;
Node next;
while (current!=null){
next = current.next;
current.next = previous;
previous=current;
current=next;
}
tail=head;
head=previous;
}
}