You are on page 1of 24

Data Structure Question Bank CAT 1 EXAM

Unit : 1 & 2 ( 2023 )


Q 1 :- Define Data Structure and Abstract data Structure ?
Ans :- DATA STRUCTURE
A data structure is a specialized format for organizing,
processing, retrieving and storing data.

ABSTRACT DATA STRUCTURE


An abstract data type is an abstraction of a data structure
that provides only the interface to which the data structure
must adhere. OR
Abstract Data type (ADT) is a type (or class) for objects whose
behaviour is defined by a set of values and a set of operations.
It is called “abstract” because it gives an implementation-
independent view.

Q 2 :- List the most frequently used operation on Data Structure ?


Ans :- Searching – We can easily search for any data element in
a data structure. Sorting – We can sort the elements either in
ascending or descending order. Insertion – We can insert new
data elements in the data structure. Deletion – We can delete
the data elements from the data structure.

Notes Available site name: http://technolmlearning.epizy.com


Data Structure Question Bank CAT 1 EXAM

Q 3 :- Write the short note on it time and space complexity.


How it is important of algorithm analysis ?
Ans :- Time Complexity:- The time complexity of an algorithm
quantifies the amount of time taken by an algorithm to run as a
function of the length of the input.
The time required by the algorithm to solve given problem is
called Time Complexity of the algorithm.
Time complexity is very useful measure in algorithm analysis.
Algorithm ADD SCALAR(A, B)
C <- A + B
return C
Space Complexity:- Problem-solving using computer
requires memory to hold temporary data or final result while
the program is in execution. The amount of memory required
by the algorithm to solve given problem is called space
complexity of the algorithm.
Types Of Space Complexity
(1) A fixed part: It is independent of the input size. It includes memory for
instructions (code), constants, variables, etc.

(2) A variable part: It is dependent on the input size. It includes memory


for recursion stack, referenced variables, etc.

Example : Addition of two scalar variables

Algorithm ADD SCALAR(A, B)


C <— A+B
return C

Notes Available site name: http://technolmlearning.epizy.com


Data Structure Question Bank CAT 1 EXAM

Q 4 :- Differentiate among Big-Oh(O), Theta(Θ) and


Omega(Ω) Notation with the help of diagram ?
Ans :-
S.No.
Big Oh (O) Big Omega (Ω) Big Theta (Θ)
It is like (>=)
It is like (<=) rate of growth is It is like (==)
rate of growth of an greater than or meaning the rate of
algorithm is less than or equal to a growth is equal to a
1. equal to a specific value. specified value. specified value.
The algorithm’s The bounding of function
The upper bound of lower bound is from above and below is
algorithm is represented represented by represented by theta
2. by Big O notation Omega notation. notation.
Big Omega (Ω) – Big Theta (Θ) – Tight
3. Big oh (O) – Upper Bound Lower Bound Bound
It is define as
lower bound and It is define as tightest
It is define as upper bound lower bound on bound and tightest
and upper bound on an an algorithm is bound is the best of all
algorithm is the most the least amount the worst case times that
4. amount of time required. of time required. the algorithm can take.
Mathematically: Mathematically – Big
Mathematically: Big Oh is Big Omega is 0 Theta is 0 <= C2g(n) <=
0 <= f(n) <= Cg(n) for all n <= Cg(n) <= f(n) f(n) <= C1g(n) for n >=
5. >= n0 for all n >= n0 n0

Notes Available site name: http://technolmlearning.epizy.com


Data Structure Question Bank CAT 1 EXAM
Big oh notation (O): Big Omega notation (Ω) : Big Theta notation (Θ) :

Q 5 :- Differentiate Between Linear and Non-Linear Data Structure ?

Ans :-

In a linear data structure, data In a non-linear data structure, data


elements are arranged in a linear elements are attached in hierarchically
1. order. manner.

In linear data structure, single level is Whereas in non-linear data structure,


2. involved. multiple levels are involved.

Its implementation is easy in


comparison to non-linear data While its implementation is complex in
3. structure. comparison to linear data structure.

In linear data structure, data While in non-linear data structure, data


elements can be traversed in a elements can’t be traversed in a single
4. single run only. run only.

While in a non-linear data structure,


In a linear data structure, memory is memory is utilized in an efficient way.
5. not utilized in an efficient way.

Its examples are: array, stack, While its examples are: trees and
6. queue, linked list, etc. graphs.

Linear data structures are useful for Non-linear data structures are useful
simple data storage and for representing complex relationships
8. manipulation. and data hierarchies.

Notes Available site name: http://technolmlearning.epizy.com


Data Structure Question Bank CAT 1 EXAM

Q 6 :- Explain how address of an element is calculated


in two dimensional arrays ?
Ans :- The2-dimensional array can be defined as an array of
arrays. The 2-Dimensional arrays are organized as matrices
which can be represented as the collection of rows and
columns as array[M][N] where M is the number of rows and N
is the number of columns.

To find the address of any element in a 2-Dimensional array there are the
following two ways-
1. Row Major Order
2. Column Major Order

Row major ordering assigns successive elements, moving across the


rows and then down the next row, to successive memory locations. In simple
language, the elements of an array are stored in a Row-Wise fashion.
To find the address of the element using row-major order uses the following
formula:

Address of A[I][J] = B + W * ((I – LR) * N + (J – LC))


I = Row Subset of an element.
J = Column Subset of an element.
B = Base address.
W = Storage size of one element store in an array(in byte).
LR = Lower Limit of row/start row index of the matrix.

Notes Available site name: http://technolmlearning.epizy.com


Data Structure Question Bank CAT 1 EXAM
LC = Lower Limit of column/start column index of the matrix
N = Number of column given in the matrix.
Given:
Base address B = 100
Storage size of one element store in any array W = 1 Bytes
Row Subset of an element whose address to be found I = 8
Column Subset of an element whose address to be found J = 6
Lower Limit of row/start row index of matrix LR = 1
Lower Limit of column/start column index of matrix = 1
Number of column given in the matrix N = Upper Bound – Lower Bound + 1
= 15 – 1 + 1
= 15
Formula:
Address of A[I][J] = B + W * ((I – LR) * N + (J – LC))
Solution:
Address of A[8][6] = 100 + 1 * ((8 – 1) * 15 + (6 – 1))
= 100 + 1 * ((7) * 15 + (5))
= 100 + 1 * (110)
Address of A[I][J] = 210

2. Column Major Order:


If elements of an array are stored in a column-major fashion means moving
across the column and then to the next column then it’s in column-major
order.
Address of A[I][J] = B + W * ((J – LC) * M + (I – LR))
I = Row Subset of an element whose address to be found,
J = Column Subset of an element whose address to be found,
B = Base address,
W = Storage size of one element store in any array(in byte),
LR = Lower Limit of row/start row index of matrix(If not given assume it as
zero),
LC = Lower Limit of column/start column index of matrix(If not given assume
it as zero),
M = Number of rows given in the matrix.
Given:
Base address B = 100
Storage size of one element store in any array W = 1 Bytes
Row Subset of an element whose address to be found I = 8
Column Subset of an element whose address to be found J = 6
Lower Limit of row/start row index of matrix LR = 1
Lower Limit of column/start column index of matrix = 1
Number of Rows given in the matrix M = Upper Bound – Lower Bound + 1

Notes Available site name: http://technolmlearning.epizy.com


Data Structure Question Bank CAT 1 EXAM
= 10 – 1 + 1
= 10
Formula: used
Address of A[I][J] = B + W * ((J – LC) * M + (I – LR))
Address of A[8][6] = 100 + 1 * ((6 – 1) * 10 + (8 – 1))
= 100 + 1 * ((5) * 10 + (7))
= 100 + 1 * (57)
Address of A[I][J] = 157

Q 7 :- Explain Algorithm. And also write the structure of


algorithm and Properties of algorithms ?
Ans:- Algorithm is a step-by-step procedure, which defines a set
of instructions to be executed in a certain order to get the
desired output. Algorithms are generally created independent of
underlying languages, i.e. an algorithm can be implemented in
more than one programming language.
• An algorithm is a set of commands that must be followed for a
computer to perform calculations or other problem-solving operations.
• According to its formal definition, an algorithm is a finite set of
instructions carried out in a specific order to perform a particular task.
• It is not the entire program or code; it is simple logic to a problem
represented as an informal description in the form of a flowchart or
pseudocode.

Let's try to learn algorithm-writing by using an example.


Problem − Design an algorithm to add two numbers and display
the result.
Step 1 − START
Step 2 − declare three integers a, b & c
Step 3 − define values of a & b

Notes Available site name: http://technolmlearning.epizy.com


Data Structure Question Bank CAT 1 EXAM
Step 4 − add values of a & b
Step 5 − store output of step 4 to c
Step 6 − print c
Step 7 − STOP

Q 8 :- 8 and 4 are equal question


Q 9 :- Define 2-dimensional array. And how is two-
dimensional array represented in memory ?
Ans :- A two-dimensional array is a tabular representation
of data in which the elements are kept in rows and
columns. M X N elements are arranged in M rows and N
columns to form a two-dimensional array.

#include <stdio.h>
void main ()
{
int arr[3][3],i,j;
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
printf("Enter a[%d][%d]: ",i,j);
scanf("%d",&arr[i][j]);
}
}
printf("\n printing the elements ....\n");
for(i=0;i<3;i++)
{
printf("\n");
for (j=0;j<3;j++)
{
printf("%d\t",arr[i][j]);
}

Notes Available site name: http://technolmlearning.epizy.com


Data Structure Question Bank CAT 1 EXAM
}

Q10. a[10...30, 55...75], base address of the array (BA) = 0, size


of an element = 4 bytes. Find the location of a[15] [68] in row
major order.

Ans:- A two-dimensional array with dimensions [10...30,


55...75]
base address of the array (BA) = 0
size of an element = 4 bytes
To find the location of a[15][68] in row major order, we
need to calculate its linear address by flattening the two-
dimensional array into a one-dimensional array. We can
use the formula:

linear address = BA + size of an element * (i * (number of


columns) + j)
where i and j are the row and column indices of the
element, respectively.

To convert the two-dimensional indices to one-


dimensional indices, we need to subtract the lower bounds
of each dimension from the indices:
i = 15 - 10 = 5
j = 68 - 55 = 13
The number of columns in the array is equal to the
number of elements in each row, which is:
number of columns = 75 - 55 + 1 = 21
Plugging in the values, we get:
linear address = 0 + 4 * (5 * 21 + 13)
= 0 + 4 * (108 + 13)
= 0 + 4 * 121

Notes Available site name: http://technolmlearning.epizy.com


Data Structure Question Bank CAT 1 EXAM
= 484

Therefore, the location of a[15][68] in row major order is


484 bytes from the base address of the array.

11. List the basic operations performed on a Stack.

Ans :- A stack is an abstract data type that follows the LIFO


(Last In First Out) principle. The basic operations
performed on a stack are as follows:

Push: The push operation adds an element to the top of the


stack. It takes the element as an input and adds it to the top
of the stack. If the stack is full, it raises an overflow error.

Pop: The pop operation removes and returns the top


element of the stack. It removes the element from the top of
the stack and returns it. If the stack is empty, it raises an
underflow error.

Peek/Top: The peek or top operation returns the top


element of the stack without removing it. It returns the
element at the top of the stack without removing it.

Size: The size operation returns the number of elements in


the stack.

is Empty: The is Empty operation returns a boolean value


indicating whether the stack is empty or not.

12. Consider the following stack, where STACK is allocated


N = 6 memory cells:

Notes Available site name: http://technolmlearning.epizy.com


Data Structure Question Bank CAT 1 EXAM
STACK: AAA, DDD, EEE, FFF, GGG, ____ .
Describe the stack as the following operations take place:
A) PUSH (STACK, KKK), B) POP
(STACK, ITEM), C) PUSH (STACK, LLL), D) PUSH (STACK,
SSS), E) POP(STACK, ITEM)
and F) PUSH (STACK, TTT).

Ans:- Initially, the stack looks like this:

STACK: AAA, DDD, EEE, FFF, GGG, ____


A) After performing PUSH(STACK, KKK), the stack will
look like:
STACK: AAA, DDD, EEE, FFF, GGG, KKK

B) After performing POP(STACK, ITEM), where ITEM is a


placeholder variable to store the popped element, the
stack will look like:
STACK: AAA, DDD, EEE, FFF, ____, KKK
The popped element will be stored in ITEM and will have a
value of "GGG".

C) After performing PUSH(STACK, LLL), the stack will look


like:
STACK: AAA, DDD, EEE, FFF, LLL, KKK

D) After performing PUSH(STACK, SSS), the stack will look


like:
STACK: AAA, DDD, EEE, FFF, LLL, KKK, SSS

E) After performing POP(STACK, ITEM), the stack will look


like:

Notes Available site name: http://technolmlearning.epizy.com


Data Structure Question Bank CAT 1 EXAM
STACK: AAA, DDD, EEE, FFF, LLL, ____, SSS
The popped element will be stored in ITEM and will have a
value of "KKK".

F) After performing PUSH(STACK, TTT), the stack will look


like:
STACK: AAA, DDD, EEE, FFF, LLL, TTT, SSS
The final state of the stack is as shown above.

Q 13. Write an algorithm to convert Infix expression to


equivalent prefix expression.
Ans:- To convert an infix exp

Q 14. Translate, by inspection and hand, each infix


expression into its equivalent prefix expression:

a) ( A – B ) * ( D / E)

b) ( A + B ^ D) / ( E – F ) + G

c) ( A * ( B + D ) / E – F * ( G + H / K)

Ans:- (a) Infix expression: (A – B) * (D / E)

We can convert this infix expression to prefix notation


using the following steps:

Reverse the order of the operands and operators: * - A B /


DE
Move the operators before their corresponding operands: *
-AB/DE
The prefix expression equivalent to the given infix

Notes Available site name: http://technolmlearning.epizy.com


Data Structure Question Bank CAT 1 EXAM
expression is: * - A B / D E

b) Infix expression: (A + B ^ D) / (E – F) + G
We can convert this infix expression to prefix notation
using the following steps:
Reverse the order of the operands and operators: + / ^ + A
BD-EFG
Move the operators before their corresponding operands:
+/^+ABD-EFG
The prefix expression equivalent to the given infix
expression is: + / ^ + A B D - E F G

(c) Infix expression: (A * (B + D) / E – F * (G + H / K))


We can convert this infix expression to prefix notation
using the following steps:

Reverse the order of the operands and operators: - / * A +


BDE*F+G/HK
Move the operators before their corresponding operands: -
/*A+BDE*F+G/HK
The prefix expression equivalent to the given infix
expression is: - / * A + B D E * F + G / H K

Q 15. Consider the following infix expression Q: (( A + B


) * D ) ^ ( E – F) . Use algorithm to translate Q into its
equivalent Postfix expression P.

Ans:- To convert the given infix expression Q into postfix


expression P, we can use the following algorithm:
➢ Create an empty stack and an empty output string.
➢ Scan the infix expression from left to right.

Notes Available site name: http://technolmlearning.epizy.com


Data Structure Question Bank CAT 1 EXAM
➢ If the current character is an operand (i.e., a variable
or a constant), append it to the output string.
➢ If the current character is a left parenthesis, push it
onto the stack.
➢ If the current character is a right parenthesis, pop
operators from the stack and append them to the
output string until a left parenthesis is encountered.
Discard the left and right parentheses.
➢ After scanning the entire infix expression, pop any
remaining operators from the stack and append them
to the output string.
➢ The resulting output string is the postfix expression.
Applying this algorithm to the given infix expression Q,
we get:
Step 1: Create an empty stack and an empty output
string.

Stack: []
Output:

Step 2: Scan the infix expression from left to right.

Token: ( ( A + B ) * D ) ^ ( E – F )
Action: A B + D * E F – ^

Step 3: If the current character is an operand (i.e., a


variable or a constant), append it to the output string.

Token: ( ( A + B ) * D ) ^ ( E – F )
Output: A B + D * E F – ^

Step 4: If the current character is a left parenthesis,

Notes Available site name: http://technolmlearning.epizy.com


Data Structure Question Bank CAT 1 EXAM
push it onto the stack.

Token: ( ( A + B ) * D ) ^ ( E – F )
Stack: (
Output: A B + D * E F – ^

Step 5: If the current character is a right parenthesis,


pop operators from the stack and append them to the
output string until a left parenthesis is encountered.
Discard the left and right parentheses.

Token: ( ( A + B ) * D ) ^ ( E – F )
Stack:
Output: A B + D * E F – ^

Step 6: If the current character is an operator, then:


a. While the stack is not empty and the top of the stack
is an operator with greater or equal precedence than
the current operator, pop the top operator from the
stack and append it to the output string.
b. Push the current operator onto the stack.

Token: ( ( A + B ) * D ) ^ ( E – F )
Stack: *
Output: A B + D * E F – ^

Token: ( ( A + B ) * D ) ^ ( E – F )
Stack:
Output: A B + D * E F – ^ *

Step 7: After scanning the

Notes Available site name: http://technolmlearning.epizy.com


Data Structure Question Bank CAT 1 EXAM
Q 16. Consider the following stack of characters, where
STACK is allowed N = 8 Memory cells:
STACK: A, C, D, F, K, ___, ___, ___ .
(For notational convenience, we use “___” to denote an
empty memory cell.) Describe the stack
as the following operations take place:

(a) POP (STACK, ITEM) e) POP (STACK, ITEM)

Ans:- (a) The top element of the stack is removed and


stored in ITEM. The resulting stack is:
STACK: A, C, D, F, K, ___, ___, ___ (ITEM = empty)

(e) The top element of the stack is removed and stored in


ITEM. The resulting stack is:
STACK: A, C, D, F, ___, ___, ___, ___ (ITEM = K)

(b) POP (STACK, ITEM) f) PUSH (STACK, R)

Ans:- POP (STACK, ITEM) removes the top element of the


stack and stores it in ITEM, so the resulting stack will be:
STACK: A, C, D, F, K, ___, ___, ___ (ITEM = empty)

PUSH (STACK, R) adds the element R to the top of the


stack, so the resulting stack will be:
STACK: A, C, D, F, K, R, ___, ___

(c) PUSH (STACK, L) g) PUSH (STACK, S)

Ans:- (c) The element L is added to the top of the stack.


The resulting stack is:
STACK: A, C, D, F, L, ___, ___, ___

(g) The element S is added to the top of the stack. The

Notes Available site name: http://technolmlearning.epizy.com


Data Structure Question Bank CAT 1 EXAM
resulting stack is:
STACK: A, C, D, F, L, S, ___, ___

(d) PUSH (STACK, P) h) POP (STACK, ITEM)

Ans:- (d) The element P is added to the top of the stack.


The resulting stack is:
STACK: A, C, D, F, K, P, ___, ___ .

(h) The top element of the stack is removed and stored in


ITEM. The resulting stack is:
STACK: A, C, D, F, K, ___, ___, ___ (ITEM = P)

Q 17. Write an algorithm to covert infix expression to


equivalent Postfix expression.

Ans:- To convert Infix expression to Postfix expression, we


will use the stack data structure. By scanning the infix
expression from left to right,if we get any operand, simply
add it to the postfix form, and for the operator and
parenthesis, add them in the stack maintaining the
precedence of them.
Infix Expression : Infix Expression contains operator in-
between every pair of operands,Expression of the form a
op b.
Postfix expression: Postfix Expression contains operator
followed for every pair of operands,Expression of the form
a b op.
Algorithm
• Step 1 : Scan the Infix Expression from left to right.

• Step 2 : If the scanned character is an operand,

append it with final Infix to Postfix string.


• Step 3 : Else,

Notes Available site name: http://technolmlearning.epizy.com


Data Structure Question Bank CAT 1 EXAM
• Step 3.1 : If the precedence order of the
scanned(incoming) operator is greater than the
precedence order of the operator in the stack (or
the stack is empty or the stack contains a ‘(‘ or ‘[‘
or ‘{‘), push it on stack.
• Step 3.2 : Else, Pop all the operators from the stack
which are greater than or equal to in precedence than
that of the scanned operator. After doing that Push
the scanned operator to the stack. (If you encounter
parenthesis while popping then stop there and push
the scanned operator in the stack.)
• Step 4 : If the scanned character is an ‘(‘ or ‘[‘ or ‘{‘,
push it to the stack.
• Step 5 : Repeat steps 2-6 until infix expression is
scanned.
• Step 6 : Print the output
• Step 7 : Pop and output from the stack until it is not
empty.
Q 18. Consider the following arithmetic expression P,
written in postfix notation:
(a) P: 12, 7, 3, –, /, 2, 1, 5, +, *, +
Ans:- Starting from the left-most token, we process each
token in turn:

Push 12 onto the stack.


Stack: [12]

Push 7 onto the stack.


Stack: [12, 7]

Push 3 onto the stack.


Stack: [12, 7, 3]

Notes Available site name: http://technolmlearning.epizy.com


Data Structure Question Bank CAT 1 EXAM

Pop 3 and 7 from the stack, subtract 3 from 7, and push the
result (4) onto the stack.
Stack: [12, 4]

Pop 4 and 12 from the stack, divide 12 by 4, and push the


result (3) onto the stack.
Stack: [3]

Push 2 onto the stack.


Stack: [3, 2]

Push 1 onto the stack.


Stack: [3, 2, 1]

Push 5 onto the stack.


Stack: [3, 2, 1, 5]

Pop 5 and 1 from the stack, add them together, and push the
result (6) onto the stack.
Stack: [3, 2, 6]

Pop 6 and 2 from the stack, multiply them together, and


push the result (12) onto the stack.
Stack: [3, 12]

Pop 12 and 3 from the stack, add them together, and push
the result (15) onto the stack.
Stack: [15]
Q 19. Execute the following Infix expression Q to
equivalent prefix expression P using algorithm:
Q: (m+n) * (k+p) / (g/h)^(a^b/c).
Ans:- To convert an infix expression to prefix notation, we
can use the following steps:

Notes Available site name: http://technolmlearning.epizy.com


Data Structure Question Bank CAT 1 EXAM

Reverse the infix expression, so that the parentheses and


operators appear in reverse order.
Replace each open parenthesis with a close parenthesis,
and each close parenthesis with an open parenthesis.
Convert the resulting expression to postfix notation.
Reverse the postfix expression to obtain the prefix notation.
Using this algorithm, we can convert the infix expression Q
to its prefix equivalent P as follows:

Reverse the infix expression: .c/b^a/h/g)*p+k(n+m)(


Replace each open parenthesis with a close parenthesis,
and each close parenthesis with an open parenthesis:
)c/b^a/h/g(*p+k(n+m(
Convert the resulting expression to postfix notation:
cba^/hg/)*pk+nm+
Reverse the postfix expression to obtain the prefix
notation: +nm+kp)*/*gh^cba
Therefore, the prefix expression P is: +nm+kp)*/*gh^cba
20. What is a Sparse Matrix? Explain an efficient way of
storing a Sparse Matrix in Memory.
Ans:- A sparse matrix is a matrix in which most of the
elements are zero. In other words, it is a matrix in which the
number of zero elements is much larger than the number of
non-zero elements. Sparse matrices often arise in scientific
and engineering applications where a large number of
variables are involved and only a small subset of them are
relevant for a particular problem.

Storing a sparse matrix using the traditional method of a 2D


array would be inefficient, as it would waste memory on
storing the large number of zero elements. Therefore, an
efficient way of storing a sparse matrix in memory is to use
a data structure that only stores the non-zero elements and
their corresponding indices.

Notes Available site name: http://technolmlearning.epizy.com


Data Structure Question Bank CAT 1 EXAM
21.WAP to implement Tower of Hanoi problem
implementing stacks
Ans:- class Stack:
def __init__(self):
self.items = []

def push(self, item):


self.items.append(item)

def pop(self):
return self.items.pop()

def peek(self):
return self.items[-1]

def is_empty(self):
return len(self.items) == 0

def tower_of_hanoi(n, source, destination, auxiliary):


if n == 1:
destination.push(source.pop())
else:
tower_of_hanoi(n-1, source, auxiliary, destination)
destination.push(source.pop())
tower_of_hanoi(n-1, auxiliary, destination, source)

# initialize the stacks


source = Stack()
auxiliary = Stack()
destination = Stack()

# populate the source stack with the discs


n = int(input("Enter the number of discs: "))
for i in range(n, 0, -1):
source.push(i)

Notes Available site name: http://technolmlearning.epizy.com


Data Structure Question Bank CAT 1 EXAM
# solve the tower of hanoi problem
tower_of_hanoi(n, source, destination, auxiliary)

# print the result


print("The discs are now on the destination stack:")
while not destination.is_empty():
print(destination.pop())

22. WAP to apply operations on Linear Queue.


Ans:- Linear Data Structure Queue
Linear data structure Queue has data elements that are
arranged sequentially. Before stepping into the main topic,
we need to understand the basic working of a queue. The
very basic principle of Queue is "First in, First Out" it
means the element inserted first will get deleted first
when the delete operation takes place.
Important features of Linear Queue
The following are the features of linear queue:
1. Similar to a stack, the Queue is a list of items with
similar data types.
2. Queues are arranged in a FIFO (First In, First
Out) structure.
3. To remove a new element from the Queue, all the
elements inserted before the new element must be
removed.
The Linear Queue mainly has two ends, one in front and
the other in the rear end. Check the below diagram to get
an idea.

Notes Available site name: http://technolmlearning.epizy.com


Data Structure Question Bank CAT 1 EXAM

23. Explain Circular Queue and how it is implemented?


Ans:- A Circular Queue is a data structure that is similar to
a regular queue, but with a circular behavior in which the
last element is connected to the first element. This means
that when an element is dequeued from the queue, the next
element to be dequeued will be the element after the one
that was just dequeued, unless it is the last element in the
queue, in which case the next element will be the first
element in the queue.
24.Write an algorithm for inserting and deleting
element from a circular queue ?

Ans:- Here are the algorithms for inserting and deleting an


element from a circular queue:

Inserting an element:
Algorithm: insert(queue, element)
if queue is full:
return "Overflow"
else if queue is empty:
front = 0
rear = 0
queue[rear] = element
else:

Notes Available site name: http://technolmlearning.epizy.com


Data Structure Question Bank CAT 1 EXAM
rear = (rear + 1) % max_size
queue[rear] = element
return "Element inserted"
Deleting an element:

Algorithm: delete(queue)
if queue is empty:
return "Underflow"
else if front == rear:
element = queue[front]
front = -1
rear = -1
else:
element = queue[front]
front = (front + 1) % max_size
return element

Notes Available site name: http://technolmlearning.epizy.com

You might also like