0% found this document useful (0 votes)
65 views24 pages

Python Data Struct

The document discusses several key data structures: 1. Lists in Python use dynamic memory allocation and store pointers to objects in an array, allowing heterogeneous types and dynamic resizing. 2. Stacks follow LIFO order, with push and pop operations at one end. 3. Queues follow FIFO order, with insert at the rear and delete at the front.

Uploaded by

Kanishk Mishra
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)
65 views24 pages

Python Data Struct

The document discusses several key data structures: 1. Lists in Python use dynamic memory allocation and store pointers to objects in an array, allowing heterogeneous types and dynamic resizing. 2. Stacks follow LIFO order, with push and pop operations at one end. 3. Queues follow FIFO order, with insert at the rear and delete at the front.

Uploaded by

Kanishk Mishra
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/ 24

DATA STRUCTURES

Memory Allocation

Memory can be allocated in two ways


1. Statically
2. Dynamically

In Static memory allocation the memory can not be


increased at run time. But in dynamic memory allocation
we can allocate more memory at run time.

Most of the languages like C, C++, Java are based on the


above concept.
Python works differently.
DATA STRUCTURES
A data structure is a group of data which can be processed as a
single unit. This group of data may be of similar or dissimilar data
types. Data Structures are very useful while programming
because they allow processing of the entire group of data as a
single unit. This makes managing of data simpler. The simplest
form of data structure is an array which combines finite data of
same data type.
Data structures are of two types: Linear and Non - Linear.
In a linear data structure, the elements are stored in a sequential
order. On the other hand, in a non linear data structure no
sequential order is followed. It is a sort of multilevel data
structure. Arrays, lists, stacks, queues, linked lists etc. are
examples of linear data structure while tree, graph etc. is a non -
linear data structure.
List is one of the simplest and most important data
structures in Python.
DATA STRUCTURES
Implementation of List in memory
In any programming language, an array is defined as a set of
contiguous data of similar data type. In most of the old languages, the
length of the array is fixed at the time of declaration, although now
recent languages provide the facility of variable length arrays. Python
lists are actually arrays of variable length. The elements of a list are of
heterogeneous types which means they are of different data types.
Hence [4,"Good", 8.45, 'k'] is a valid list.
This is possible because in Python language, technically
pointers, and not objects are stored in the form of an array. So a list in
Python is an array that contains elements (pointers to objects) of a
specific size only and this is a common feature of all dynamically typed
languages. For implementation of a list, a contiguous array of
references to other objects is used. Python keeps a pointer to this array
and the array's length is stored in a list head structure. This makes
indexing of a list independent of the size of the list or the value of the
index. When items are appended or inserted, the array of references is
resized.
DATA STRUCTURES
Sequential Memory Allocation
As you have studied before, a list is allocated memory in sequential
manner. This means that the elements of the list are stored in memory
in sequence of their declaration. So if you want to view the fourth
element of the list, you have to first traverse through first three
elements of the list. This is called sequential allocation of memory. The
list is allocated memory in accordance with its members and their
types. Also if memory is being shared by two or more members of the
list, then memory consumption will be different from the situation
where no sharing is involved. In Python, the length of the lists is not
fixed as elements can be dynamically added and removed from the
lists. Secondly memory consumption of the list is referenced by a
pointer, which will take 4 bytes in 32 bit mode and 8 bytes in 64 bit
mode. Here mode refers to the word size of the processor. Hence the
size of list will be 4 times the number of objects in the list in the 32 bit
mode and 8 times the number of objects in the list in 64 bit mode.
Other than these every list has a fixed header overhead and some
other over allocations involved for all Python lists.
DATA STRUCTURES(Stack)
STACK
A stack is a data structure whose elements are accessed
according to the Last-In First-Out (LIFO) principle. This is because in
a stack, insertion and deletion of elements can only take place at
one end, called top of the stack.
Consider the following examples of stacks:
1. Ten glass plates placed one above another. (The plate that is
kept last has to be taken out first)
2. The tennis balls in a container. (You cannot remove more than
one ball at a time)
3. A pile of books
4. A stack of coins

The two operations performed on the stack are:


1. Push: Adding(inserting) new element on to the stack.
2. Pop: Removing (deleting) an element from the stack
DATA STRUCTURES(Stack)
DATA STRUCTURES(Stack)
PUSH Operation
Adding new element to the stack list is called push operation. When the
stack is empty, the value of top is -1. Basically, an empty stack is initialized with an
invalid subscript. Whenever a Push operation is performed, the top is
incremented by one and then the new value is inserted on the top of the list till
the time the value of top is less than or equal to the size of the stack.

POP Operation
Removing existing elements from the stack list is called pop operation.
Here we have to check if the stack is empty by checking the value of top. If the
value of top is -1, then the stack is empty and such a situation is called Underflow.
Otherwise Pop operation can be performed in the stack. The top is decremented
by one if an element is deleted from the list.

Traversal Operation
Traversal is moving through the elements of the stack. If we want to display all the
elements of the stack.

We already have append and pop functions of list to perform push and pop
operation.
DATA STRUCTURES(Stack)
DATA STRUCTURES(Stack)
DATA STRUCTURES(Stack)
DATA STRUCTURES(Stack)

Q1. Write a Python menu driven program to


implement Stack(using inbuilt functions)
Q2. Write a Python menu driven program to
implement Stack(without using inbuilt functions)
DATA STRUCTURES
Expression
An expression is a combination of variables, constants and
operators. Expressions can be written in Infix, Postfix or Prefix
notations.

Infix Expression: In this type of notation, the operator is placed


between the operands.
For example: A+B, A*(B+C), X*Y/Z, etc

Postfix Expression: In this type of notation, the operator is placed


after the operands.
For example: AB+, ABC+*,XYZ/*, etc.

Prefix Expression: In this type of notation, the operator is placed


before the operands.
For example: +AB,*A+BC,*X/YZ, etc.
DATA STRUCTURES
Conversion of an infix expression to postfix expression
The following algorithm shows the logic to convert an infix expression to an
equivalent postfix expression:
Step 1: Start
Step 2:Add "("(left parenthesis) and ")" (right parenthesis) to the start and end of
the expression(E).
Step 3:Push "("left parenthesis onto stack.
Step 4:Check all symbols from left to right and repeat step 5 for each symbol of 'E'
until the stack becomes empty.
Step 5:If the symbol is:
i) an operand then add it to list.
ii) a left parenthesis "("then push it onto stack.
iii) an operator then:
a) Pop operator from stack and add to list which has the same
or higher precedence than the incoming operator.
b) Otherwise add incoming operator to stack.
iv) A right parenthesis")" then:
a) Pop each operator from stack and add to list until a left
parenthesis is encountered.
b) Remove the left parenthesis.
Step 6:Stop
DATA STRUCTURES
DATA STRUCTURES
Evaluation of Postfix Expression
The algorithm to evaluate a postfix expression is as
follows:
Step 1: Start
Step 2: Check all symbols from left to right and repeat
steps 3 & 4 for each symbol of expression 'E' until all
symbols are over.
i) If the symbol is an operand, push it onto stack.
ii) If the symbol is an operator then
a) Pop the top two operands from stack and
apply an operator in between them.
b) Evaluate the expression and place the
result back on stack.
Step 3:Set result equal to top element on the stack.
Step 4:Stop
DATA STRUCTURES
DATA STRUCTURES(Queue)
QUEUE
A queue is a container of elements, which are inserted and
removed according to the first-in first-out (FIFO) principle.
In a queue, persons who stand in the queue will carry out their work
one by one. That means those who stands first in the queue will be
allowed to carry out his work first and the person who stands at the
second position will be allowed to carry out his work second only. At
the same time those who come late will be joining the queue at the
end. In simple terms it is called 'first come first out’.

Technically speaking a queue is a linear list, to keep an


ordered collection of elements / objects. The principle operations,
which can be performed on it are
1. Addition of elements
2. Removal of elements.
DATA STRUCTURES(Queue)

• Addition of element is known as INSERT operation, also


known as enqueu-ing. It is done using rear terminal
position, i.e. tail end.
• Removal of element is known as DELETE operation also
know as dequeue-ing. It is done using front terminal
position, i.e. head of the list.
• As the two operations in the queue are performed from
different ends, we need to maintain both the access
points. These access points are known as FRONT, REAR.
FRONT is first element of the queue and REAR is last
element.
• As queue is FIFO implementation, FRONT is used for
delete operation and REAR is used for insert operation.
DATA STRUCTURES(Queue)

Queue Operations
Various operations, which can be performed on a queue
are:
• Create a queue having a data structure to store linear
list with ordering of elements.
• Insert an element will happen using REAR, REAR will be
incremented to hold the new value in queue.
• Delete an element will happen using FRONT and
FRONT will also be incremented to be able to access
next element
• Traversal of queue will be from front to rear.
DATA STRUCTURES(Queue)
DATA STRUCTURES(Queue)
DATA STRUCTURES(Queue)

As the queue is empty, this is an exception to be handled. We can


always say that deletion is attempted from an empty queue, hence
not possible. The situation is known as underflow situation. Similarly
when we work with fixed size list, insertion in a full list results into
overflow situation. In python as we don't have fixed size list, so don't
need to bother about overflow situation.
DATA STRUCTURES(Queue)

Q1. Write a Python menu driven program to


implement Queue.

You might also like