Data Structures: Question Set – 02
What is a queue data structure?
A queue is a linear data structure that gives users the ability to store items in a list in a methodical fashion. Users can store items in a queue in a variety of ways. When the queue has enough items, the items at the head of the queue are withdrawn, and the things at the back of the queue are added again. This process continues until the queue is empty. When customers want to hold goods for an extended amount of time, such as during the checkout process, queues are a typical organisational tool that can be utilised in a variety of settings. A line of people waiting for a resource is an excellent illustration of a queue; in this kind of line, the first client in line is served first.
List out a few applications of the queue
The following is a list of applications that make use of queue data structure:
- The breadth-first search algorithm in graphs
- Operating system: job scheduling operations, Disk scheduling, CPU scheduling etc.
- Call management in call centres
What are different operations possible in queue data structure?
- enqueue: This adds an element to the rear end of the queue. Overflow conditions occur if the queue is full.
- dequeue: This removes an element from the front end of the queue. Underflow conditions occur if the queue is empty.
- isEmpty: This returns true if the queue is empty or else false.
- isFull: This returns true if the queue is full or else false.
- rear: This returns the rear-end element without removing it.
- front: This returns the front-end element without removing it.
- size: This returns the size of the queue.
Differentiate: Stack vs Queue
|Stack is a linear data structure where data is added and removed from the top.||Queue is a linear data structure where data is ended at the rear end and removed from the front.|
|Stack is based on LIFO(Last In First Out) principle||Queue is based on FIFO(First In First Out) principle|
|Insertion operation in Stack is known as push.||Insertion operation in Queue is known as eneque.|
|Delete operation in Stack is known as pop.||Delete operation in Queue is known as dequeue.|
|Only one pointer is available for both addition and deletion: top()||Two pointers are available for addition and deletion: front() and rear()|
|Used in solving recursion problems||Used in solving sequential processing problems|
How should a stack be used to implement a queue?
It is possible to implement a queue by using two stacks. Let’s call the queue q, and the two stacks that will be used to implement q stack1 and stack2. We are aware that the stack offers operations known as push, pop, and peek, and with the help of these operations, we need to simulate the actions known as enqueue and dequeue that are performed by the queue. Therefore, queue q can be created using either of the following two approaches (the complexity of the auxiliary space required by both of these methods is O(n)):
By making enqueue operation costly:
- Here, the oldest element is always at the top of
stack1which ensures dequeue operation occurs in O(1) time complexity.
- To place the element at top of stack1, stack2 is used.
- Enqueue: Here time complexity will be O(n)
enqueue(q, data): While stack1 is not empty do: Push everything from stack1 to stack2. Push data to stack1 Push everything back to stack1. end
- Dequeue: Here time complexity will be O(1)
deQueue(q): If stack1 is empty then error else Pop an item from stack1 and return it
By making the dequeue operation costly:
- In this case, the enqueue action involves pushing the newly added element to the top of stack1. In this scenario, the temporal complexity of the enqueue operation is O. (1).
- If stack2 is empty when dequeue is being performed, all of the components from stack1 are moved to stack2, and the result is the top of stack2. In a nutshell, inverting the list by pushing items into a stack and then retrieving the first item that was enqueued. The complexity of this operation, which consists of placing all of the items onto a new stack, is O(n).
- Enqueue: Time complexity: O(1)
enqueue(q, data): Push data to stack1
- Dequeue: Time complexity: O(n)
dequeue(q): If both stacks are empty then raise error. If stack2 is empty: While stack1 is not empty do: push everything from stack1 to stack2. Pop the element from stack2 and return it. end
How do you implement stack using queues?
- A stack can be created by combining the functionality of two queues. We know that a queue supports enqueue and dequeue operations. It is necessary for us to build push-and-pop operations using these activities.
- Let stack be ‘s’ and queues utilised to construct be ‘q1’ and ‘q2’. Then, there are two possible approaches to implementing stack s:
By making push operation costly:
- This method ensures that the newly entered element is always at the front of ‘q1’ so that pop operation just dequeues from ‘q1’.
- ‘q2’ is used as the auxiliary queue to put every new element in front of ‘q1’ while ensuring pop happens in O(1) complexity.
- Push element to stack s: Here push takes O(n) time complexity.
push(s, data): Enqueue data to q2 Dequeue elements one by one from q1 and enqueue to q2. Swap the names of q1 and q2
- Pop element from stack s: Takes O(1) time complexity.
pop(s): dequeue from q1 and return it.
By making pop operation costly:
- In push operation, the element is enqueued to q1.
- In pop operation, all the elements from q1 except the last remaining element, are pushed to q2 if it is empty. That last element remaining of q1 is dequeued and returned.
- Push element to stack s: Here push takes O(1) time complexity.
push(s,data): Enqueue data to q1
- Pop element from stack s: Takes O(n) time complexity.
pop(s): Step 1: Dequeue every elements except the last element from q1 and enqueue to q2. Step 2: Dequeue the last item of q1, the dequeued item is stored in result variable. Step 3: Swap the names of q1 and q2 (for getting updated data after dequeue) Step 4: Return the result.
Explain the various types of arrays that are used as data structures.
Arrays can be broken down into the following categories:
- One-dimensional array: The elements of a one-dimensional array are stored in contiguous memory regions, and a single index value is used to access any given element in the array. It is a data structure that is linear and holds all of the items in sequential order.
- Two-dimensional array: A two-dimensional array is a tabular array that stores data and comprises rows and columns. This type of array is called a two-dimensional array. To generate a two-dimensional array with the dimensions M by N, you must first group the array’s M rows and N columns into N columns and rows.
- A three-dimensional array is a grid that has rows, columns, and depth as a third dimension. This type of grid is referred to as a three-dimensional array. It is structured in the form of a cube with rows, columns, and depth as the third dimension. The position of an element in the three-dimensional array can be represented by one of three subscripts: the row, the column, or the depth. The first index is the depth (also known as the dimension or layer), the second index is the row index, and the third index is the column index.
What exactly is meant by the term “linked list data structure”?
One way to think of a linked list is as a collection of nodes (also called items) that are connected to one another by links (or paths). Every entry in the linked list is represented by a link, and every link in the linked list points to the following node in the sequence. The order in which nodes are generated is taken into consideration to determine the order in which they are put to the list.
What are the applications for the Linked list?
- Stack, Queue, binary trees, and graphs are implemented using linked lists.
- Dynamic management for Operating System memory.
- Round-robin scheduling for operating system tasks.
- Forward and backward operation in the browser.