0% found this document useful (0 votes)
2 views

data structures in python - for app

Chapter 5 discusses data structures in Python, focusing on the stack, which is a LIFO (Last-In, First-Out) data structure. It covers basic operations such as PUSH and POP, along with real-life and computer applications of stacks. The chapter also explains stack implementation using lists, handling overflow and underflow conditions, and provides algorithms for push and pop operations.

Uploaded by

chamritrnd
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

data structures in python - for app

Chapter 5 discusses data structures in Python, focusing on the stack, which is a LIFO (Last-In, First-Out) data structure. It covers basic operations such as PUSH and POP, along with real-life and computer applications of stacks. The chapter also explains stack implementation using lists, handling overflow and underflow conditions, and provides algorithms for push and pop operations.

Uploaded by

chamritrnd
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Chapter 5: Data Structure in Python

What is Data Structure:


Data structure is a way of storing, organizing and fetching data in computer.
What is Stack:
A stack is a linear data structure in python in which addition and deletion of elements can be done at one
end only that is called TOP. A stack is known as LIFO (Last – In, First – Out) data structure in python.
Insertion in stack is also known as a PUSH operation. Deletion from stack is also known as POP operation
in stack.

Some of the application of stack in real life:

1. Pile of clothes in an Almirah.


2. Multiple chairs in a vertical pile.
3. Bangles worn on wrist.
4. Pile of boxes of eatables in a pantry or on a kitchen shelf.

Some of the application of stack in computer:

1. Reversing a word/Line.
2. Undo Mechanism in Text Editors.
3. Using in recursion.
4. Stack application in backtracking. Backtracking is used in a large number of puzzles games like
Sudoku.
5. <html> code

Working with Stack using List / Implementation of Stack using List:


Working with stack is similar to working with list. Basic operations that we should know are :
1. How to create an empty stack?
2. How to add elements to a stack?
3. How to delete / remove elements from the stack
4. How to traverse or displaying elements of stack?
5. How to check for empty stack?

Create an empty stack:


st = [ ] or st = list( )

Adding an element to a Stack : This is known as PUSH


st.append(5)

Deleting / removing elements from the stack : This is known as POP


st.pop( )

Traverse or displaying elements of stack:


L = len(st)
for i in l[::-1] #to display in reverse order
print(i)

Check for empty stack (is_empty):


if (st == [ ]):
print("stack is empty")

Overflow and Underflow:


Overflow: It refers to a situation, when one tries to push an element in stack/queue, that is full. In
python list OVERFLOW condition does not arise until all memory is exhausted because list can grow as per
data. Because List is dynamic in nature.

Underflow: It refers to situation when one tries to pop/delete an item from an empty stack or queue.
Example:
List=[]
List.pop()
What is Peek: Get the top data element of the stack, without removing it.
We can do this by: list[-1].
Steps algorithm for Push Operations:
Steps 1: Checks if the stack is full.
Steps 2: If stack is full, produce an error and exit.
Steps 3: If stack is not full, Add an element in stack using append().
Steps 4: Then Access the List or Stack.
Steps for algorithm Pop Operations:
Steps 1: Checks if the stack is empty.
Steps 2: If stack is empty, produce an error and exit.
Steps 3: But If stack is not empty, Then delete top element of stack using pop().
Steps 4: Then Access the List or Stack.

You might also like