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

Stack Data Structure 2

The document provides Python implementations for various stack operations, including adding and deleting clients, storing odd and even numbers, and basic stack manipulation functions. It includes examples of pushing and popping elements while maintaining the largest number in the stack. Additionally, it outlines user interaction for inserting and deleting elements from the stack based on user choices.

Uploaded by

petrichorclass9b
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 views9 pages

Stack Data Structure 2

The document provides Python implementations for various stack operations, including adding and deleting clients, storing odd and even numbers, and basic stack manipulation functions. It includes examples of pushing and popping elements while maintaining the largest number in the stack. Additionally, it outlines user interaction for inserting and deleting elements from the stack based on user choices.

Uploaded by

petrichorclass9b
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

STACK DATA STRUCTURE

1. Write AddClient(Client) and DeleteClient(Client) methods in


Python to add a new client and delete a client from a list client
name, considering them to act as insert and delete operations of
the stack data structure.

# Create an empty list to store the client names


client_list = []
def AddClient(client):
"""
Adds a new client to the client list (stack).
"""
client_list.append(client)

def DeleteClient(client):
"""
Deletes a client from the client list (stack) if it exists.
"""
if client in client_list:
client_list.remove(client)
else:
print(f"Client '{client}' not found in the list.")
# Example usage
AddClient("Ram")
AddClient("Kamal")
AddClient("Jhon")
print(client_list)

DeleteClient("Alice")
print(client_list)
DeleteClient("Eve")
print(client_list)

DeleteClient("Jhon")
print(client_list) #
DeleteClient("Eve")

2. Write a program to create a Stack for storing only odd numbers


out of all the numbers entered by the user. Display the content of
the Stack along with the largest odd number in the Stack. (Hint.
Keep popping out the elements from stack and maintain the
largest element retrieved so far in a variable. Repeat till Stack is
empty)

# Stack implementation using a list


stack = []

def Push(element):
[Link](element)

def Pop():
if not IsEmpty():
return [Link]()

def IsEmpty():
return len(stack) == 0
# Get input from the user
num_count = int(input("Enter the number of elements: "))

# Process the input


for _ in range(num_count):
num = int(input("Enter a number: "))
if num % 2 != 0: # Check if the number is odd
Push(num)
largest_odd = None

# Display the content of the stack and find the largest odd number
print("Stack content:")
while not IsEmpty():
num = Pop()
print(num)
if largest_odd is None or num > largest_odd:
largest_odd = num

print("Largest odd number:", largest_odd)

3. Write a program to create a Stack for storing only even numbers


out of all the numbers entered by the user. Display the content of
the Stack along with the largest even number in the Stack. (Hint.
Keep popping out the elements from stack and maintain the
largest element retrieved so far in a variable. Repeat till Stack is
empty)
4. Write a program to insert or delete an element from a stack
depending upon the user’s choice. The elements are not shifted
after insertion or deletion.

# Stack implementation using a list


stack = []

def Push(element):
[Link](element)

def Pop():
if not IsEmpty():
return [Link]()

def IsEmpty():
return len(stack) == 0

# Function to display the content of the stack


def DisplayStack():
if IsEmpty():
print("Stack is empty.")
else:
print("Stack content:")
for element in reversed(stack):
print(element)

# Program execution
while True:
print("1. Insert element")
print("2. Delete element")
print("3. Display stack")
print("4. Exit")
choice = int(input("Enter your choice: "))

if choice == 1:
element = input("Enter the element to insert: ")
Push(element)
print("Element inserted.")
elif choice == 2:
if IsEmpty():
print("Stack is empty. Cannot delete element.")
else:
element = Pop()
print("Deleted element:", element)
elif choice == 3:
DisplayStack()
elif choice == 4:
print("Exiting program.")
break
else:
print("Invalid choice. Please try again.")

5. Write a function, INSERTQ(Arr,data) and DELETEQ(Arr) for


performing insertion and deletion operation in a stack. Arr is the
list used for implementing stack and data is the value to be
inserted.
def INSERTQ(Arr, data):
[Link](data)
print("Element", data, "inserted into the stack.")

def DELETEQ(Arr):
if len(Arr) == 0:
print("Stack is empty. Cannot delete element.")
else:
deleted_element = [Link]()
print("Element", deleted_element, "deleted from the
stack.")

# Example usage:
stack = []
INSERTQ(stack, 10)
INSERTQ(stack, 20)
INSERTQ(stack, 30)
DELETEQ(stack)
DELETEQ(stack)
DELETEQ(stack)
DELETEQ(stack)
6. Write a function in python PUSH(arr), where Arr is a list of
numbers. From this list push all numbers divisible by 5 in to stack
implemented by using a list. Display the stack if it has at least one
element, otherwise display appropriate error message.

def PUSH(Arr):
stack = []
for num in Arr:
if num % 5 == 0:
[Link](num)

if len(stack) > 0:
print("Stack elements:")
for element in stack:
print(element)
else:
print("No numbers divisible by 5 found. Stack is empty.")

# Example usage:
numbers = [10, 7, 25, 14, 30, 18, 20]
PUSH(numbers)
[Link] functions in python for PushS(List) and for PopS(List) for
performing Push and Pop operations with a stack of list containing
integers.

def PushS(stack, data):


[Link](data)
print("Element", data, "pushed into the stack.")

def PopS(stack):
if len(stack) == 0:
print("Stack is empty. Cannot perform pop operation.")
return None
else:
popped_element = [Link]()
print("Element", popped_element, "popped from the stack.")
return popped_element

# Example usage:
stack = []
PushS(stack, 10)
PushS(stack, 20)
PushS(stack, 30)
PopS(stack)
PopS(stack)
PopS(stack)
PopS(stack)

Note: PRG 5 th and 7th concept is same only.

You might also like