SlideShare a Scribd company logo
1 of 10
“DATA SCIENCE AND ALGORITHM”
NAME: FAISAL MUJAHID
ROLL NO: SU91-BSBIM-F22-032
SUBMITTED TO: MA`AM ZAHIRA ILYAS
2
“INTRODUCTION”
TOPIC: Stack
Certainly! A stack is a fundamental data structure that follows the Last-In-First-
Out (LIFO) principle. It means that the most recently added element is the first one
to be removed. Here's a Python implementation of a stack along with a brief
description of each method:
CODING OF MERGE SORT
class Stack:
def __init__(self):
self.items = []
def is_empty(self):
"""Check if the stack is empty.""“
return len(self.items) == 0
def push(self, item):
"""Add an item to the top of the stack."""
3
1
self.items.append(item)
def pop(self):
""" Remove and return the item at the top of the stack. Raises an IndexError if
the stack is empty. ""“
if not self.is_empty():
return self.items.pop()
else:
raise IndexError("Stack is empty")
def peek(self):
""" Return the item at the top of the stack without removing it. Raises an IndexError if the
stack is empty. """
4
2
if not self.is_empty():
return self.items[-1]
else:
raise IndexError("Stack is empty")
def size(self):
"""Return the number of elements in the stack.""“
return len(self.items)
# Example usage:
stack = Stack()
stack.push(1)
stack.push(2)
stack.push(3) 5
3
print("Stack:", stack.items) # Output: Stack: [1, 2, 3]
print("Size of stack:", stack.size()) # Output: Size of stack: 3
print("Top of stack:", stack.peek()) # Output: Top of stack: 3
print("Popped item:", stack.pop()) # Output: Popped item: 3
print("Stack after pop:", stack.items) # Output: Stack after pop: [1, 2]
6
4
Description:
__init__(self):
The constructor initializes an empty list to store the stack elements.
is_empty(self):
Checks if the stack is empty.
push(self, item):
Adds an item to the top of the stack by appending it to the list.
pop(self):
Removes and returns the top item from the stack, raising an exception if the stack
is empty.
7
6
peek(self):
Returns the top item without removing it, raising an exception if the stack is
empty.
size(self):
Returns the number of elements in the stack.
8
7
Conclusion:
In conclusion, the Python code provided implements a stack, a fundamental
data structure that follows the Last-In-First-Out (LIFO) principle. The stack is
represented as a list, and various methods are defined to manipulate it. The
push method adds elements to the top of the stack, the pop method
removes and returns the top element, the peek method allows you to view
the top element without removing it, and the size method returns the
number of elements in the stack.
9
8
The code demonstrates the basic operations of a stack and how it can be
used to manage a collection of items in a manner where the most recently
added item is the first to be processed or removed. Understanding stacks is
crucial in computer science and programming for tasks like parsing
expressions, backtracking algorithms, and managing function calls in
recursion, among many others.
10
9

More Related Content

Similar to stack coding.pptx

Basic data structures in python
Basic data structures in pythonBasic data structures in python
Basic data structures in pythonLifna C.S
 
Difference between stack and queue
Difference between stack and queueDifference between stack and queue
Difference between stack and queuePulkitmodi1998
 
(674335607) cs2309 java-lab-manual
(674335607) cs2309 java-lab-manual(674335607) cs2309 java-lab-manual
(674335607) cs2309 java-lab-manualChandrapriya Jayabal
 
23 stacks-queues-deques
23 stacks-queues-deques23 stacks-queues-deques
23 stacks-queues-dequesRishabh Jindal
 
01-intro_stacks.ppt
01-intro_stacks.ppt01-intro_stacks.ppt
01-intro_stacks.pptsoniya555961
 
Introduction and BackgroundIn recent lectures we discussed usi.pdf
Introduction and BackgroundIn recent lectures we discussed usi.pdfIntroduction and BackgroundIn recent lectures we discussed usi.pdf
Introduction and BackgroundIn recent lectures we discussed usi.pdfarpitaeron555
 
Chapter 5-stack.pptx
Chapter 5-stack.pptxChapter 5-stack.pptx
Chapter 5-stack.pptxHalid Assen
 
stack 1.pdf
stack 1.pdfstack 1.pdf
stack 1.pdfhafsa40
 
stack_presentaton_HUSNAIN[2].pojklklklptx
stack_presentaton_HUSNAIN[2].pojklklklptxstack_presentaton_HUSNAIN[2].pojklklklptx
stack_presentaton_HUSNAIN[2].pojklklklptxHusnainNaqvi2
 
Objectives- In this lab- students will practice- 1- Stack with single.pdf
Objectives- In this lab- students will practice- 1- Stack with single.pdfObjectives- In this lab- students will practice- 1- Stack with single.pdf
Objectives- In this lab- students will practice- 1- Stack with single.pdfAugstore
 
Stack operation algorithms with example
Stack operation algorithms with exampleStack operation algorithms with example
Stack operation algorithms with exampleNamanKikani
 
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority QueueWhat is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority QueueBalwant Gorad
 
Data Structure.pptx
Data Structure.pptxData Structure.pptx
Data Structure.pptxSajalFayyaz
 

Similar to stack coding.pptx (20)

Basic data structures in python
Basic data structures in pythonBasic data structures in python
Basic data structures in python
 
Difference between stack and queue
Difference between stack and queueDifference between stack and queue
Difference between stack and queue
 
(674335607) cs2309 java-lab-manual
(674335607) cs2309 java-lab-manual(674335607) cs2309 java-lab-manual
(674335607) cs2309 java-lab-manual
 
23 stacks-queues-deques
23 stacks-queues-deques23 stacks-queues-deques
23 stacks-queues-deques
 
01-intro_stacks.ppt
01-intro_stacks.ppt01-intro_stacks.ppt
01-intro_stacks.ppt
 
U3.stack queue
U3.stack queueU3.stack queue
U3.stack queue
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
 
Introduction and BackgroundIn recent lectures we discussed usi.pdf
Introduction and BackgroundIn recent lectures we discussed usi.pdfIntroduction and BackgroundIn recent lectures we discussed usi.pdf
Introduction and BackgroundIn recent lectures we discussed usi.pdf
 
Chapter 5-stack.pptx
Chapter 5-stack.pptxChapter 5-stack.pptx
Chapter 5-stack.pptx
 
stack 1.pdf
stack 1.pdfstack 1.pdf
stack 1.pdf
 
Stack - Operations and Applications
Stack - Operations and ApplicationsStack - Operations and Applications
Stack - Operations and Applications
 
stack_presentaton_HUSNAIN[2].pojklklklptx
stack_presentaton_HUSNAIN[2].pojklklklptxstack_presentaton_HUSNAIN[2].pojklklklptx
stack_presentaton_HUSNAIN[2].pojklklklptx
 
Objectives- In this lab- students will practice- 1- Stack with single.pdf
Objectives- In this lab- students will practice- 1- Stack with single.pdfObjectives- In this lab- students will practice- 1- Stack with single.pdf
Objectives- In this lab- students will practice- 1- Stack with single.pdf
 
Algo>Stacks
Algo>StacksAlgo>Stacks
Algo>Stacks
 
Stack.pptx
Stack.pptxStack.pptx
Stack.pptx
 
Stack operation algorithms with example
Stack operation algorithms with exampleStack operation algorithms with example
Stack operation algorithms with example
 
Data structure Stack
Data structure StackData structure Stack
Data structure Stack
 
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority QueueWhat is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
 
Data Structure.pptx
Data Structure.pptxData Structure.pptx
Data Structure.pptx
 
notes.pdf
notes.pdfnotes.pdf
notes.pdf
 

Recently uploaded

Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...RKavithamani
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 

Recently uploaded (20)

Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 

stack coding.pptx

  • 1. “DATA SCIENCE AND ALGORITHM” NAME: FAISAL MUJAHID ROLL NO: SU91-BSBIM-F22-032 SUBMITTED TO: MA`AM ZAHIRA ILYAS
  • 2. 2 “INTRODUCTION” TOPIC: Stack Certainly! A stack is a fundamental data structure that follows the Last-In-First- Out (LIFO) principle. It means that the most recently added element is the first one to be removed. Here's a Python implementation of a stack along with a brief description of each method:
  • 3. CODING OF MERGE SORT class Stack: def __init__(self): self.items = [] def is_empty(self): """Check if the stack is empty.""“ return len(self.items) == 0 def push(self, item): """Add an item to the top of the stack.""" 3 1
  • 4. self.items.append(item) def pop(self): """ Remove and return the item at the top of the stack. Raises an IndexError if the stack is empty. ""“ if not self.is_empty(): return self.items.pop() else: raise IndexError("Stack is empty") def peek(self): """ Return the item at the top of the stack without removing it. Raises an IndexError if the stack is empty. """ 4 2
  • 5. if not self.is_empty(): return self.items[-1] else: raise IndexError("Stack is empty") def size(self): """Return the number of elements in the stack.""“ return len(self.items) # Example usage: stack = Stack() stack.push(1) stack.push(2) stack.push(3) 5 3
  • 6. print("Stack:", stack.items) # Output: Stack: [1, 2, 3] print("Size of stack:", stack.size()) # Output: Size of stack: 3 print("Top of stack:", stack.peek()) # Output: Top of stack: 3 print("Popped item:", stack.pop()) # Output: Popped item: 3 print("Stack after pop:", stack.items) # Output: Stack after pop: [1, 2] 6 4
  • 7. Description: __init__(self): The constructor initializes an empty list to store the stack elements. is_empty(self): Checks if the stack is empty. push(self, item): Adds an item to the top of the stack by appending it to the list. pop(self): Removes and returns the top item from the stack, raising an exception if the stack is empty. 7 6
  • 8. peek(self): Returns the top item without removing it, raising an exception if the stack is empty. size(self): Returns the number of elements in the stack. 8 7
  • 9. Conclusion: In conclusion, the Python code provided implements a stack, a fundamental data structure that follows the Last-In-First-Out (LIFO) principle. The stack is represented as a list, and various methods are defined to manipulate it. The push method adds elements to the top of the stack, the pop method removes and returns the top element, the peek method allows you to view the top element without removing it, and the size method returns the number of elements in the stack. 9 8
  • 10. The code demonstrates the basic operations of a stack and how it can be used to manage a collection of items in a manner where the most recently added item is the first to be processed or removed. Understanding stacks is crucial in computer science and programming for tasks like parsing expressions, backtracking algorithms, and managing function calls in recursion, among many others. 10 9