0% found this document useful (0 votes)
27 views22 pages

17 05 2024 DSA With Python For Student

The document discusses different data structures including stacks, queues, deques, and priority queues. It explains what each data structure is, how it works, and examples of basic operations on each. It also provides implementations of each using arrays and linked lists.
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)
27 views22 pages

17 05 2024 DSA With Python For Student

The document discusses different data structures including stacks, queues, deques, and priority queues. It explains what each data structure is, how it works, and examples of basic operations on each. It also provides implementations of each using arrays and linked lists.
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/ 22

DSA with

Python
(DSA - Data Structure and
Algoithms)

CSD203
Stacks
A stack is a data structure that can hold many
elements.
Think of a stack like a pile of pancakes.
In a pile of pancakes, the pancakes are both added and removed from
the top. So when removing a pancake, it will always be the last pancake
you added. This way of organizing elements is called LIFO: Last In First
Out.

Basic operations we can do on a stack are:


1 2 3 4 5

push: Adds a pop: Removes peek: Returns the isEmpty: Checks size: Finds the
new element on and returns the top element on if the stack is number of
the stack. top element from the stack. empty. elements in the
the stack. stack.

Experiment with these basic operations in the stack animation above.


Stacks can be implemented by using arrays or linked lists.
- Stacks can be used to implement undo mechanisms, to revert to previous states, to create algorithms for depth-first search
in graphs, or for backtracking.
- Stacks are often mentioned together with Queues.
Stack
Implementation
using Arrays
How it looks like when
we use an array as a
stack?
Stack
Implementation
using Arrays -
Example
Stack
Implementation
using Arrays -
Create a stack
class
Stack Implementation using
Linked Lists
How a stack can be
implemented using a
linked list?
Stack
Implementatio
n using Linked
List - Example
Queues
A queue is a data structure that can hold many
elements
Think of a queue as people standing in line in a supermarket.
The first person to stand in line is also the first who can pay
and leave the supermarket. This way of organizing elements is
called FIFO: First In First Out.

Basic operations we can do on a queue are:


1 2 3 4 5

enqueue: Adds a dequeue Remov peek: Returns the isEmpty: Checks size: Finds the
new element to es and the first first element in if the queue is number of
the queue. (front) element the queue. empty. elements in the
from the queue. queue.

Experiment with these basic operations in the queue animation above.


Queues can be implemented by using arrays or linked lists.

- Queues can be used to implement job scheduling for an office printer, order processing for e-tickets, or to create algorithms for
breadth-first search in graphs.
- Queues are often mentioned together with Stacks.
Queue
Implementatio
n using Arrays

How it looks like when


we use an array as a
queue?
Queue
Implementation
using Arrays -
Example
A B C

OUTPU
T
Queue
Implementation using
Arrays - Create a queue
class
Queue
Implementatio
n using Linked
Lists
- Create a
queue class
Deque
A deque is a data structure that can hold many
elements.
Deque or Double Ended
Queue is a type of queue in
which insertion and removal
of elements can either be
performed from the front or
the rear. Thus, it does not
follow FIFO rule (First In First
Out).
1 2 3 4 5 6
Basic operations we can do
on a stack are:
addFront: add addRear: adds removeFront: removeRear: isEmpty: Che Size: Find the
s an element an element to deletes an deletes an cks if the number of
at the front. the rear. element from element from deque is elements in
the front. the rear. empty. the deque.

Applications of Deque Data Structure


1. In undo operations on software.
2. To store history in browsers.
3. For implementing both stacks and queues. https://www.programiz.com/dsa/deque
Deque
Implementation using array – create Deque
class
Deque
Implementation

Exercise: Let's implement deque using a linked


list
Deque implemention using a linked
list
Priority Queue
Priority Queues are abstract data structures where each data/value in the queue has a certain priority. For
example, In airlines, baggage with the title “Business” or “First-class” arrives earlier than the rest.

Priority Queue is an extension of the queue with the following


properties:
• Every item has a priority associated with it.
• An element with high priority is dequeued before an element
with low priority.
• If two elements have the same priority, they are served
according to their order in the queue.

Basic operations we can do on a stack are:

Applications of Deque Data Structure


element. 1
Insertion: adds a new
2
Deletion: remove the
element which has
maximum priority first.
3
Peek: return the maximum
element.

Priority queues are often used in


real-time systems. They are also used in
algorithms to improve their efficiencies,
such as Dijkstra’s algorithm for finding
the shortest path in a graph and the A* Priority queue can be implemented using the following data
search algorithm for pathfinding. structures:
Priority Queue Implementation using
array

OUTPU
T
Priority Queue Implementation using Linked
List
Priority Queue Implementation using Linked
List
Priority Queue Implementation using Linked
List

OUTPU
T

You might also like