0% found this document useful (0 votes)
15 views2 pages

Hernandez Lab Act 6

Uploaded by

harleyherald20
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)
15 views2 pages

Hernandez Lab Act 6

Uploaded by

harleyherald20
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
You are on page 1/ 2

Hernandez, Harley Herald R.

CPE - 301

Lab Activity 6: Implementing a Stack using an Array (List)

Objective:

• To understand the basic operations of a stack (push, pop, peek) using Python lists to represent the stack.

Instructions:

1. Create a class Stack that uses a Python list to store the elements.

2. Implement the following methods in the Stack class:

o push(item) – Add an item to the top of the stack.

o pop() – Remove and return the top item from the stack. Ensure the stack is not empty before
popping.

o peek() – Return the top item from the stack without removing it.

o is_empty() – Return True if the stack is empty, otherwise return False.

o size() – Return the number of elements in the stack.

Task:

• Test your stack by performing the following:

1. Push the elements [5, 10, 15, 20] onto the stack.

2. Print the top element using peek().

3. Pop two elements from the stack and print them.

4. Print the size of the stack after the pops.

You might also like