-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedListQueue.java
156 lines (139 loc) · 4.13 KB
/
LinkedListQueue.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
class LinkedListQueue {
static class Node {
Object data;
Node next;
public Node(Object data) {
this.data = data;
this.next = null;
}
}
private Node front, rear;
int size;
public LinkedListQueue() {
this.front = this.rear = null;
}
public void enqueue(Object data) {
Node newNode = new Node(data);
if (isEmpty()) {
front = rear = newNode;
size++;
return;
}
rear.next = newNode;
rear = newNode;
size++;
}
public Node dequeue() {
if (isEmpty()) {
System.out.println("Queue is empty. Cannot dequeue.");
return null;
}
front = front.next;
size--;
if (front == null) {
rear = null;
size--;
}
return null;
}
void deleteSecond(){
if(isEmpty()) {
System.out.println("Queue is empty!!");
return;
}
front.next = front.next.next;
}
public boolean isEmpty() {
return front == null;
}
int getSize(){
return size;
}
Object pop(){
if(isEmpty())
throw new IllegalStateException("Queue is empty!!!");
Object oldFront = front.data;
front =front.next;
size--;
return oldFront;
}
public Object peek() {
if (isEmpty()) {
throw new IllegalStateException("Queue is empty. Cannot peek.");
}
return front.data;
}
public void display() {
Node current = front;
while (current != null) {
System.out.println(current.data + " ");
current = current.next;
}
System.out.println();
}
void displayReverse(){
LinkedListStack stack = new LinkedListStack();
for(Node temp = front ; temp != null ; temp = temp.next)
stack.push(temp.data);
stack.display();
}
public String toString() {
StringBuilder stringBuilder = new StringBuilder("[");
for (Node temp = front; temp != null; temp = temp.next)
stringBuilder.append(temp.data + ", ");
stringBuilder.append("null ]");
return stringBuilder.toString();
}
Object[] toArray(){
Object[] array = new Object[getSize()];
Node temp = front;
for(int i=0 ; i<getSize() ; i++) {
array[i] = temp.data;
temp = temp.next;
}
return array;
}
public static void divideQueue(LinkedListQueue originalQueue, LinkedListQueue firstHalf, LinkedListQueue secondHalf) {
int size = originalQueue.getSize();
for (int i = 0; i < size / 2; i++)
firstHalf.enqueue(originalQueue.pop());
while (!originalQueue.isEmpty())
secondHalf.enqueue(originalQueue.pop());
}
Object findMiddle() {
Node hare = front;
Node turtle = front;
while (hare.next != null && hare.next.next != null) {
hare = hare.next.next;
turtle = turtle.next;
}
return turtle.data;
}
void mergeQueue(LinkedListQueue... lists) {
for (LinkedListQueue list : lists) {
if (front == null)
front = list.front;
else {
rear = front;
while (rear.next != null)
rear = rear.next;
rear.next = list.front;
}
}
}
public static void main(String[] args) {
var name = new LinkedListQueue();
name.enqueue("passenger1");
name.enqueue("passenger2");
name.enqueue("passenger3");
name.enqueue("passenger4");
var cast = new LinkedListQueue();
cast.enqueue("Soomro");
cast.enqueue("Brohi");
cast.enqueue("Bhatti");
cast.enqueue("Jamali");
var collection = new LinkedListQueue();
collection.mergeQueue(name , cast);
collection.display();
}
}