SlideShare a Scribd company logo
2
Most read
4
Most read
7
Most read
STACK DATA STRUCTURE
STACK
• A stack is a linear data structure that follows the principle of Last In First Out (LIFO).
This means the last element inserted inside the stack is removed first.
• Here you can:
Put a new plate on top
Remove the top plate
And, if you want the plate at the bottom, you must first remove all the plates on top.
This is exactly how the stack data structure works.
• LIFO (Last In First Out) principal, putting an item on top of the stack is called push and
removing an item is called pop.
Empty Stack PUSH
A
B
A
PUSH
A
B
C
PUSH
A
B
C
POP
STACK OPERATIONS PUSH AND POP
BASIC OPERATIONS OF STACK
There are some basic operations that allow us to perform different actions on a stack.
• Push: Add an element to the top of a stack
• Pop: Remove an element from the top of a stack
• IsEmpty: Check if the stack is empty
• IsFull: Check if the stack is full
• size() - It returns the length of the stack.
• Peek or top: Get the value of the top element without removing it
IMPLEMENTATION OF STACK IN PYTHON
Following ways are:
• List
• deque
• LifoQueue
LIST OPERATIONS FOR STACK
• Python list can be used as the stack.
• It uses the append() method to insert elements to the list where stack uses
the push() method.
• The list also provides the pop() method to remove the last element.
• To return size of stack ,python provide function len().
IMPLEMENTATION USING COLLECTION.DEQUE
• The deque is pronounced as the "deck" which means "double-ended queue".
• The collection module provides the deque class, which is used to creating
Python stacks.
• The deque can be preferred over the list because it performs append and pop
operation faster than the list.
IMPLEMENTATION USING QUEUE MODULE
• The queue module has the LIFO queue, which is the same as the stack.
Generally, the queue uses the put() method to add the data .
Below are a few methods that available in the queue.
• empty() - If queue empty, returns true; otherwise return false.
• maxsize() - This method is used to set the maximum number of
elements allowed in the queue.
• qsize() - It returns the size of the queue.
IMPLEMENTATION USING QUEUE MODULE
• get() - It returns and removes the element from the queue.
Sometime. The queue can be empty; it waits until element is
available.
• full() - It returns True if the queue is full. The queue is defined as
maxsize = 0 by default. In this case, it will not return True.
• put(item) - It adds the element to the queue; if the queue is full, wait
until space is available.
• put_nowait(item) - It adds the element into the queue without
delaying.
LINKED LIST
• A linked list is a sequence of data elements, which are connected
together via links. Each data element contains a connection to another
data element in form of a pointer.
• Python does not have linked lists in its standard library. We implement
the concept of linked lists using the concept of nodes.
LINKED LIST
• A linked list is a linear data structure that includes a series of connected
nodes. Here, each node stores the data and the address of the next node.
For example,
SINGLE LINKED LIST
• In this type of data structure there is only one link between any two
data elements. We create such a list and create additional methods to
insert, update and remove elements from the list.
NOTE
1.Every Python module has it’s __name__ defined and if this is ‘__main__’, it
implies that the module is being run standalone by the user and we can do
corresponding appropriate actions.
2.If you import this script as a module in another script, the __name__ is set to
the name of the script/module.
3.Python files can act as either reusable modules, or as standalone programs.
4.if __name__ == “__main__”: is used to execute some code only if the file was
run directly, and not imported.

More Related Content

Similar to Data Structure Stack operation in python (20)

PPTX
stack_presentaton_HUSNAIN[2].pojklklklptx
HusnainNaqvi2
 
PPTX
Stack and queue power point presentation data structure and algorithms Stack-...
abhaysingh19149
 
PPTX
DSEC1.pptx stack exchange communities that I am not 🚭 hi nahi
ganesh209832
 
PPTX
Lecture 2 Introduction to Stacks and Queues.pptx
yhrcxd8wpm
 
PPT
unit 5 stack & queue.ppt
SeethaDinesh
 
PPTX
Bca ii dfs u-2 linklist,stack,queue
Rai University
 
PPTX
Mca ii dfs u-3 linklist,stack,queue
Rai University
 
PPTX
Bsc cs ii dfs u-2 linklist,stack,queue
Rai University
 
PPTX
stacks and queues for public
iqbalphy1
 
PPTX
stack coding.pptx
JamJahanzaib
 
PPTX
Stack_and_Queue_Presentation_Final (1).pptx
binduraniha86
 
PPTX
Stack_and_Queue_Presentation_Final (1).pptx
binduraniha86
 
PPTX
Stacks.pptx in software engineering in simple understanding
MohammedAnas871930
 
PPTX
stack.pptx
mayankKatiyar17
 
PPTX
CD3291 2.5 stack.pptx
mareeswari15
 
PPT
week 7,8,10,11 alll files included from .ppt
LidetAdmassu
 
PDF
Chapter 5 Stack and Queue.pdf
GirT2
 
PPTX
VCE Unit 03vv.pptx
skilljiolms
 
PPTX
Lecture 1 Abstract Data Types of Complexity Analysis of Big Oh Notation.pptx
yhrcxd8wpm
 
PPTX
Lecture 1 Abstract Data Types of Complexity Analysis of Big Oh Notation.pptx
yhrcxd8wpm
 
stack_presentaton_HUSNAIN[2].pojklklklptx
HusnainNaqvi2
 
Stack and queue power point presentation data structure and algorithms Stack-...
abhaysingh19149
 
DSEC1.pptx stack exchange communities that I am not 🚭 hi nahi
ganesh209832
 
Lecture 2 Introduction to Stacks and Queues.pptx
yhrcxd8wpm
 
unit 5 stack & queue.ppt
SeethaDinesh
 
Bca ii dfs u-2 linklist,stack,queue
Rai University
 
Mca ii dfs u-3 linklist,stack,queue
Rai University
 
Bsc cs ii dfs u-2 linklist,stack,queue
Rai University
 
stacks and queues for public
iqbalphy1
 
stack coding.pptx
JamJahanzaib
 
Stack_and_Queue_Presentation_Final (1).pptx
binduraniha86
 
Stack_and_Queue_Presentation_Final (1).pptx
binduraniha86
 
Stacks.pptx in software engineering in simple understanding
MohammedAnas871930
 
stack.pptx
mayankKatiyar17
 
CD3291 2.5 stack.pptx
mareeswari15
 
week 7,8,10,11 alll files included from .ppt
LidetAdmassu
 
Chapter 5 Stack and Queue.pdf
GirT2
 
VCE Unit 03vv.pptx
skilljiolms
 
Lecture 1 Abstract Data Types of Complexity Analysis of Big Oh Notation.pptx
yhrcxd8wpm
 
Lecture 1 Abstract Data Types of Complexity Analysis of Big Oh Notation.pptx
yhrcxd8wpm
 

More from deepalishinkar1 (20)

PPTX
data structure stack appplication in python
deepalishinkar1
 
PPTX
Stack application in infix to prefix expression
deepalishinkar1
 
PPTX
steps for template in django for project
deepalishinkar1
 
PDF
Web application on menu card qrcode generator.pdf
deepalishinkar1
 
PPTX
Django model create a table in django web framework
deepalishinkar1
 
PPTX
TO DO APP USING STREAMLIT PYTHON PROJECT
deepalishinkar1
 
PPTX
basic concepts of object oriented in python
deepalishinkar1
 
PDF
Inheritance and polymorphism oops concepts in python
deepalishinkar1
 
PPTX
DATABASE CONNECTIVITY PYTHON USING MYSQL/SQLITE/POSTGRE
deepalishinkar1
 
PPTX
File Operations in python Read ,Write,binary file etc.
deepalishinkar1
 
PDF
How to create a django project procedure
deepalishinkar1
 
PDF
Virtual environment in python on windows / linux os
deepalishinkar1
 
PPTX
Operators in python
deepalishinkar1
 
PPTX
Data handling in python
deepalishinkar1
 
PDF
Practical approach on numbers system and math module
deepalishinkar1
 
PDF
Demonstration on keyword
deepalishinkar1
 
PPTX
Numbers and math module
deepalishinkar1
 
PPTX
Basic concepts of python
deepalishinkar1
 
PPTX
Introduction to python
deepalishinkar1
 
PDF
Set methods in python
deepalishinkar1
 
data structure stack appplication in python
deepalishinkar1
 
Stack application in infix to prefix expression
deepalishinkar1
 
steps for template in django for project
deepalishinkar1
 
Web application on menu card qrcode generator.pdf
deepalishinkar1
 
Django model create a table in django web framework
deepalishinkar1
 
TO DO APP USING STREAMLIT PYTHON PROJECT
deepalishinkar1
 
basic concepts of object oriented in python
deepalishinkar1
 
Inheritance and polymorphism oops concepts in python
deepalishinkar1
 
DATABASE CONNECTIVITY PYTHON USING MYSQL/SQLITE/POSTGRE
deepalishinkar1
 
File Operations in python Read ,Write,binary file etc.
deepalishinkar1
 
How to create a django project procedure
deepalishinkar1
 
Virtual environment in python on windows / linux os
deepalishinkar1
 
Operators in python
deepalishinkar1
 
Data handling in python
deepalishinkar1
 
Practical approach on numbers system and math module
deepalishinkar1
 
Demonstration on keyword
deepalishinkar1
 
Numbers and math module
deepalishinkar1
 
Basic concepts of python
deepalishinkar1
 
Introduction to python
deepalishinkar1
 
Set methods in python
deepalishinkar1
 
Ad

Recently uploaded (20)

PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PPTX
Quarter 1_PPT_PE & HEALTH 8_WEEK 3-4.pptx
ronajadolpnhs
 
PDF
Geographical Diversity of India 100 Mcq.pdf/ 7th class new ncert /Social/Samy...
Sandeep Swamy
 
PPTX
Controller Request and Response in Odoo18
Celine George
 
PPTX
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
PDF
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
PPTX
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
PPTX
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PPTX
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PPT
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
PDF
Horarios de distribución de agua en julio
pegazohn1978
 
PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PDF
Characteristics, Strengths and Weaknesses of Quantitative Research.pdf
Thelma Villaflores
 
PPTX
EDUCATIONAL MEDIA/ TEACHING AUDIO VISUAL AIDS
Sonali Gupta
 
PPTX
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
PPTX
QUARTER 1 WEEK 2 PLOT, POV AND CONFLICTS
KynaParas
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
Quarter 1_PPT_PE & HEALTH 8_WEEK 3-4.pptx
ronajadolpnhs
 
Geographical Diversity of India 100 Mcq.pdf/ 7th class new ncert /Social/Samy...
Sandeep Swamy
 
Controller Request and Response in Odoo18
Celine George
 
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
Dimensions of Societal Planning in Commonism
StefanMz
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
Horarios de distribución de agua en julio
pegazohn1978
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
Characteristics, Strengths and Weaknesses of Quantitative Research.pdf
Thelma Villaflores
 
EDUCATIONAL MEDIA/ TEACHING AUDIO VISUAL AIDS
Sonali Gupta
 
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
QUARTER 1 WEEK 2 PLOT, POV AND CONFLICTS
KynaParas
 
Ad

Data Structure Stack operation in python

  • 2. STACK • A stack is a linear data structure that follows the principle of Last In First Out (LIFO). This means the last element inserted inside the stack is removed first. • Here you can: Put a new plate on top Remove the top plate And, if you want the plate at the bottom, you must first remove all the plates on top. This is exactly how the stack data structure works. • LIFO (Last In First Out) principal, putting an item on top of the stack is called push and removing an item is called pop.
  • 4. BASIC OPERATIONS OF STACK There are some basic operations that allow us to perform different actions on a stack. • Push: Add an element to the top of a stack • Pop: Remove an element from the top of a stack • IsEmpty: Check if the stack is empty • IsFull: Check if the stack is full • size() - It returns the length of the stack. • Peek or top: Get the value of the top element without removing it
  • 5. IMPLEMENTATION OF STACK IN PYTHON Following ways are: • List • deque • LifoQueue
  • 6. LIST OPERATIONS FOR STACK • Python list can be used as the stack. • It uses the append() method to insert elements to the list where stack uses the push() method. • The list also provides the pop() method to remove the last element. • To return size of stack ,python provide function len().
  • 7. IMPLEMENTATION USING COLLECTION.DEQUE • The deque is pronounced as the "deck" which means "double-ended queue". • The collection module provides the deque class, which is used to creating Python stacks. • The deque can be preferred over the list because it performs append and pop operation faster than the list.
  • 8. IMPLEMENTATION USING QUEUE MODULE • The queue module has the LIFO queue, which is the same as the stack. Generally, the queue uses the put() method to add the data . Below are a few methods that available in the queue. • empty() - If queue empty, returns true; otherwise return false. • maxsize() - This method is used to set the maximum number of elements allowed in the queue. • qsize() - It returns the size of the queue.
  • 9. IMPLEMENTATION USING QUEUE MODULE • get() - It returns and removes the element from the queue. Sometime. The queue can be empty; it waits until element is available. • full() - It returns True if the queue is full. The queue is defined as maxsize = 0 by default. In this case, it will not return True. • put(item) - It adds the element to the queue; if the queue is full, wait until space is available. • put_nowait(item) - It adds the element into the queue without delaying.
  • 10. LINKED LIST • A linked list is a sequence of data elements, which are connected together via links. Each data element contains a connection to another data element in form of a pointer. • Python does not have linked lists in its standard library. We implement the concept of linked lists using the concept of nodes.
  • 11. LINKED LIST • A linked list is a linear data structure that includes a series of connected nodes. Here, each node stores the data and the address of the next node. For example,
  • 12. SINGLE LINKED LIST • In this type of data structure there is only one link between any two data elements. We create such a list and create additional methods to insert, update and remove elements from the list.
  • 13. NOTE 1.Every Python module has it’s __name__ defined and if this is ‘__main__’, it implies that the module is being run standalone by the user and we can do corresponding appropriate actions. 2.If you import this script as a module in another script, the __name__ is set to the name of the script/module. 3.Python files can act as either reusable modules, or as standalone programs. 4.if __name__ == “__main__”: is used to execute some code only if the file was run directly, and not imported.