0% found this document useful (0 votes)
8 views11 pages

Python Stack Data Structure Guide

The document provides an overview of data structures, focusing on stacks, which are linear data structures that operate on a Last In First Out (LIFO) principle. It explains key stack operations such as PUSH and POP, as well as terms like Peek, Overflow, and Underflow. Additionally, it outlines applications of stacks in various computational tasks and demonstrates stack implementation in Python using built-in functions.
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)
8 views11 pages

Python Stack Data Structure Guide

The document provides an overview of data structures, focusing on stacks, which are linear data structures that operate on a Last In First Out (LIFO) principle. It explains key stack operations such as PUSH and POP, as well as terms like Peek, Overflow, and Underflow. Additionally, it outlines applications of stacks in various computational tasks and demonstrates stack implementation in Python using built-in functions.
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

D ATA S TRUCTURE

Stack
Data-structures
It a way of organizing and storing data in such a manner so that it
can be accessed and work over it can be done efficiently and less
resources are required. It define the relationship between the data
and the operations over those data. There are many various types
of data structures defined that make it easier for the computer
programmer,to concentrate on the main problems rather than
getting lost in the details of data description and access.
Python Data Structure
Data-structures
Stack:
A stack is a linear data structure in which all the
insertion and deletion of data / values are done at one
end only.
 It is type of linear data
structure.
 It follows LIFO(Last In First
Out) property.
 Insertion / Deletion in stack
can only be done from top.
 Insertion in stack is also
known as a PUSH operation.
 Deletion from stack is also
known as POP operation in
stack.
O THER S TACK TERM
 Peek : getting the most recent value of stack i.e
value at TOP

 OverFlow : a situation when we are Pushing item


in Stack that is full.

 Underflow : a situation when we are Popping


item from empty stack
Data-structures
Applications of Stack:
• Expression Evaluation: It is used to evaluate prefix,
postfix and infix expressions.
• Expression Conversion: It can be used to convert one
form of expression(prefix,postfix or infix) to one another.
• Syntax Parsing: Many compilers use a stack for parsing
the syntax of expressions.
• Backtracking: It can be used for back traversal of steps
in a problem solution.
• Parenthesis Checking: Stack is used to check the
proper opening and closing of parenthesis.
• String Reversal: It can be used to reverse a string.
• Function Call: Stack is used to keep information about
the active functions or subroutines.
Data-structures
Using List as Stack in Python:

The concept of Stack implementation is easy in


Python , because it support inbuilt functions
(append() and pop()) for stack
[Link] Using these functions make
the code short and simple for stack
implementation.
To add an item to the top of the list, i.e., to
push an item, we use append() function and to
pop out an element we use pop() function.
These functions work quiet efficiently and fast
in end operations.
Data-structures
Stack e.g. program:

stack = [5, 9, 3]
[Link](7)
[Link](11) OUTPUT
print(stack) [5, 9, 3, 7, 11]
print([Link]()) 11
print(stack) [5, 9, 3, 7]
print([Link]()) 7
print(stack) [5, 9, 3]
I MPLEMENTING S TACK IN P YTHON
This function will
check Stack is
empty or not

This function will


add new item in
Stack, here setting
top is mandatory

This function is used


to remove item from
stack, also perform
checks before deletion
I MPLEMENTING S TACK IN P YTHON
This function will
return the top
most item from the
stack

This function will


display stack items
I MPLEMENTING S TACK IN P YTHON

Displaying menu
to user to interact
I MPLEMENTING S TACK IN P YTHON

You might also like