0% found this document useful (0 votes)
5 views38 pages

Python Stack Data Structure Guide

The document provides a detailed overview of a Stack data structure, including its implementation in pseudo code and Python. It describes key operations such as Push, Pop, Peek, IsEmpty, and IsFull, along with example usage. The Stack class is initialized with a capacity and allows for the management of elements in a Last In First Out (LIFO) manner.

Uploaded by

washiqueahmed11
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)
5 views38 pages

Python Stack Data Structure Guide

The document provides a detailed overview of a Stack data structure, including its implementation in pseudo code and Python. It describes key operations such as Push, Pop, Peek, IsEmpty, and IsFull, along with example usage. The Stack class is initialized with a capacity and allows for the management of elements in a Last In First Out (LIFO) manner.

Uploaded by

washiqueahmed11
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

Data Structure

Stack
Dr. YOGESH K M
Assistant Professor
M S Ramaiah University of Applied Science
Dept. of Computer Science and Engineering
Bangalore, Karnataka INDIA - 560 058
[Link]@[Link]
Pseudo Code
Course Summary..
CLASS Stack
INIT Stack(capacity)
stack = ARRAY OF SIZE capacity
top = -1

PROCEDURE Push(value)
IF top < capacity - 1
top = top + 1
stack[top] = value
ELSE
PRINT "Stack is full"

PROCEDURE Pop()
IF top >= 0
value = stack[top]
top = top - 1
RETURN value
ELSE
PRINT "Stack is empty"

PROCEDURE Peek()
IF top >= 0
RETURN stack[top]
ELSE
PRINT "Stack is empty"

PROCEDURE IsEmpty()
RETURN top == -1

PROCEDURE IsFull()
RETURN top == capacity - 1
Python Implementation
Course Summary..
class Stack:
def __init__(self, capacity):
# Initialize the stack with a given capacity
[Link] = capacity
# Create an array to store the elements
[Link] = [None] * capacity
# Initialize the top index to -1
[Link] = -1

def push(self, value):


# Check if the stack is full
if self.is_full():
print("Stack is full. Cannot push element.")
return
# Increment the top index
[Link] += 1
# Add the element to the top of the stack
[Link][[Link]] = value
print(f"Element {value} pushed to the stack.")
Course Summary..
def pop(self):
# Check if the stack is empty
if self.is_empty():
print("Stack is empty. Cannot pop element.")
return
# Store the top element
value = [Link][[Link]]
# Decrement the top index
[Link] -= 1
print(f"Element {value} popped from the stack.")
return value

def peek(self):
# Check if the stack is empty
if self.is_empty():
print("Stack is empty.")
return
# Return the top element
return [Link][[Link]]
Course Summary..
def is_empty(self):
# Check if the stack is empty
return [Link] == -1

def is_full(self):
# Check if the stack is full
return [Link] == [Link] - 1

def print_stack(self):
# Print the elements in the stack
if self.is_empty():
print("Stack is empty.")
return
print("Stack elements:")
for i in range([Link] + 1):
print([Link][i], end=" ")
Course Summary..
Example
stack = Stack(5)
[Link](1)
[Link](2)
[Link](3)
stack.print_stack() # Output: Stack elements: 1 2 3
print("Top element:", [Link]()) # Output: Top element: 3
[Link]()
stack.print_stack() # Output: Stack elements: 1 2
print("Is stack empty?", stack.is_empty()) # Output: Is stack empty? False
print("Is stack full?", stack.is_full()) # Output: Is stack full? False

Explanation
➢ The Stack class is initialized with a given capacity, and an array is created to store the elements.
➢ The push method adds an element to the top of the stack if it's not full.
➢ The pop method removes the top element from the stack if it's not empty.
➢ The peek method returns the top element without removing it.
➢ The is_empty and is_full methods check if the stack is empty or full, respectively.
➢ The print_stack method prints the elements in the stack.

You might also like