You are on page 1of 29

DATA STRUCTURES

IN
PYTHON

PREPARED BY :
JYOTI GUPTA
PGT(COMPUTER SCIENCE)
KV SLIET LONGOWAL
As per latest CBSE syllabus 2020-21, the topics
to be covered in Data-structures for class XII
are:
• Lists as covered in Class XI
• Stacks – Push, Pop using a list
• Queues – Insert, Delete using a list.
PREREQUISITES
The students should already know

• the methods used for creation of LISTS


• Operations on LISTS
• Inbuilt Functions of LISTS
 
LEARNING OBJECTIVES:
• The students will learn the basic concept
and working of stacks and queues.

• The students will be able to make


programs on Python Shell
 
CONCEPTS TO BE COVERED

DATA STRUCTURES LIST A REVIEW OPERATIONS


AND TYPES STACK EXAMPLES
ON STACK
DATA STRUCTURES
Data structures (in general) are :

• The structures which can hold some data


together.
• In other words, they are used to store a
collection of related data.
 
IN PYTHON :
LINEAR DATA STRUCTURES
• The data items are organized sequentially or, in
other words, linearly.
• The data items are traversed serially one after
another using their index values.
• All the data items in a linear data structure can
be traversed during a single run.
LIST IN PYTHON

(A Review)
LIST IS :

used to store collection of heterogeneous


items.

mutable, which means that you can


change their content without changing
their identity.
You can recognize lists by their square
brackets [ and ]

that hold elements, separated by a comma ,.


MANIPULATING LIST
(Already covered in Class XI)

Adding Removing sorting or


news items some items reversing a
in a list from a list list
EXAMPLE
EXAMPLE
Consider a List list_num as :

Listnum=[3,6,8,4,9,2,8]

You might remember that we have two types of


indexing available with this list :
 
 
EXAMPLE
Listnum=[3,6,8,4,9,2,8]
Length=7

3 6 8 4 9 2 8

index 0 1 2 3 4 5 6
Negative
index -7 -6 -5 -4 -3 -2 -1
Now , we will perform following operations on given list using in
built functions of Python :
Listnum=[3,6,8,4,9,2,8]

Use insert() to insert 11 at


index or position 3 in the
Add 11 to the list_num list with list_num list
append(). By default, this number
will be added to the end of the Listnum.insert(3,11)
list:
The resultant list will be:
[3,6,8,11,4,9,2,8]
Listnum.append(11)

The resultant list will be:


[3,6,8,4,9,2,8,11]
For deleting items from given list using in built functions of
Python :
Listnum=[3,6,8,4,9,2,8]

Remove the item at index -2


from list_num
Remove the first occurence of 8
from list_num with the help of Listnum.pop(-2)
remove()
The resultant list will be:
Listnum.remove(8) [3,6,8,4,9,8]

The resultant list will be:


[3,6,4,9,2,8]
STACK
STACK
• A stack is a container of objects

• The objects are inserted and removed according

to the Last-In-First-Out (LIFO) concept.

 
STACK EXAMPLES
stack of books or stack of plates
 
STACK OPERATIONS
There are basically two operations on Stack:
(a) PUSH (b) POP

 
PUSH OPERATION
inserting an element in the stack
 
POP OPERATION
deleting an element from the stack
 
WHAT IS TOP ?
• It is the index of top most element of the stack.
Or
• the recent element pushed in the Stack.
Implementation
Of Stacks
In PYTHON
Implementation
Stacks can be implemented using lists in Python.

• When you add elements to a stack, it is known


as a push operation.
• We will use append() function of list for this.

• When you remove or delete an element it is


called a pop operation.
• We will use pop() function of list for this.
PROGRAMMIMG
This program has been implemented in online Python Compiler.
APPLICATIONS OF STACK

Imagine you’re a developer and you are working on a brand new


word processor. You need to create an undo feature – allowing
users to backtrack their actions until the beginning of the
session. A stack is an ideal fit for this scenario. We can record
every action of the user by pushing it to the stack. When the
user wants to undo an action they can pop accordingly from the
stack.

All recursive functions are implemented using stack.


THANK YOU

JYOTI GUPTA
PGT(COMPUTER SCIENCE)
KV SLIET LONGOWAL

You might also like