-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkedListQueue.java
225 lines (205 loc) · 5.33 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
interface IQueue {
/*** Inserts an item at the queue front.*/
public void enqueue(Object item);
/*** Removes the object at the queue rear and returnsit.*/
public Object dequeue();
/*** Tests if this queue is empty.*/
public boolean isEmpty();
/*** Returns the number of elements in the queue*/
public int size();
/** print the queue*/
public void printQueue();
}
public class LinkedListQueue implements IQueue {
DoubleLinkedList Q = new DoubleLinkedList();
public void enqueue(Object item){
Q.add(item);
}
public Object dequeue(){
Object temp = Q.get(0);
Q.remove(0);
return temp;
}
public boolean isEmpty(){
return Q.isEmpty();
}
public int size(){
return Q.size();
}
public void printQueue(){
Q.printList();
}
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
LinkedListQueue Q = new LinkedListQueue();
Scanner sc = new Scanner(System.in);
String st = sc.nextLine().replaceAll("\\[|\\]", "");
String[] s = st.split(", ");
if (!(s.length == 1 && s[0].isEmpty())){
for(int i = s.length - 1; i >= 0; i--){
Object o = Integer.parseInt(s[i]);
Q.enqueue(o);
}
}
String keyword = sc.nextLine();
if(Objects.equals(keyword, "enqueue")){
int element = sc.nextInt();
Q.enqueue(element);
Q.printQueue();
}
if(Objects.equals(keyword, "dequeue")){
if(Q.isEmpty()){
System.out.println("Error");
}
else{
Q.dequeue();
Q.printQueue();
}
}
if(Objects.equals(keyword, "isEmpty")){
if(Q.isEmpty())
System.out.println("True");
else
System.out.println("False");
}
if(Objects.equals(keyword, "size")){
System.out.println(Q.size());
}
}
}
interface ILinkedList {
/**
* Inserts the specified element at the end of the list.
* @param element
*/
public void add(Object element);
/**
* @param index
* @return the element at the specified position in this list.
*/
public Object get(int index);
/**
* @return true if this list contains no elements.
*/
public boolean isEmpty();
/**
* Removes the element at the specified position in this list.
* @param index
*/
public void remove(int index);
/**
* @return the number of elements in this list.
*/
public int size();
/**
* print the list
*/
public void printList();
}
class DoubleLinkedList implements ILinkedList {
public int length = 0;
public static boolean flag;
public class Node {
Object item;
Node next;
Node prev;
}
public Node head = null;
public Node tail = null;
public void printList() {
Node current = tail;
if(head == null) {
System.out.println("[]");
}
else {
System.out.print("[" + current.item);
current = current.prev;
while (current != null) {
System.out.print(", " + current.item);
current = current.prev;
}
System.out.println("]");
}
}
public void add(Object element) {
Node newNode = new Node();
newNode.item = element;
if (length == 0) {
newNode.next =newNode.prev= null;
head = tail = newNode;
}
else {
newNode.next = null;
newNode.prev =tail;
tail.next = newNode;
tail = newNode;
}
length++;
}
public Object get(int index) {
if(index < 0 || index >= length) {
return 0;
}
else if(index == 0) {
return head.item;
}
else if(index == length - 1) {
return tail.item;
}
else {
Node current = head;
for(int i = 0; i < index; i++) {
current = current.next;
}
return current.item;
}
}
public boolean isEmpty() {
if (length == 0) {
return true;
}
else {
return false;
}
}
public void remove(int index) {
flag = true;
Node current = head;
if(index < 0 || index >= length) {
flag = false;
}
else if(length == 1) {
head = tail = null;
length--;
}
else if (index == 0) {
head = head.next;
head.prev=null;
length--;
}
else if (index == length - 1) {
for(int i = 0; i < index - 1; i++) {
current = current.next;
}
current.next = null;
tail = current;
length--;
}
else {
for(int i = 0; i < index - 1; i++) {
current = current.next;
}
current.next = current.next.next;
current.next.next.prev= current;
length--;
}
}
public int size() {
return length;
}
}