Code : Queue Using LL
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.
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.
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.
You are not required to print anything explicitly. It has already been taken care of. Just implement the functions.
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
7
1 17
1 23
1 11
2
2
2
2
17
23
11
-1
3
2
1 10
4
-1
1
#include <iostream>using namespace std;
class Node { public: int data; Node *next;
Node(int data) { this->data = data; next = NULL; }};
#include "solution.h"
int main() { Queue q;
int t; cin >> t;
while (t--) { int choice, input; cin >> choice; switch (choice) { case 1: cin >> input; q.enqueue(input); break; case 2: cout << q.dequeue() << "\n"; break; case 3: cout << q.front() << "\n"; break; case 4: cout << q.getSize() << "\n"; break; default: cout << ((q.isEmpty()) ? "true\n" : "false\n"); break; } }}
/************************************************************ Following is the structure of the node class class Node { public : int data; Node *next;
Node(int data) { this->data = data; next = NULL; } };
**************************************************************/
class Queue { // Define the data members Node *Front, *rear; int size; public: Queue() { // Implement the Constructor Front = rear = NULL; size = 0; } /*----------------- Public Functions of Stack -----------------*/
int getSize() { // Implement the getSize() function return size; }
bool isEmpty() { // Implement the isEmpty() function return Front == nullptr; }
void enqueue(int element) { // Implement the enqueue() function if(rear == nullptr) { Front = rear = new Node(element); } else { rear -> next = new Node(element); rear = rear -> next; } ++size; }
int dequeue() { // Implement the dequeue() function if(!isEmpty()) { Node *temp = Front; int element = temp -> data; Front = Front -> next; delete temp; --size; if(size == 0) { rear = nullptr; Front = nullptr; } return element; } return -1; }
int front() { // Implement the front() function if(!isEmpty()) { return Front -> data; } return -1; }};
Comments
Post a Comment