0% found this document useful (0 votes)
25 views

Bachelor of Computer Applications - SEM III CA229: Fundamentals of Data Structures and Algorithms

The document discusses linear data structures using arrays. It covers concepts like stacks, queues, their operations and applications. It also discusses recursion and provides examples to calculate factorials recursively. Overall, the document provides an overview of linear data structures and related algorithms.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Bachelor of Computer Applications - SEM III CA229: Fundamentals of Data Structures and Algorithms

The document discusses linear data structures using arrays. It covers concepts like stacks, queues, their operations and applications. It also discusses recursion and provides examples to calculate factorials recursively. Overall, the document provides an overview of linear data structures and related algorithms.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 51

Smt.

Chandaben Mohanbhai Patel


Institute of Computer Applications

Bachelor of Computer Applications – SEM III


CA229: Fundamentals of Data Structures and Algorithms

Dr. Kalpit G. Soni (MCA, PhD)


Assistant Professor
CMPICA - CHARUSAT
CA229
Fundamentals Data Structures and Algorithms
Unit No. Title of the Unit Minimum Numbers of
Hours
Theory (48) Practical

1 Introduction to Data Structures and Algorithms 06

2 Linear Data Structures using Array 10

3 Dynamic Data Structures 09


36
4 Tree Data Structure 08

5 Graph Data Structure 08

6 Sorting and Searching 07


Unit 2 – Linear Data Structure using Array
• Linear Data Structures using sequential organization
• Concepts of Ordered list
• Representation of Stacks using sequential organization
• Applications of Stack
• Recursion
• Application of Recursion
• Concepts of Queues
• Realization of Queues using sequential organization
• Multiqueue
• Deque
• Priority Queue
• Applications of Queue
Linear Data Structures
• In Linear data structures, the items are arranged in a linear sequence; the
linear arrangement of data is in terms of a sequence of data, such as an
array, stack, queue, or linked list where all elements will be arranged
linearly in memory.
Stack
• A stack is a linear structure in which items may be added or removed only at
one end.
• For Example, such a structure: a stack of dishes, a stack of folded towels.
• Observe that an item may be added or removed only from the top of any of
the stacks.
• In particular, the last item to be added to a stack is the first item to be
removed.
• Accordingly, the stack is also called the Last-In-First-Out (LIFO) list. Other
names used for stacks are "piles" and "push-down lists."
Stack
• Definition: A stack is an ordered collection of elements in which new items
may be inserted and from which items may be deleted only at one end, called
the top of the stack.
Operation of Stack
There are mainly four operations performed on the stack.
• PUSH
• POP
• PEEP
• CHANGE

PUSH Operation: This function adds an element to the top of the Stack.

POP Operation: This function removes the top most element from the Stack.

PEEP Operation: This function refers to the specific number of an element from the stack. We can access it
from the top of the Stack.

CHANGE Operation: This function is used to change or update the value of an element stored in the Stack at a
specific position. We can access it from the top of the Stack.
Application of Stack
• To convert infix expression to postfix and prefix expression.
• To evaluate postfix and prefix expressions.
• To reverse a string.
• To check whether a string is a palindrome or not.
• To check the validity of an arithmetic expression containing nested
parentheses. (Balancing the symbols)
• Tower of Hanoi
• N queens’ problem, etc.
Algorithm for PUSH Operation
PUSH (S, TOP, MAX, ELE)
Step 1: [Check for an Overflow condition]
If TOP = MAX Then
Write (“Stack Overflow”) and exit
End If
Step 2: [Increment the Pointer]
TOP  TOP + 1
Step 3: [Insert with the Element]
S[TOP]  ELE
Step 4: Exit
Algorithm for POP Operation
POP (S, TOP, ELE)
Step 1: [Check for an Underflow condition]
If TOP = 0 Then
Write (“Stack is Underflow”) and Exit
End If
Step 2: [Return term top of the element]
ELE  S [TOP]
Step 3: [Decrement the Pointer]
TOP  TOP - 1
Step 4: Exit
Algorithm for PEEP Operation
PEEP (S, TOP, POS)
Step 1: [Check for Position is valid or not]
If TOP – POS < 0 Then
Write (“Invalid Position”)
End If
Step 2: [Return the Element from the top of the stack]
Return S[TOP – POS + 1]
Step 3: Exit
Algorithm for CHANGE Operation
CHANGE (S, TOP, POS,ELE)
Step 1: [Check for a Position is valid or not]
If TOP – POS < 0 Then
Write (“Invalid Position”)
End If
Step 2: [Change POSth Element from TOP]
S[TOP – POS + 1]  ELE
Step 3: Exit
Arithmetic Expression: POLISH Notation
• Here we will take five operators +, -, *, /, ^ (Exponentiation).
• Level 2: ^ (Exponentiation)
• Level 1: * Multiplication and/or Division
• Level 0: + (Addition) and – (Subtraction)
Note:
• This exponentiation will be represented by the operator $, %, or ^. The value of
the expression x ^ y is x raised to y power so that 4 ^ 2 is 16.
Arithmetic Expression: POLISH Notation
Example:
Suppose we want to evaluate the following parenthesis-free arithmetic
expression:
Arithmetic Expression:
2 ^ 3 + 5 * 2 ^ 2 – 12 / 6
= 8 + 5 * 4 – 12 / 6
= 8 + 20 – 2
= 26
POLISH Notation
There are three ways to represent an arithmetic expression. INFIX PREFIX POSTFIX
A+B +AB AB+
INFIX: When the operator is between two operands that are
called infix. (Example: A + B) C–D -CD CD-
PREFIX: Where the operator precedes the operands is called
E*F *EF EF*
prefix or polish notation. (Example: + A B)
POSTFIX: Where the operator follows the two operands is called G/H /GH GH/

postfix or reverse polish notation. (Example: A B +)

INFIX PREFIX POSTFIX


A+(B*C) A+*BC A+BC*
A + X (Where X = * B C) A + X (Where X = B C *)
+AX AX+
+A*BC ABC*+
POLISH Notation
Question: A + ( B * C – ( D / E ^ F ) * G ) * H )
Scanned Symbol Stack S Expression P
(
A ( A
+ (+ A
( (+( A
B (+( AB
* (+(* AB
C (+(* ABC
- (+(- ABC*
Question: A + ( B * C – ( D / E ^ F ) * G ) * H )
Scanned Symbol Stack S Expression P
( (+(-( ABC*
D (+(-( ABC*D
/ (+(-(/ ABC*D
E (+(-(/ ABC*DE
^ (+(-(/^ ABC*DE
F (+(-(/^ ABC*DEF
) (+(- ABC*DEF^/
* (+(-* ABC*DEF^/
G (+(-* ABC*DEF^/G
) (+ ABC*DEF^/G*-
* (+* ABC*DEF^/G*-
H (+* ABC*DEF^/G*-H
) ABC*DEF^/G*-H*+
Advantages and Disadvantages of Stack
Advantages:
• To implement LIFO (Last in first out) mechanism.
• To evaluate any prefix or postfix expression.
• To reverse things.
• To convert prefix or postfix string.
• Recursion.
Disadvantages:
• We cannot delete an element at the specified position.
• It consumes memory.
Recursion
• Recursion is an essential concept in computer science.
• Many algorithms can be best described in terms of recursion.
• Recursion is the process that comes into existence when a function calls a copy
of itself to work on a smaller problem.
• Any function which calls itself is called a recursive function, and such function
calls are called recursive calls.
Recursion
Factorial function:
The product of the positive integers from 1 to n, inclusive, is called “n factorial” and is
usually denoted by n!
n! = 1 * 2 * 3 * ……. * (n – 2) * (n – 1) * n
It is also convenient to define 0! = 1 so that the function is defined for all nonnegative
integers. Thus we have
• 1!= 1
• 2! = 1 * 2 = 2
• 3! = 1 * 2 * 3 = 6
• 4! = 1 * 2 * 3 * 4 = 24
• 5! = 1 * 2 * 3 * 4 * 5 = 120 and so on.
• Observe that5! = 5 * 4! = 120
Recursion
Example:
Let us calculate 5! using the recursive function. This calculation requires the following
11 steps.
5! = 5 * 4!
4! = 4 * 3!
3! = 3 * 2!
2! = 2 * 1!
1! = 1
2! = 2 * 1 = 2
3! = 3 * 2 = 6
4! = 4 * 6 = 24
5! = 5 * 24 = 120
Queue
• A queue is a linear list in which items may be added only at one end and
items may be removed only at the other end.
• The name "queue" likely comes from the everyday use of the term. Consider a
queue of people waiting at a bus stop, each new person who comes takes his
/ her place at the end of the line, and when the bus comes, the people at the
front of the line board first.
• The first person in the line is the first person to leave. Thus, the queue is also
called First In First Out (FIFO).
Queue
Definition: The queue is a linear list of an element in which the deletion of an
element can take place only at one end called the front and insertion can
take place only at the other end called the rear. The front and rear element of
the queue is also called the first and last element of the queue, respectively.
Types of Queue
There are four types of queues:
• Simple Queue
• Circular Queue
• Priority Queue
• Deque (Double-Ended Queue)
Simple Queue
• A simple queue defines the simple operation of a queue in which insertion
occurs at the rear of the list and deletion occurs at the front of the list.
Operation of Queue
There are mainly two operations performed on the Queue.
• Insert Operation: This function adds an element to the rear of the
Queue.
• Delete Operation: This function removes the front element from the
Queue.
Simple Queue
The below figure is a schematic diagram of a queue with 4 elements. Where A is
the front element and D is the last element.

Suppose an element is deleted from the queue. Then it must be A. After this
operation queue is shown below figure. Where B is the front element and D is
the rear element.
Simple Queue
• Suppose E is added to the queue and then F is added to the queue. Then they
must be added at the rear of the queue as shown below figure.

• Now suppose another element is deleted from the queue, then it must be B.
After this operation queue is shown below figure.
Simple Queue
• Observe that whenever an element is deleted from the queue, the value of
FRONT is increased by 1.
FRONT = FRONT + 1
• Similarly, whenever an element is added to the queue, the value of REAR is
increased by 1.
REAR = REAR + 1
Algorithm for INSERT Operation
INSERT (Q, F, R, N, ELE)
Step 1: [Check for an Overflow condition]
If REAR >=MAX Then
Write "Queue is Full" and exit.
End If
Step 2: [Increment the Rear Pointer]
REAR  REAR + 1
Step 3: [Insert ELE Element in the queue]
Q[REAR]  ELE
Continue…
INSERT (Q, F, R, N, ELE)
Step 4: [Set the value of the front Pointer]
If FRONT = 0 Then
FRONT  1
End If
Step 5: Exit
Algorithm for DELETE Operation
DELETE (Q, F, R, ELE)
Step 1: [Check for Underflow condition]
If FRONT = 0 Then
Write "Queue is Empty" and exit.
End If
Step 2: [Store Element in ELE that is to be deleted]
ELE  Q[FRONT]
Continue…
DELETE (Q, F, R, ELE)
Step 3: [Set the value of the front and Rear Pointer]
If FRONT = REAR Then
FRONT  0
REAR  0
Else
FRONT  FRONT + 1
End If
Step 4: [Return the Element that is to be deleted]
Return ELE
Step 5: Exit
Advantages and Disadvantages Queue
Advantages:
• It is good for operating and managing huge data.
• It implements other data structure like arrays and lists etc.
• Helpful in managing multiple user’s needs.
• Very easy to use or implement.
Disadvantages:
• Space is predefined so it is limited
• A new element cannot be inserted without the deletion of the old element.
• Searching for any data in this is not easy.
Circular Queue
• In a circular queue, all nodes are treated as circular. The last node is connected
back to the first node.
• A circular queue is also called a Ring Buffer.
• The circular queue contains a collection of data elements that allow the
insertion of data at the end and the deletion at the beginning of the queue.
Circular Queue
• In a linear queue, we face the problem of an overflow of a queue frequently.
• As soon as the value of the rear becomes equal to size, our insert algorithm will
meet the overflow situation. Here queue may contain only one element; it
means the element is in the size location. All other locations starting from 1st
to size-1 are empty, but still, we get overflow because they are r=size.
• If somehow, we can utilize the previous size-1 location, then overflow will only
occur, If the queue contains total size elements and then, an attempt to insert
the new element.
Circular Queue
• A queue is called a circular queue when the last room (Element) comes just
before the first room (Element). Here we have array Q, which contains n
elements in which Q [0] comes after Q[n-1] in the away.
Algorithm for INSERT Operation
INSERT (Q, F, R, N, ELE)
Step 1: [Check for an Overflow condition]
If FRONT = 1 and REAR = MAX or FRONT = REAR + 1 Then
Write “Queue Full”
End If
Step 2: [Set the value of Rear Pointer]
If REAR = MAX Then
REAR  1
Else
REAR  REAR + 1
End If
Continue…
INSERT (Q, F, R, N, ELE)
Step 3: [Set the Front Pointer]
If FRONT <= 0 Then
FRONT  1
End If
Step 4: [Insert ELE Element in the queue]
Q[REAR]  ELE
Step 4: Exit
Algorithm for DELETE Operation
DELETE (Q, F, R, N, ELE)
Step 1: [Check for an Underflow condition]
If FRONT <= 0 Then
Write “Queue is EMPTY” and Exit
End If
Step 2: [Store the Element that is to be deleted]
ELE  Q[FRONT]
Step 3: [Set the value of Front and Rear Pointer]
If FRONT = REAR Then
FRONT  0
REAR  0
End If
Continue…
DELETE (Q, F, R, N, ELE)
Step 4: [Increment the Front Pointer]
If FRONT = MAX Then
FRONT  1
Else
FRONT  FRONT + 1
End If
Step 5: [Return the Element that is to be deleted]
return ELE
Step 6: Exit
Advantages and Disadvantages Queue
Advantages:
• It provides a quick way to store FIFO data with a maximum size.
• Efficient utilization of the memory.
• All operations occur in O(1) constant time.
• Does not use dynamic memory.
Disadvantages:
• In a circular queue, the number of elements you can store is only as much as
the queue length, you have to know the maximum size beforehand.
Priority Queue
• In this type of queue, every element of the queue has some priority, and based
on that priority; it will be processed.
• It means whatever operation performed on a given queue is depending on
some priority will be processed before the element which has less priority.
• Suppose two elements have the same priority then, in this case, the FIFO rule
will follow means the element which comes first in the queue will be processed
first.
Priority Queue
• The priority queue contains data items that have some preset priority. While
removing an element from a priority queue, the data item with the highest
priority is removed first.
• In a priority queue, insertion is performed in the order of arrival, and deletion
is performed based on priority.
Application of Priority Queue
• CPU Scheduling
• Graph algorithms like Dijkstra’s shortest path algorithm, Prim’s Minimum
Spanning Tree
• Event-driven simulation
• All queue applications where priority is involved.
Double Ended Queue
• In Double-Ended Queue, insert and delete operations can occur at both ends
that are front and rear of the queue.
Double Ended Queue (Deque)
• A deque is a linear list in which elements can be added or removed at either
end but not in the middle.
• The term deque is a contraction of the name double-ended queue.
• We will assume a circular array DEQUE maintains our deque with pointers
LEFT and RIGHT, which point to the two ends of the deque.
Operations of Deque
Mainly the following four basic operations are performed on queue:
• Adds an item at the front of the deque.
• Adds an item at the rear of the deque.
• To delete an item from the front of the deque.
• To delete an item from the rear of the deque.
Applications of Deque
Since deque supports both stack and queue operations, it can be used as both.
The Deque data structure supports clockwise and anticlockwise rotations in
O(1) time which can be useful in specific applications. Also, the problems where
elements need to be removed and or added to both ends can be efficiently solved
using deque.
Advantages and Disadvantages of Deque
Advantages:
• You are able to add and/or remove items from the both front and back of the
queue.
• Deques are faster in adding or removing the elements to the end or beginning.
Disadvantages:
• They are less memory efficient than a normal queue.
Application of Queue
• In a time-sharing computer system where many users share the system
simultaneously.
• The procedure, which is used to design such type of system, is the Round-robin
technique in DBMS.
• The railway reservation counter, where the people collect their tickets on a first-
come, first-serve basis.
• The people collect their tickets on a first-come, first-served basis such as at a railway
reservation counter.
• Many users share the system simultaneously in a time-sharing computer system.
• The pointer queues.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy