|
| 1 | +Implement a Queue Data Structure specifically to store integer data using a Singly Linked List. |
| 2 | +The data members should be private. |
| 3 | +You need to implement the following public functions : |
| 4 | +1. Constructor: |
| 5 | +It initialises the data members as required. |
| 6 | +2. enqueue(data) : |
| 7 | +This function should take one argument of type integer. It enqueues the element into the queue and returns nothing. |
| 8 | +3. dequeue() : |
| 9 | +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. |
| 10 | +4. front() : |
| 11 | +It returns the element being kept at the front of the queue. In case the queue is empty, it returns -1. |
| 12 | +5. getSize() : |
| 13 | +It returns the size of the queue at any given instance of time. |
| 14 | +6. isEmpty() : |
| 15 | +It returns a boolean value indicating whether the queue is empty or not. |
| 16 | +Operations Performed on the Stack: |
| 17 | +Query-1(Denoted by an integer 1): Enqueues an integer data to the queue. |
| 18 | + |
| 19 | +Query-2(Denoted by an integer 2): Dequeues the data kept at the front of the queue and returns it to the caller. |
| 20 | + |
| 21 | +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. |
| 22 | + |
| 23 | +Query-4(Denoted by an integer 4): Returns the current size of the queue. |
| 24 | + |
| 25 | +Query-5(Denoted by an integer 5): Returns a boolean value denoting whether the queue is empty or not. |
| 26 | +Input Format: |
| 27 | +The first line contains an integer 'q' which denotes the number of queries to be run against each operation on the queue. |
| 28 | +Then the test cases follow. |
| 29 | + |
| 30 | +Every 'q' lines represent an operation that needs to be performed. |
| 31 | + |
| 32 | +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. |
| 33 | + |
| 34 | +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. |
| 35 | +Output Format: |
| 36 | +For Query-1, you do not need to return anything. |
| 37 | +For Query-2, prints the data being dequeued from the queue. |
| 38 | +For Query-3, prints the data kept on the front of the queue. |
| 39 | +For Query-4, prints the current size of the queue. |
| 40 | +For Query-5, prints 'true' or 'false'(without quotes). |
| 41 | + |
| 42 | +Output for every query will be printed in a separate line. |
| 43 | + Note: |
| 44 | +You are not required to print anything explicitly. It has already been taken care of. Just implement the functions. |
| 45 | +Constraints: |
| 46 | +1 <= q <= 10^5 |
| 47 | +1 <= x <= 5 |
| 48 | +-2^31 <= data <= 2^31 - 1 and data != -1 |
| 49 | + |
| 50 | +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. |
| 51 | + |
| 52 | +Time Limit: 1 second |
| 53 | +Sample Input 1: |
| 54 | +7 |
| 55 | +1 17 |
| 56 | +1 23 |
| 57 | +1 11 |
| 58 | +2 |
| 59 | +2 |
| 60 | +2 |
| 61 | +2 |
| 62 | +Sample Output 1: |
| 63 | +17 |
| 64 | +23 |
| 65 | +11 |
| 66 | +-1 |
| 67 | +Sample Input 2: |
| 68 | +3 |
| 69 | +2 |
| 70 | +1 10 |
| 71 | +4 |
| 72 | +Sample Output 2: |
| 73 | +-1 |
| 74 | +1 |
| 75 | + |
| 76 | +********************************************************Code************************************************ |
| 77 | +public class Queue { |
| 78 | + |
| 79 | + private Node front; |
| 80 | + private Node rear; |
| 81 | + private int size; |
| 82 | + |
| 83 | + public Queue() { |
| 84 | + |
| 85 | + front=null; |
| 86 | + rear=null; |
| 87 | + size=0; |
| 88 | + } |
| 89 | + public int getSize() { |
| 90 | + |
| 91 | + return size; |
| 92 | + } |
| 93 | + |
| 94 | + public boolean isEmpty() { |
| 95 | + |
| 96 | + return size==0; |
| 97 | + } |
| 98 | + |
| 99 | + public void enqueue(int data) { |
| 100 | + |
| 101 | + Node ele= new Node(data); |
| 102 | + if(front==null) |
| 103 | + { |
| 104 | + front=ele; |
| 105 | + rear=ele; |
| 106 | + } |
| 107 | + else{ |
| 108 | + rear.next=ele; |
| 109 | + rear=ele; |
| 110 | + } |
| 111 | + size++; |
| 112 | + } |
| 113 | + |
| 114 | + |
| 115 | + public int dequeue() { |
| 116 | + |
| 117 | + if(front!=null) |
| 118 | + { |
| 119 | + int temp=front.data; |
| 120 | + front=front.next; |
| 121 | + size--; |
| 122 | + return temp; |
| 123 | + } |
| 124 | + else{ |
| 125 | + return -1; |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + |
| 130 | + public int front() { |
| 131 | + |
| 132 | + if(front!=null) |
| 133 | + { |
| 134 | + return front.data; |
| 135 | + } |
| 136 | + else |
| 137 | + return -1; |
| 138 | + } |
| 139 | + } |
| 140 | + |
0 commit comments