-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueueUsingLL.java
More file actions
164 lines (142 loc) · 4.44 KB
/
QueueUsingLL.java
File metadata and controls
164 lines (142 loc) · 4.44 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
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
// Queue Using LL
// Send Feedback
// Implement a Queue Data Structure specifically to store integer data using a
// Singly Linked List.
// The data members should be private.
// You need to implement the following public functions :
// 1. Constructor:
// It initialises the data members as required.
// 2. enqueue(data) :
// This function should take one argument of type integer. It enqueues the
// element into the queue and returns nothing.
// 3. dequeue() :
// It dequeues/removes the element from the front of the queue and in turn,
// returns the element being dequeued or removed. In case the queue is empty, it
// returns -1.
// 4. front() :
// It returns the element being kept at the front of the queue. In case the
// queue is empty, it returns -1.
// 5. getSize() :
// It returns the size of the queue at any given instance of time.
// 6. isEmpty() :
// It returns a boolean value indicating whether the queue is empty or not.
// Operations Performed on the Stack:
// Query-1(Denoted by an integer 1): Enqueues an integer data to the queue.
// Query-2(Denoted by an integer 2): Dequeues the data kept at the front of the
// queue and returns it to the caller.
// Query-3(Denoted by an integer 3): Fetches and returns the data being kept at
// the front of the queue but doesn't remove it, unlike the dequeue function.
// Query-4(Denoted by an integer 4): Returns the current size of the queue.
// Query-5(Denoted by an integer 5): Returns a boolean value denoting whether
// the queue is empty or not.
// Input Format:
// The first line contains an integer 'q' which denotes the number of queries to
// be run against each operation on the queue.
// Then the test cases follow.
// Every 'q' lines represent an operation that needs to be performed.
// For the enqueue operation, the input line will contain two integers separated
// by a single space, representing the type of the operation in integer and the
// integer data being enqueued into the queue.
// For the rest of the operations on the queue, the input line will contain only
// one integer value, representing the query being performed on the queue.
// Output Format:
// For Query-1, you do not need to return anything.
// For Query-2, prints the data being dequeued from the queue.
// For Query-3, prints the data kept on the front of the queue.
// For Query-4, prints the current size of the queue.
// For Query-5, prints 'true' or 'false'(without quotes).
// Output for every query will be printed in a separate line.
// Note:
// You are not required to print anything explicitly. It has already been taken
// care of. Just implement the functions.
// Constraints:
// 1 <= q <= 10^5
// 1 <= x <= 5
// -2^31 <= data <= 2^31 - 1 and data != -1
// Where 'q' is the total number of queries being performed on the queue, 'x' is
// the range for every query and data represents the integer pushed into the
// queue.
// Time Limit: 1 second
// Sample Input 1:
// 7
// 1 17
// 1 23
// 1 11
// 2
// 2
// 2
// 2
// Sample Output 1:
// 17
// 23
// 11
// -1
// Sample Input 2:
// 3
// 2
// 1 10
// 4
// Sample Output 2:
// -1
// 1
class Node {
int data;
Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
class Queue {
// Define the data members
private Node front;
private Node rear;
private int size;
public Queue() {
// Implement the Constructor
front = null;
rear = null;
size = 0;
}
/*----------------- Public Functions of Stack -----------------*/
public int getSize() {
// Implement the getSize() function
return size;
}
public boolean isEmpty() {
// Implement the isEmpty() function
return size == 0;
}
public void enqueue(int data) {
// Implement the enqueue(element) function
Node newNode = new Node(data);
size++;
if (rear == null) {
front = newNode;
rear = newNode;
} else {
rear.next = newNode;
rear = newNode;
}
}
public int dequeue() {
// Implement the dequeue() function
if (front == null) {
return -1;
}
int temp = front.data;
front = front.next;
size--;
if (front == null) {
rear = null;
}
return temp;
}
public int front() {
// Implement the front() function
if (front == null) {
return -1;
}
return front.data;
}
}