BE03000081 DS Unit 2 Part1
BE03000081 DS Unit 2 Part1
Syllabus Array: Representation of arrays, Applications of arrays, sparse matrix and its
representation
Stack: Stack-Definitions & Concepts, Operations On Stacks, Applications of Stacks,
Polish Expression, Reverse Polish Expression And Their Compilation, Recursion,
Tower of Hanoi
Queue: Representation Of Queue, Operations On Queue, Circular Queue, Priority
Queue, Array representation of Priority Queue, Double Ended Queue, Applications of
Queue
Linked List: Singly Linked List, Doubly Linked list, Circular linked list, Linked
implementation of Stack, Linked implementation of Queue, Applications of linked list.
Array
What is an Array?
Types of Array:
1. One Dimensional
2. Two Dimensional
3. Multi Dimensional
datatype ArrayName[Arraysize];
NLJIET Page | 1
New L.J Institute of Engineering and Technology
Semester: III Subject: Data Structures (BE03000081)
Unit 2: Linear Data Structure
NLJIET Page | 2
New L.J Institute of Engineering and Technology
Semester: III Subject: Data Structures (BE03000081)
Unit 2: Linear Data Structure
data-type array-name[row-size][column-size] ;
For Example, float x[3][4];
NLJIET Page | 3
New L.J Institute of Engineering and Technology
Semester: III Subject: Data Structures (BE03000081)
Unit 2: Linear Data Structure
NLJIET Page | 4
New L.J Institute of Engineering and Technology
Semester: III Subject: Data Structures (BE03000081)
Unit 2: Linear Data Structure
Array Representation
Array Representation
The elements of a Two dimensional array can be arranged in two ways.
1. Row major Representation
2. Column major Representation
6. Formula.
Where,
B = Base address(Address of First Element)
I = Row subscript of element whose address is to be found
J = Column subscript of element whose address is to be found
W = Storage Size of one element stored in the array (in byte) (Int=2
byte,Float=4 byte)
Lr = Lower limit of row/start row index of matrix, if not given assume 0
(zero)
Lc = Lower limit of column/start column index of matrix, if not given
assume 0 (zero)
M = Number of row of the given matrix
N = Number of column of the given matrix
NLJIET Page | 6
New L.J Institute of Engineering and Technology
Semester: III Subject: Data Structures (BE03000081)
Unit 2: Linear Data Structure
Address of A [ I ][ J ] = B + W * [ N * ( I – Lr ) + ( J – Lc ) ]
A [ 2 ][ 2 ] = 1050 + 2 * [ 4 * ( 2 – 0 ) + ( 2 – 0) ]
= 1070
NLJIET Page | 7
New L.J Institute of Engineering and Technology
Semester: III Subject: Data Structures (BE03000081)
Unit 2: Linear Data Structure
NLJIET Page | 8
New L.J Institute of Engineering and Technology
Semester: III Subject: Data Structures (BE03000081)
Unit 2: Linear Data Structure
NLJIET Page | 9
New L.J Institute of Engineering and Technology
Semester: III Subject: Data Structures (BE03000081)
Unit 2: Linear Data Structure
Applications of Array
Applications
Array are used for representing some sequential data structures such as stack
and queues.
Array are used to represent sparse matrices.
Array are widely used to implement mathematical vector, matrices and other
kind of tables.
Array can be used for sorting elements in ascending or descending order.
It is used in every possible situation where you need to gather similar objects
at one place.
Arrays are used to implement Search Algorithms.
Arrays are also used to implement CPU Scheduling Algorithms.
Sparse Matrix
NLJIET Page | 10
New L.J Institute of Engineering and Technology
Semester: III Subject: Data Structures (BE03000081)
Unit 2: Linear Data Structure
A linear list which allow insertion and deletion of an element at one end only
is called Stack.
A Stack is a data structure which is used to store data in a particular order.
It follows Last In First Out (LIFO) Order.
NLJIET Page | 11
New L.J Institute of Engineering and Technology
Semester: III Subject: Data Structures (BE03000081)
Unit 2: Linear Data Structure
The element which is inserted last, that will be come out first.
There are many real-life examples of a stack.
Consider an example of plates stacked over one another in the canteen.
Operations on Stack
There are four operations which we can perform on stack.
NLJIET Page | 12
New L.J Institute of Engineering and Technology
Semester: III Subject: Data Structures (BE03000081)
Unit 2: Linear Data Structure
Procedure: PUSH(S,TOP,X)
This procedure insert an element X to the top of a stack.
Stack is represented by a vector S Containing N Elements.
A pointer TOP represents the top element of the stack.
2. [Increment Top]
Top ← Top + 1
3. [Insert Element]
S[Top] ← X
4. [Finished]
Return
Procedure: POP(S,TOP)
This procedure remove and returns the top element from a stack.
Stack is represented by a vector S Containing N Elements.
A pointer TOP represents the top element of the stack.
2. [Decrement Pointer]
Top ← Top-1
NLJIET Page | 13
New L.J Institute of Engineering and Technology
Semester: III Subject: Data Structures (BE03000081)
Unit 2: Linear Data Structure
Procedure: PEEP(S,TOP,I)
This procedure returns the value of ith element from top of the stack.
The element is not deleted by this function.
Stack is represented by a vector S Containing N Elements.
A pointer TOP represents the top element of the stack.
Procedure: CHANGE(S,TOP,X,I)
This procedure changes the value of ith element from the top of the stack to
the value contained in X.
Stack is represented by a vector S Containing N Elements.
A pointer TOP represents the top element of the stack
3. [Finished]
Return
Application of Stack
NLJIET Page | 14
New L.J Institute of Engineering and Technology
Semester: III Subject: Data Structures (BE03000081)
Unit 2: Linear Data Structure
Infix = a + b
Prefix = +ab
Postfix = ab+
NLJIET Page | 15
New L.J Institute of Engineering and Technology
Semester: III Subject: Data Structures (BE03000081)
Unit 2: Linear Data Structure
1. UNPARENTHESIZED_SUFFIX(Without Bracket).
e.g. a + b * c - d / e
2. REVPOL(With Bracket)
e.g. a * (b + c) – d
o Algorithm UNPARENTHESIZED_SUFFIX
This algorithm converts the string INFIX to its reverse Polish string
equivalent.
INFIX: Input string INFIX representing an infix expression whose single
character symbols have precedence values and ranks as given in Table.
S: vector S representing a stack.
NEXTCHAR: String function NEXTCHAR which, when invoked,
returns the next character of the input string.
RANK: RANK contains the value of each head of the reverse Polish
string.
NEXT: NEXT contains the symbol being examined.
TEMP:TEMP is a temporary variable which contains the unstacked
element.
POLISH: which contain final output string(postfix form)
We assume that the given input string is padded on the right with the
special symbol “#”.
NLJIET Page | 16
New L.J Institute of Engineering and Technology
Semester: III Subject: Data Structures (BE03000081)
Unit 2: Linear Data Structure
Algorithm UNPARENTHESIZED_SUFFIX:
6. [Push current symbol onto stack and obtain next input symbol]
Call Push (S, Top, NEXT)
NEXT ← NEXTCHAR (INFIX)
NLJIET Page | 17
New L.J Institute of Engineering and Technology
Semester: III Subject: Data Structures (BE03000081)
Unit 2: Linear Data Structure
o Example: A + B * C –D
INFIX: A + B * C –D #
o Algorithm REVPOL:
o This algorithm converts INFIX into reverse polish and places the result
into POLISH.
o Given an input string INFIX containing an infix expression which has
been padded on the right with ‘)’.
o All Symbol have precedence value given in table.
o Stack is represented by vector S, TOP denotes the top of the stack,
Algorithm PUSH and POP are used for stack manipulation.
o Function NEXTCHAR returns the next symbol from given input string.
o The integer variable RANK contains the rank of Expression.
o The string variable TEMP is used for temporary storage purpose.
1. [Initialize stack]
Top ← 1
S[Top] ← ‘(‘
NLJIET Page | 18
New L.J Institute of Engineering and Technology
Semester: III Subject: Data Structures (BE03000081)
Unit 2: Linear Data Structure
o Example: A *(B + C ) –D
INFIX: (A *(B + C) –D)
NLJIET Page | 19
New L.J Institute of Engineering and Technology
Semester: III Subject: Data Structures (BE03000081)
Unit 2: Linear Data Structure
o Algorithm Evaluate_Postfix
1. [Initialize Stack]
Top ← 0
Answer← 0
Answer ← PERFORM_OPERATION(OP1,OP2,TEMP)
PUSH(S, TOP, Answer)
POSTFIX: 9 3 4 * 8 + 4 / -
POSTFIX: 5 4 6 + * 4 9 3 / + *
o Algorithm Evaluate_Prefix
NLJIET Page | 21
New L.J Institute of Engineering and Technology
Semester: III Subject: Data Structures (BE03000081)
Unit 2: Linear Data Structure
1. [Initialize Stack]
Top ← 0
Answer← 0
PREFIX: -9 / + * 3 4 8 4
+ * 48 –12+ 12* 8 4
NLJIET Page | 22
New L.J Institute of Engineering and Technology
Semester: III Subject: Data Structures (BE03000081)
Unit 2: Linear Data Structure
A + B * C –D Infix
NLJIET Page | 23
New L.J Institute of Engineering and Technology
Semester: III Subject: Data Structures (BE03000081)
Unit 2: Linear Data Structure
NLJIET Page | 24
New L.J Institute of Engineering and Technology
Semester: III Subject: Data Structures (BE03000081)
Unit 2: Linear Data Structure
A + B -C *D / E +F $ G / (I+J) Infix
Recursion
What is Recursion?
o Identify the simplest instance of the problem that can be solved directly
without further recursive calls.
o This is the stopping condition for the recursion.
NLJIET Page | 25
New L.J Institute of Engineering and Technology
Semester: III Subject: Data Structures (BE03000081)
Unit 2: Linear Data Structure
o Without a base case, the function would call itself infinitely, leading to a
stack overflow.
o Each recursive call must bring the problem closer to the base case.
o This means the input to the recursive call should be a smaller or simpler
version of the original input.
o After the recursive calls return their solutions for the sub problems,
combine these results to construct the solution for the original, larger
problem.
o This step is particularly relevant in problems like tree traversals or
sorting algorithms.
o Let us consider a problem to find the sum of natural numbers, there are
several ways of doing that but the simplest approach is simply to add the
numbers starting from 0 to n.
Need of Recursion?
NLJIET Page | 26
New L.J Institute of Engineering and Technology
Semester: III Subject: Data Structures (BE03000081)
Unit 2: Linear Data Structure
Tower of Hanoi
Tower of Hanoi
NLJIET Page | 27
New L.J Institute of Engineering and Technology
Semester: III Subject: Data Structures (BE03000081)
Unit 2: Linear Data Structure
Queue
A linear list which permits deletion to be performed at one end of the list and
insertion at the other end is called queue.
The information in such a list is processed FIFO (first in first out) or FCFS
(first come first served) manner.
Front is the end of queue from that deletion is to be performed.
Rear is the end of queue at which new element is to be inserted.
Insertion operation is called Enqueue & deletion operation is called Dequeue.
Representation of Queue
Types of Queue
o Simple Queue.
o Circular Queue
o Priority Queue
o Double Ended Queue
NLJIET Page | 28
New L.J Institute of Engineering and Technology
Semester: III Subject: Data Structures (BE03000081)
Unit 2: Linear Data Structure
QINSERT (Q, F, R, N, Y)
o This procedure inserts Y at rear end of Queue.
o Queue is represented by a vector Q containing N elements.
o F is pointer to the front element of a queue.
o R is pointer to the rear element of a queue.
1. [Overflow?]
If R ≥ N
Then Write (‘Overflow’)
Return
3. [Insert element]
Q[R] ← Y
QDELETE (Q, F, R)
o This function deletes & returns an element from front end of the Queue.
o Queue is represented by a vector Q containing N elements.
NLJIET Page | 29
New L.J Institute of Engineering and Technology
Semester: III Subject: Data Structures (BE03000081)
Unit 2: Linear Data Structure
1. [Underflow?]
If F = 0
then Write (‘Underflow’)
Return (0) (‘0’ denotes an empty queue)
2. [Delete element]
Y ← Q[F]
3. [Queue empty?]
If F = R
then F ← R ← 0
else F ← F + 1 (Increment front pointer)
4. [Return element]
Return (Y)
NLJIET Page | 30
New L.J Institute of Engineering and Technology
Semester: III Subject: Data Structures (BE03000081)
Unit 2: Linear Data Structure
Circular Queue
CQINSERT (F, R, Q, N, Y)
o This procedure inserts Y at rear end of the Circular Queue.
o Queue is represented by a vector Q containing N elements.
o F is pointer to the front element of a queue.
o R is pointer to the rear element of a queue.
2. [Overflow?]
If F = R
then Write (‘Overflow’)
Return
NLJIET Page | 31
New L.J Institute of Engineering and Technology
Semester: III Subject: Data Structures (BE03000081)
Unit 2: Linear Data Structure
3. [Insert element]
Q[R] ← Y
CQDELETE (F, R, Q, N)
o This function deletes & returns an element from front end of the Circular
Queue.
o Queue is represented by a vector Q containing N elements.
o F is pointer to the front element of a queue.
o R is pointer to the rear element of a queue.
1. [Underflow?]
If F = 0
then Write (‘Underflow’)
Return (0)
2. [Delete element]
Y ← Q[F]
3. [Queue empty?]
If F = R
then F ← R ← 0
Return (Y)
NLJIET Page | 32
New L.J Institute of Engineering and Technology
Semester: III Subject: Data Structures (BE03000081)
Unit 2: Linear Data Structure
Algorithm: DISPLAY_CircularQueue
Input: A circular queue CQ[], front index FRONT, rear index REAR, and size
SIZE
Output: Elements of the circular queue in order from front to rear
1. IF FRONT == -1
PRINT "Queue is empty"
RETURN
2. SET i = FRONT
3. REPEAT
a. PRINT CQ[i]
b. SET i = (i + 1) % SIZE
UNTIL i == (REAR + 1) % SIZE
Priority Queue
NLJIET Page | 33
New L.J Institute of Engineering and Technology
Semester: III Subject: Data Structures (BE03000081)
Unit 2: Linear Data Structure
The priority of the element will be used to determine the order in which the
elements will be processed
The general rules of processing the elements of a priority queue are
o An element with higher priority is processed before an element with a
lower priority.
o Two elements with the same priority are processed on a first-come-first-
served (FCFS) basis.
Priority queue can be thought of as a modified queue in which when an
element has to be removed from the queue, the one with the highest-priority
is retrieved first.
The priority of the element can be set based on various factors.
Priority queues are widely used in operating systems to execute the highest
priority process first.
The priority of the process may be set based on the CPU time it requires to
get executed completely.
For example, if there are three processes, where the first process needs 5 ns to
complete, the second process needs 4 ns, and the third process needs 7 ns, then
the second process will have the highest priority and will thus be the first to
be executed.
However, CPU time is not the only factor that determines the priority, rather
it is just one among several factors.
Another factor is the importance of one process over another.
In case we have to run two processes at the same time, where one process is
concerned with online order booking and the second with printing of stock
details, then obviously the online booking is more important and must be
executed first.
Lower priority number means higher priority. For example, if there are two
elements A and B, where A has a priority number 1 and B has a priority
number 5, then A will be processed before B as it has higher priority than B.
NLJIET Page | 34
New L.J Institute of Engineering and Technology
Semester: III Subject: Data Structures (BE03000081)
Unit 2: Linear Data Structure
However, if there exists an element that has the same priority as the new
element, the new element is inserted after that element.
For example, consider the priority queue shown in Fig.
If we have to insert a new element with data = F and priority number = 4, then
the element will be inserted before D that has priority number 5, which is
lower priority than that of the new element.
So, the priority queue now becomes as shown in Fig.
NLJIET Page | 35
New L.J Institute of Engineering and Technology
Semester: III Subject: Data Structures (BE03000081)
Unit 2: Linear Data Structure
Output restricted dequeue: In this dequeue, deletions can be done only at one
of the ends, while insertions can be done on both ends.
In a dequeue, two pointers are maintained, LEFT and RIGHT, which point to
either end of the dequeue.
NLJIET Page | 36
New L.J Institute of Engineering and Technology
Semester: III Subject: Data Structures (BE03000081)
Unit 2: Linear Data Structure
Applications of Queue
Queue of people at any service point such as ticketing etc.
Queue of air planes waiting for landing instructions.
Queue of processes in OS.
Queue is also used by Operating systems for Job Scheduling.
When a resource is shared among multiple consumers. E.g., in case of printers
the first one to be entered is the first to be processed.
When data is transferred asynchronously (data not necessarily received at
same rate as sent) between two processes. Examples include IO Buffers, pipes,
file IO, etc.
Queue is used in BFS (Breadth First Search) algorithm. It helps in traversing
a tree or graph.
Queue is used in networking to handle congestion.
NLJIET Page | 37
New L.J Institute of Engineering and Technology
Semester: III Subject: Data Structures (BE03000081)
Unit 2: Linear Data Structure
NLJIET Page | 38
New L.J Institute of Engineering and Technology
Semester: III Subject: Data Structures (BE03000081)
Unit 2: Linear Data Structure
Sr.
Questions CO Answer Marks
No
What is Sparse matrix? Write efficient vector
representation of following Sparse matrix.
[NLJIET]
Pg
1 2 3
10,11
NLJIET Page | 39
New L.J Institute of Engineering and Technology
Semester: III Subject: Data Structures (BE03000081)
Unit 2: Linear Data Structure
NLJIET Page | 40
New L.J Institute of Engineering and Technology
Semester: III Subject: Data Structures (BE03000081)
Unit 2: Linear Data Structure
NLJIET Page | 41
New L.J Institute of Engineering and Technology
Semester: III Subject: Data Structures (BE03000081)
Unit 2: Linear Data Structure
NLJIET Page | 42
New L.J Institute of Engineering and Technology
Semester: III Subject: Data Structures (BE03000081)
Unit 2: Linear Data Structure
NLJIET Page | 43