DSA Assignment - 2
DSA Assignment - 2
The complexity analysis of queue operations, where n is the number of elements in the queue, is
typically discussed in terms of time complexity. The primary queue operations include enqueue
(inserting an element into the queue) and dequeue (removing an element from the front of the queue).
It varies depending on the specific implementation of the queue. Here are the complexity analysis for
common queue operations in different types of queue:
A. Array-based Queue:
Enqueue: 0(1) amortized time complexity as long as the array does not need to be resized. In
the worst case scenario when the array needs to be resized, it becomes 0(n) because all
existing elements from the array are a constant time operation.
Dequeue: 0(1) times complexity. Removing the front element from the array is a constant time
operation.
Enqueue: 0(1) time complexity. Inserting a new element at the end of a linked list can be done
in constant time.
Dequeue: 0(1) time complexity. Similarly, removing the front element of a linked list can also
be done in constant time.
C. Circular Queue
Enqueue: 0(1) time complexity. Adding an element to the rear of the circular queue is a
constant time operation, regardless of the size of the queue.
Dequeue: 0(1) time complexity. Removing the front element from the circular queue also takes
constant time.
UNIK KHANAL
2. Why and When should we use Stack or Queue Data Structure instead of
Arrays/Lists ?
We use stack or queue data structure instead of array of list because they assist you in a more
specific manner than arrays and list. They are dynamic linear data structures whereas arrays or lists
are static linear data structures. Stacks and queues are both abstract data types that can be
implemented using array or list or other underlying data structures.
The following are the reasons for using stacks or queue over array or lists:
● We use stack or queue instead of arrays/lists when we want the elements in a specific order
i.e. in the order we put them (queue) or in reverse order (stack).
● Queues and stacks are dynamic while arrays are static. So when we require dynamic memory
we use queue or stack over arrays.
● Stacks and queues are used over arrays when sequential access is required.
● To efficiently remove any data from the start (queue) or the end (stack) of a data structure.
● When you want to get items out in the same order that you put them in, use a queue (FIFO).
● When you need to bring things out in the opposite order that they were put in, use a stack
(LIFO).
UNIK KHANAL
A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving
data which is based on Last-In-First-Out (LIFO).
An Abstract Data Type (ADT) defines a logical model for a certain class of data structures, specifying
the operations that can be performed on the data and the properties of these operations. The Stack
ADT describes a data structure that supports the following operations:
B. Push (s,x)
C. Top (s)
D. Pop (s)
E. Is Full (s)
Determine whether the stack s is full or not. Return true if s is full; return false otherwise.
F. Is Empty (s)
Determine whether the stack s is full or not. Return true if s is an empty; return false
otherwise.
UNIK KHANAL
a) Infix:
In an infix expression, the operator is placed between the operands. This is the conventional way we
write mathematical expressions. This is the way which we come across in our general mathematics,
where the operator is written in-between the operands. In infix expressions, operators may have
different precedence levels, and parentheses are used to specify the order of evaluation. For example:
a + b, 5 * (2 + 3), (a + b) * (c - d).
b) Postfix:
In a postfix expression, the operator comes after their operands. It is also known as Reverse Polish
Notation (RPN).Operators are written after their operands. Postfix expressions can be efficiently
evaluated using a stack-based algorithm. They eliminate the need for parentheses and have a
straightforward evaluation order. For Example: A B C + * - (Here, the addition is performed first, then
the multiplication, and then subtraction.)
c) Prefix:
In a prefix expression, the operator precedes their operands. It is also known as Polish Notation.
Operators are written before their operands. As like postfix, prefix notation too eliminates the need for
parentheses, as the position of operators determines the order of operations. Prefix expressions can
be easily evaluated using a stack-based algorithm. For example: / * A + B C D. As for Prefix, operators
are evaluated left-to-right and brackets are not needed. Operators act on the two nearest values on
the right.
UNIK KHANAL
Prefix and Postfix Notation offer advantages in certain situations, especially in the context of
computer science, programming languages, and mathematical evaluations. We use prefix and postfix
for the following reasons:
1) In Infix notation the use of parentheses is often required to indicate the precedence but prefix and
postfix notations removes the ambiguity.
2) Prefix and Postfix notations lend themselves well to evaluation using stack-based algorithms.
3) Expression trees, which represent the structure of mathematical expressions, are often easier to
build and manipulate using prefix or postfix notations.
4) In the context of compiler design, converting expressions to postfix or prefix notations can simplify
code generation for target machine languages.
5) While infix notation is more commonly used in human-readable mathematics, prefix and postfix
notations find applications in computer science, programming languages, and other areas where
expressions need to be processed algorithmically.
UNIK KHANAL
UNIK KHANAL
UNIK KHANAL
UNIK KHANAL
UNIK KHANAL
UNIK KHANAL
UNIK KHANAL
UNIK KHANAL