Bachelor of Computer Applications - SEM III CA229: Fundamentals of Data Structures and Algorithms
Bachelor of Computer Applications - SEM III CA229: Fundamentals of Data Structures and Algorithms
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/
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.