0% found this document useful (0 votes)
32 views64 pages

Unit 2

The document covers data structures and algorithms, focusing on stacks and queues, their definitions, operations, implementations, and applications. It explains the LIFO principle of stacks, including operations like push and pop, and discusses various types of queues and their FIFO nature. Additionally, it highlights the advantages and disadvantages of both data structures, along with their real-world applications in programming and memory management.

Uploaded by

provokingdiaries
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views64 pages

Unit 2

The document covers data structures and algorithms, focusing on stacks and queues, their definitions, operations, implementations, and applications. It explains the LIFO principle of stacks, including operations like push and pop, and discusses various types of queues and their FIFO nature. Additionally, it highlights the advantages and disadvantages of both data structures, along with their real-world applications in programming and memory management.

Uploaded by

provokingdiaries
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 64

DATA STRUCTURES AND ALGORITHMS

Venkata Ramana S
Assistant Professor
CSE,MRECW

MALLA REDDY ENGINEERING COLLEGE FOR


WOMEN (2405ES02) DATA STRUCTURES AND
Data Structures & Algorithms

UNIT-II Stacks and Queues using C& Python: ADT Stack and its operations:
Algorithms and their complexity analysis, Applications of Stacks: Expression
Conversion and evaluation – corresponding algorithms and complexity analysis.
ADT Queue: Types of Queue: Simple Queue, Circular Queue, Priority Queue.
Double ended Queue and Operations on each types of Queues and Algorithms.
Applications of queues.
What is a Stack ?
 A Stack is a linear data structure that follows the LIFO (Last-
In-First-Out) principle. Stack has one end, whereas the Queue
has two ends (front and rear).
 It contains only one pointer top pointer pointing to the
topmost element of the stack.
 Whenever an element is added in the stack, it is added on the
top of the stack, and the element can be deleted only from the
stack.
 In other words, a stack can be defined as a container in
which insertion and deletion can be done from the one
end known as the top of the stack.
4

Stacks ADT implemented

 Stacks can be implemented with the help of contiguous memory, an


Array, and non-contiguous memory, a Linked List.
 Real-life examples of Stacks are piles of books, a deck of cards, piles
of money, and many more.
Operations in the Stack ADT
 The primary operations in the Stack are as follows:
 push(): When we insert an element in a stack then the operation is known as a push. If the stack
is full then the overflow condition occurs.
 pop(): When we delete an element from the stack, the operation is known as a pop. If the stack is
empty means that no element exists in the stack, this state is known as an underflow state.
 isEmpty(): It determines whether the stack is empty or not.
 isFull(): It determines whether the stack is full or not.'
 peek(): It returns the element at the given position.
 count(): It returns the total number of elements available in a stack.
 change(): It changes the element at the given position.
 display(): It prints all the elements available in the stack.
6

Key points related to stack


Some key points related to stack

 It is called as stack because it behaves like a real-world stack, piles of books,


etc.
 A Stack is an abstract data type with a pre-defined capacity, which means that
it can store the elements of a limited size.
 It is a data structure that follows some order to insert and delete the elements,
and that order can be LIFO or FILO.
7

Applications of Stacks
Some Applications of Stacks:
 Balancing of symbols: Stack is used for balancing a symbol.
 String reversal: Stack is also used for reversing a string. For example, we
want to reverse a “RamSita" string, so we can achieve this with the help of a
stack.
First, we push all the characters of the string in a stack until we reach the
null character.
After pushing all the characters, we start taking out the character one by
one until we reach the bottom of the stack.
 UNDO/REDO: One of the most common uses of Stack in programming is to
implement undo and redo functionality in text editors and other applications.
When the user types text, each keystroke is pushed onto a stack. When the
user hits undo, the most recently pushed text is popped off the Stack and
removed from display. Redo pops the item back onto the Stack. As Stack
maintains LIFO order, the text removed last is undone first. Visual editors
like Photoshop also use it for undo/redo image edits.
 Recursion: The recursion means that the function is calling itself again. To
maintain the previous states, the compiler creates a system stack in which
all the previous records of the function are maintained.
8

Applications of Stacks
 Backtracking: Suppose we have to create a path to solve a maze problem. If we
are moving in a particular path, and we realize that we come on the wrong way.
In order to come at the beginning of the path to create a new path, we have to
use the stack data structure.
 Expression conversion: Stack can also be used for expression conversion. This
is one of the most important applications of stack. The list of the expression
conversion is given below:
I. Infix to prefix
II. Infix to postfix
III.Prefix to infix
IV. Prefix to postfix
V. Postfix to infix
 Memory management: The stack manages the memory. The memory is assigned
in the contiguous memory blocks. The memory is known as stack memory as all
the variables are assigned in a function call stack memory. The memory size
assigned to the program is known to the compiler. When the function is
created, all its variables are assigned in the stack memory. When the function
Working of Stack
 Stack works on the LIFO pattern. As we can observe in the below
figure there are five memory blocks in the stack; therefore, the size
of the stack is 5.
 Suppose we want to store the elements in a stack and let's assume
that stack is empty. We have taken the stack of size 5 as shown
below in which we are pushing the elements one by one until the
 Since our stack is full as the size of the stack is
stack becomes full.
5. In the above cases, we can observe that it
goes from the top to the bottom when we were
entering the new element in the stack. The
stack gets filled up from the bottom to the top.

 When we perform the delete operation on the


stack, there is only one way for entry and exit as
the other end is closed. It follows the LIFO
pattern, which means that the value entered
first will be removed last. In the above case, the
value 1 is entered first, so it will be removed
only after the deletion of all the other elements.
PUSH operation
 The steps involved in the PUSH operation is
given below:
1. Before inserting an element in a stack, we check
whether the stack is full.
2. If we try to insert the element in a stack, and the
stack is full, then the overflow condition occurs.
3. When we initialize a stack, we set the value of top as
-1 to check that the stack is empty.
4. When the new element is pushed in a stack, first, the
value of the top gets incremented,
i.e., top=top+1, and the element will be placed at the
new position of the top.
5. The elements will be inserted until we reach
the max size of the stack.
 POP operation
 The steps involved in the POP operation is given
below:
1. Before deleting the element from the stack, we check whether
the stack is empty.
2. If we try to delete the element from the empty stack, then
the underflow condition occurs.
3. If the stack is not empty, we first access the element which is
pointed by the top
4. Once the pop operation is performed, the top is decremented
by 1, i.e., top=top-1.
Stack Time Complexity
 Push Operation: O(1)
 Pop Operation: O(1)
 Top Operation: O(1)
 Search Operation: O(n)
Linked list implementation of stack

 Instead of using array, we can also use linked list to implement


stack. Linked list allocates the memory dynamically.
 However, time complexity in both the scenario is same for all
the operations i.e. push, pop and peek.
 In linked list implementation of stack, the nodes are
maintained non-contiguously in the memory.
 Each node contains a pointer to its immediate successor node
in the stack.
 Stack is said to be overflown if the space left in the memory
heap is not enough to create a node.
 The top most node in the stack always contains null in its
address field
Adding a node to the stack (Push
 operation)
Adding a node to the stack is referred to as push operation.
Pushing an element to a stack in linked list implementation is
different from that of an array implementation. In order to
push an element onto the stack, the following steps are
involved.
1. Create a node first and allocate memory to it.
2. If the list is empty then the item is to be pushed as the start
node of the list. This includes assigning value to the data part
of the node and assign null to the address part of the node.
3. If there are some nodes in the list already, then we have to
add the new element in the beginning of the list (to not violate
the property of the stack). For this purpose, assign the
address of the starting element to the address field of the new
node and make the new node, the starting node of the list.
 Advantages of Stacks
 Last In, First Out (LIFO):
 Stacks follow the LIFO principle, which is useful in scenarios like undo mechanisms in text editors or backtracking
algorithms.
 Memory Management:
 Stacks are used for managing function calls and local variables, helping with memory organization and allocation.
 Simplicity:
 Stacks are simple to implement and use. Operations like push (add) and pop (remove) are straightforward.
 Reversing Data:
 Stacks can be used to reverse data, such as reversing a string or a list.
 Expression Evaluation:
 Stacks are essential for evaluating and parsing expressions, particularly in compilers and calculators.
 Backtracking:
 Stacks are ideal for backtracking algorithms, such as finding paths in mazes or solving puzzles like the Tower of
Hanoi.
 Disadvantages of Stacks
 Limited Access:
 You can only access the top element of the stack. This restricted access can be limiting for certain applications.
 Fixed Size (Static Stacks):
 Static stacks, typically implemented with arrays, have a fixed size, leading to overflow if the stack becomes full and
underutilization if the stack is not fully used.
 Memory Overhead:
 In dynamic stacks (like those implemented with linked lists), each element requires additional memory for pointers.
 No Direct Access to Elements:
 Unlike arrays or linked lists, you cannot directly access or modify elements in the middle of the stack without removing
the elements above them.
 Potential for Overflow:
 Stacks can overflow if there are too many elements pushed onto them, especially in environments with limited
memory.
 Performance Issues with Large Stacks:
 Managing very large stacks can be challenging and may lead to performance degradation if not handled properly.
Algebraic Expressions
Unit-2

1. Conversion from post fix to infix:


Procedure to convert postfix expression to infix expression is as follows: Scan
the postfix expression from left to right. If the scanned symbol is an operand, then
push it on to the stack. If the scanned symbol is an operator, pop two symbols
from the stack and create it as a string by placing the operator in between the
operands and push it on to the stack. Repeat steps 2 and 3 till the end of the
expression
Precedence Type Operators Associativity
1 Postfix () [] -> . ++ — Left to Right
2 Unary + – ! ~ ++ — (type)* & sizeof Right to Left
3 Multiplicative */% Left to Right
4 Additive +– Left to Right
5 Shift <<, >> Left to Right
6 Relational < <= > >= Left to Right
7 Equality == != Left to Right
8 Bitwise AND & Left to Right
9 Bitwise XOR ^ Left to Right
10 Bitwise OR | Left to Right
11 Logical AND && Left to Right
12 Logical OR || Left to Right
13 Conditional ?: Right to Left
14 Assignment = += -+ *= /= %= >>= <<= &= ^= |= Right to Left
15 Comma , Left to Right
Conversion from infix to postfix
 Procedure to convert from infix expression to postfix expression is as follows: Scan the infix expression from left to
right.
Conversion from infix to prefix
The precedence rules for converting an expression from infix to prefix are identical.
The only change from postfix conversion is that traverse the expression from right to left and the operator is placed before the operands rather than after them.
The prefix form of a complex expression is not the mirror image of the postfix form.
Unit-2
Postfix to infix conversion
Unit-2
Postfix to infix conversion
Queue
 1. A queue can be defined as an ordered list which enables insert
operations to be performed at one end called REAR and delete
operations to be performed at another end called FRONT.
 2. Queue is referred to be as First In First Out list.
 3. For example, people waiting in line for a rail ticket form a queue.
Data Structures & Algorithms

Applications of Queue
There are various applications of queues discussed as below.

1. Queues are widely used as waiting lists for a single shared resource like
printer, disk, CPU.
2. Queues are used in asynchronous transfer of data (where data is not
being transferred at the same rate between two processes) for eg. pipes,
file IO, sockets.
3. Queues are used as buffers in most of the applications like MP3 media
player, CD player, etc.
4. Queue are used to maintain the play list in media players in order to add
and remove the songs from the play-list.
5. Queues are used in operating systems for handling interrupts.
Operations associated with queues
The main operations associated with
queues are:
o Enqueue: Add an element to the back of the queue.
o Dequeue: Remove an element from the front of the
queue.
o IsEmpty: Check if the queue is empty.
o IsFull: Check if the queue is full.
o Peek: Get the value of the front element without
removing it.
Algorithms:
Queues are based on the FIFO algorithm. The steps are:
o Enqueue: Add an item to the back of the queue, increase the rear
pointer
o Dequeue: Remove the item from the front of the queue, increase
the front pointer
o Check if the rear pointer has reached the end of the queue
capacity
o Check if the front pointer equals the rear (empty queue)
Data Structures & Algorithms
Advantages of Queues Disadvantages of Queues
1. Maintains Order: 1. Limited Access:
o You can only access the front and rear elements, not
o Elements are processed in the order
the middle ones.
they are added (First In, First Out). 2. Memory Usage:
2. Fairness: o Dynamic queues (like linked lists) use extra memory
o Ensures every element gets processed for pointers.

in the order it arrives. 3. Blocking Issues:


o If the queue is full, new tasks might have to wait.
3. Resource Management: 4. Inefficient for Some Operations:
o Efficiently manages tasks and o Searching or modifying middle elements is slow and
resources, such as print jobs or CPU inefficient.
tasks. 5. Fixed Size (Static Queues):
o Static queues (like arrays) have a fixed size, which can
4. Easy to Implement:
lead to overflow or wasted space.
o Simple to code and understand.
6. Performance Issues:
5. Concurrency: o Circular queues can become slow if they need
o Helps manage tasks between different frequent adjustments to manage space.
threads in programming.
Data Structures & Algorithms

Time Complexity

Data Time Complexity Space


Structure Compleity

Average Worst Worst

Access Search Insertion Deletion Access Search Insertion Deletion

Queue θ(n) θ(n) θ(1) θ(1) O(n) O(n) O(1) O(1) O(n)
Data Structures & Algorithms
Data Structures & Algorithms

Simple Queue or Linear Queue


 In Linear Queue, an insertion takes place from one end while the deletion
occurs from another end.
 The end at which the insertion takes place is known as the rear end, and the
end at which the deletion takes place is known as front end. It strictly follows
the FIFO rule.

 The major drawback of using a linear Queue is that insertion is done only from
the rear end.
 If the first three elements are deleted from the Queue, we cannot insert more
elements even though the space is available in a Linear Queue. In this case, the
linear Queue shows the overflow condition as the rear is pointing to the last
element of the Queue.
Data Structures & Algorithms

Circular Queue
 In Circular Queue, all the nodes are represented as circular. It is similar to the
linear Queue except that the last element of the queue is connected to the
first element.
 It is also known as Ring Buffer, as all the ends are connected to another end.
The representation of circular queue is shown in the below image -

 The drawback that occurs in a linear queue is overcome by using the circular
queue.
 If the empty space is available in a circular queue, the new element can be
added in an empty space by simply incrementing the value of rear.
 The main advantage of using the circular queue is better memory utilization.
Data Structures & Algorithms

Why was the concept of the circular queue introduced?


There was one limitation in the array implementation of Queue. If the rear
reaches to the end position of the Queue then there might be possibility that
some vacant spaces are left in the beginning which cannot be utilized. So, to
overcome such limitations, the concept of the circular queue was introduced.
Data Structures & Algorithms

Why was the concept of the circular queue


introduced?
 As we can see in the above image, the rear is at the
last position of the Queue and front is pointing
somewhere rather than the 0th position.
 In the above array, there are only two elements and
other three positions are empty.
 The rear is at the last position of the Queue; if we try
to insert the element then it will show that there are
no empty spaces in the Queue.
 There is one solution to avoid such wastage of
memory space by shifting both the elements at the
left and adjust the front and rear end accordingly.
 It is not a practically good approach because shifting
all the elements will consume lots of time.
 The efficient approach to avoid the wastage of the
memory is to use the circular queue data structure.
 Operations on Circular Queue
 The following are the operations that can be performed on a circular
queue:
 Front: It is used to get the front element from the Queue.
 Rear: It is used to get the rear element from the Queue.
 enQueue(value): This function is used to insert the new value in the
Queue. The new element is always inserted from the rear end.
 deQueue(): This function deletes an element from the Queue. The
deletion in a Queue always takes place from the front end.
 Applications of Circular Queue
 The circular Queue can be used in the following scenarios:
 Memory management: The circular queue provides memory management.
As we have already seen that in linear queue, the memory is not managed
very efficiently. But in case of a circular queue, the memory is managed
efficiently by placing the elements in a location which is unused.
 CPU Scheduling: The operating system also uses the circular queue to insert
the processes and then execute them.
 Traffic system: In a computer-control traffic system, traffic light is one of the
best examples of the circular queue. Each light of traffic light gets ON one by
one after every jinterval of time. Like red light gets ON for one minute then
yellow light for one minute and then green light. After green light, the red light
gets ON.
Enqueue operation
The steps of enqueue operation are given
below:
o First, we will check whether the Queue is full or not.
o Initially the front and rear are set to -1. When we
insert the first element in a Queue, front and rear
both are set to 0.
o When we insert a new element, the rear gets
Dequeue Operation
incremented, i.e., rear=rear+1.
The steps of dequeue operation are given below:
o First, we check whether the Queue is empty or not. If the
queue is empty, we cannot perform the dequeue
operation.
o When the element is deleted, the value of front gets
decremented by 1.
o If there is only one element left which is to be deleted,
then the front and rear are reset to -1.
Priority Queue
 It is a special type of queue in which the elements are arranged based
on the priority. It is a special type of queue data structure in which
every element has a priority associated with it.
 Suppose some elements occur with the same priority, they will be
arranged according to the FIFO principle. The representation of
priority queue is shown in the below image -
Priority queue is mainly used to implement the CPU scheduling
algorithms.
There are two types of priority queue that are discussed as follows -
 Ascending priority queue - In ascending priority queue, elements can be
inserted in arbitrary order, but only smallest can be deleted first. Suppose
an array with elements 7, 5, and 3 in the same order, so, insertion can be
done with the same sequence, but the order of deleting the elements is 3,
5, 7.
 Descending priority queue - In descending priority queue, elements can
be inserted in arbitrary order, but only the largest element can be deleted
first. Suppose an array with elements 7, 3, and 5 in the same order, so,
insertion can be done with the same sequence, but the order of deleting
the elements is 7, 5, 3.
Characteristics of a Priority queue
A priority queue is an extension of a queue that contains the
following characteristics:
o very element in a priority queue has some priority associated with it.
o An element with the higher priority will be deleted before the deletion
of the lesser priority.
o If two elements in a priority queue have the same priority, they will be
arranged using the FIFO principle.
 Let's understand the priority queue through an example.
 We have a priority queue that contains the following values:
 1, 3, 4, 8, 14, 22
 All the values are arranged in ascending order. Now, we will observe how the
priority queue will look after performing the following operations:
o poll(): This function will remove the highest priority element from the priority queue.
In the above priority queue, the '1' element has the highest priority, so it will be
removed from the priority queue.
o add(2): This function will insert '2' element in a priority queue. As 2 is the smallest
element among all the numbers so it will obtain the highest priority.
o poll(): It will remove '2' element from the priority queue as it has the highest priority
queue.
o add(5): It will insert 5 element after 4 as 5 is larger than 4 and lesser than 8, so it will
obtain the third highest priority in a priority queue.
 Types of Priority Queue
 There are two types of priority queue:
 Ascending order priority queue: In ascending order priority queue, a
lower priority number is given as a higher priority in a priority. For
example, we take the numbers from 1 to 5 arranged in an ascending
order like 1,2,3,4,5; therefore, the smallest number, i.e., 1 is given as
the highest priority in a priority queue.
 Descending order priority queue: In descending order priority
queue, a higher priority number is given as a higher priority in a priority.
For example, we take the numbers from 1 to 5 arranged in descending
order like 5, 4, 3, 2, 1; therefore, the largest number, i.e., 5 is given as
the highest priority in a priority queue.
 Representation of priority queue
 Now, we will see how to represent the priority queue through a one-way
list.
 We will create the priority queue by using the list given below in
which INFO list contains the data elements, PRN list contains the
priority numbers of each data element available in the INFO list, and
LINK basically contains the address of the next node.
In the case of priority queue, lower priority number is considered the
higher priority, i.e., lower priority number = higher priority.
Step 1: In the list, lower priority number is 1, whose data value is 333, so it
will be inserted in the list as shown in the below diagram:
Step 2: After inserting 333, priority number 2 is having a higher priority, and
data values associated with this priority are 222 and 111. So, this data will be
inserted based on the FIFO principle; therefore 222 will be added first and
then 111.
Step 3: After inserting the elements of priority 2, the next higher priority
number is 4 and data elements associated with 4 priority numbers are 444,
555, 777. In this case, elements would be inserted based on the FIFO
principle; therefore, 444 will be added first, then 555, and then 777.
Step 4: After inserting the elements of priority 4, the next higher priority
number is 5, and the value associated with priority 5 is 666, so it will be
inserted at the end of the queue.
Deque (or, Double Ended Queue)
 In Deque or Double Ended Queue, insertion and deletion can be done from
both ends of the queue either from the front or rear. It means that we can
insert and delete elements from both front and rear ends of the queue.
 Deque can be used as a palindrome checker means that if we read the string
from both ends, then the string would be the same.
 Deque can be used both as stack and queue as it allows the insertion and
deletion operations on both ends.
 Deque can be considered as stack because stack follows the LIFO (Last In
First Out) principle in which insertion and deletion both can be performed
only from one end.
 And in deque, it is possible to perform both insertion and deletion from one
end, and Deque does not follow the FIFO principle.
There are two types of deque that are discussed as follows -
Input restricted deque - As the name implies, in input restricted queue,
insertion operation can be performed at only one end, while deletion can be
performed from both

o Output restricted deque - As the name implies, in output restricted queue,


deletion operation can be performed at only one end, while insertion can be
performed from both ends.
Operations performed on queue
The fundamental operations that can be performed on queue are listed as follows
-
o Enqueue: The Enqueue operation is used to insert the element at the rear end of the
queue. It returns void.
o Dequeue: It performs the deletion from the front-end of the queue. It also returns the
element which has been removed from the front-end. It returns an integer value.
o Peek: This is the third operation that returns the element, which is pointed by the front
pointer in the queue but does not delete it.
o Queue overflow (isfull): It shows the overflow condition when the queue is
completely full.
o Queue underflow (isempty): It shows the underflow condition when the Queue is
empty, i.e., no elements are in the Queue.
Ways to implement the queue
There are two ways of implementing the Queue:
o Implementation using array: The sequential allocation in a
Queue can be implemented using an array.
o Implementation using Linked list: The linked list allocation in a
Queue can be implemented using a linked list. For more details,
click on the below
 Operations performed on deque
 There are the following operations that can be applied on a deque -
 Insertion at front
 Insertion at rear
 Deletion at front
 Deletion at rear
We can also perform peek operations in the deque along with the operations listed
above. Through peek operation, we can get the deque's front and rear elements of the
deque. So, in addition to the above operations, following operations are also supported in
deque -
 Get the front item from the deque
 Get the rear item from the deque
 Check whether the deque is full or not
 Checks whether the deque is empty or not
 Insertion at the front end
 In this operation, the element is inserted from the front end of the
queue. Before implementing the operation, we first have to check
whether the queue is full or not. If the queue is not full, then the
element can be inserted from the front end by using the below
conditions -
 If the queue is empty, both rear and front are initialized with 0. Now,
both will point to the first element.
 Otherwise, check the position of the front if the front is less than 1 (front
< 1), then reinitialize it by front = n - 1, i.e., the last index of the array.
 Insertion at the rear end
 In this operation, the element is inserted from the rear end of the queue.
Before implementing the operation, we first have to check again
whether the queue is full or not. If the queue is not full, then the
element can be inserted from the rear end by using the below conditions
-
 If the queue is empty, both rear and front are initialized with 0. Now,
both will point to the first element.
 Otherwise, increment the rear by 1. If the rear is at last index (or size -
1), then instead of increasing it by 1, we have to make it equal to 0.
 Deletion at the front end
 In this operation, the element is deleted from the front end of the queue.
Before implementing the operation, we first have to check whether the
queue is empty or not.
 If the queue is empty, i.e., front = -1, it is the underflow condition, and
we cannot perform the deletion. If the queue is not full, then the
element can be inserted from the front end by using the below
conditions -
 If the deque has only one element, set rear = -1 and front = -1.
 Else if front is at end (that means front = size - 1), set front = 0.
 Else increment the front by 1, (i.e., front = front + 1).
Deletion at the rear end
In this operation, the element is deleted from the rear end of the queue. Before
implementing the operation, we first have to check whether the queue is
empty or not.
If the queue is empty, i.e., front = -1, it is the underflow condition, and we
cannot perform the deletion.
If the deque has only one element, set rear = -1 and front = -1.
If rear = 0 (rear is at front), then set rear = n - 1.
Else, decrement the rear by 1 (or, rear = rear -1).
 Applications of deque
1. Deque can be used as both stack and queue, as it supports both operations.
2. Deque can be used as a palindrome checker means that if we read the string from both ends, the
string would be the same.

 Complexity Analysis of Queue Operations

 Just like Stack, in case of a Queue too, we know exactly, on which position new element will be
added and from where an element will be removed, hence both these operations requires a
single step.
 Enqueue: O(1)
 Dequeue: O(1)
 Size: O(1)

You might also like