0% found this document useful (0 votes)
2 views14 pages

STACK -with list

The document outlines the implementation of a stack using a list in Python, detailing the creation of a Stack class and methods for push, pop, peek, is_empty, and size. It includes a starter activity to create an empty stack, insert elements, and delete one. The document concludes with a recap and prompts for questions and feedback on the lesson.

Uploaded by

taslimbasha31
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views14 pages

STACK -with list

The document outlines the implementation of a stack using a list in Python, detailing the creation of a Stack class and methods for push, pop, peek, is_empty, and size. It includes a starter activity to create an empty stack, insert elements, and delete one. The document concludes with a recap and prompts for questions and feedback on the lesson.

Uploaded by

taslimbasha31
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

STACK

RECAP

Push
operation Pop
operation
STARTER ACTIVITY

Creation of empty stack


After creation insert 5 elements and delete one element
IMPLEMENTATION OF STACK USING LIST
LEARNING OBJECTIVE
Understand the concept of implementation of stack
using list
Step 1: Define the Stack Class

• First, we will create a class called Stack to encapsulate the

stack operations and data.

class Stack:

def __init__(self):
Step:2Implement the push Method

• Next, we implement the push method to add an element to

the stack.

def push(self, element):

self.stack.append(element)
Step 3: Implement the pop Method

• We then implement the pop method to remove and return the top element from the stack.

• def pop(self):

• if not self.is_empty():

• return self.stack.pop()

• else:

• print("Stack is empty")

• return None
Step 4: Implement the peek Method

• We add a peek method to view the top element of the stack without removing it.

• def peek(self):

• if not self.is_empty():

• return self.stack[-1]

• else:

• print("Stack is empty")

• return None
Step 5: Implement the is_empty Method

• We implement the is_empty method to check if the stack is

empty.

• def is_empty(self):

• return len(self.stack) == 0
Step 6: Implement the size Method

• def size(self):

• return len(self.stack)
PLENARY
• Recap all the contents

• Discussions sessions
Any Questions you have? One new syntax you learnt.

Ah Ha Moment!!!!!!!!!! Did you enjoy the lesson?


THANK
YOU……

You might also like