0% found this document useful (0 votes)
6 views5 pages

Python Array Operations Guide

The document provides Python programs for various data structure operations including arrays, stacks, and queues. It includes detailed implementations for creating, inserting, deleting, updating, and displaying elements in arrays, as well as stack operations like push, pop, and peek, and queue operations using linked lists. Each section includes explanations of the operations and a menu-driven interface for user interaction.
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)
6 views5 pages

Python Array Operations Guide

The document provides Python programs for various data structure operations including arrays, stacks, and queues. It includes detailed implementations for creating, inserting, deleting, updating, and displaying elements in arrays, as well as stack operations like push, pop, and peek, and queue operations using linked lists. Each section includes explanations of the operations and a menu-driven interface for user interaction.
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

Array operations in Python

WAP in python to create an array of 5 integers and perform insertion,deletion, update and
display operation

Here’s a simple Python program (WAP) that creates an array of 5 integers and performs insertion,
deletion, update, and display operations 👇
python

# Program to perform insertion, deletion, update, and display in an array

# Step 1: Create an array of 5 integers


arr = [10, 20, 30, 40, 50]

def display():
print("Current Array:", arr)

def insert_element():
pos = int(input("Enter position to insert (0-{}): ".format(len(arr))))
val = int(input("Enter value to insert: "))
[Link](pos, val)
display()

def delete_element():
pos = int(input("Enter position to delete (0-{}): ".format(len(arr)-1)))
if 0 <= pos < len(arr):
[Link](pos)
else:
print("Invalid position!")
display()

def update_element():
pos = int(input("Enter position to update (0-{}): ".format(len(arr)-1)))
if 0 <= pos < len(arr):
val = int(input("Enter new value: "))
arr[pos] = val
else:
print("Invalid position!")
display()

# Main Menu
while True:
print("\n--- Array Operations ---")
print("1. Display Array")
print("2. Insert Element")
print("3. Delete Element")
print("4. Update Element")
print("5. Exit")

choice = int(input("Enter your choice: "))

if choice == 1:
display()
elif choice == 2:
insert_element()
elif choice == 3:
delete_element()
elif choice == 4:
update_element()
elif choice == 5:
print("Exiting Program...")
break

Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 1/5
else:
print("Invalid choice! Try again.")

🔍 Explanation:
Array initialization: Starts with [10, 20, 30, 40, 50].

Insert: Adds a new element at a user-given position.


Delete: Removes element at a user-given position.

Update: Changes the value at a given index.


Display: Prints the current array.

Would you like me to make it menu-less (only one-time execution without user input) — for exam or
short-answer format?

WAP to implement a stack using array of fixed [Link] Push (), Pop(), display() and peek()
operation

Here’s a clean and exam-ready Python program to implement a Stack using an array (fixed size) and
perform Push(), Pop(), Display(), and Peek() operations 👇
python

# Program to implement Stack using array of fixed size

# Step 1: Create an empty stack


stack = []
MAX_SIZE = 5 # fixed size stack

# Function to push element into stack


def push():
if len(stack) == MAX_SIZE:
print("Stack Overflow! Cannot push element.")
else:
val = int(input("Enter value to push: "))
[Link](val)
print(val, "pushed into stack.")

# Function to pop element from stack


def pop():
if not stack:
print("Stack Underflow! No element to pop.")
else:
val = [Link]()
print(val, "popped from stack.")

# Function to display all elements


def display():
if not stack:
print("Stack is empty.")
else:
print("Current Stack (top to bottom):", stack[::-1])

# Function to peek top element


def peek():
if not stack:
print("Stack is empty.")
else:
print("Top element is:", stack[-1])

Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 2/5
# Main Menu
while True:
print("\n--- Stack Operations ---")
print("1. Push")
print("2. Pop")
print("3. Peek")
print("4. Display")
print("5. Exit")

choice = int(input("Enter your choice: "))

if choice == 1:
push()
elif choice == 2:
pop()
elif choice == 3:
peek()
elif choice == 4:
display()
elif choice == 5:
print("Exiting Program...")
break
else:
print("Invalid choice! Try again.")

⚙️ Explanation:
Stack is implemented using a Python list.

MAX_SIZE = 5 defines a fixed size stack.


push() → Adds element if stack isn’t full.

pop() → Removes top element if stack isn’t empty.

peek() → Shows top element without removing it.

display() → Prints the stack from top to bottom.

Would you like me to make a short version (without user menu, just function calls) suitable for a 5–10
line exam answer?

Wap in python for Implementation of queue using Linked list

Here’s a Python program to implement a Queue using a Linked List — with enqueue, dequeue, and
display operations 👇
python

# Program to implement Queue using Linked List

# Node class
class Node:
def __init__(self, data):
[Link] = data
[Link] = None

# Queue class using Linked List


class Queue:
def __init__(self):
[Link] = None

Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 3/5
[Link] = None

# Enqueue operation
def enqueue(self, data):
new_node = Node(data)
if [Link] is None: # Queue is empty
[Link] = [Link] = new_node
else:
[Link] = new_node
[Link] = new_node
print(f"{data} enqueued into queue.")

# Dequeue operation
def dequeue(self):
if [Link] is None:
print("Queue Underflow! No element to dequeue.")
return
temp = [Link]
[Link] = [Link]
if [Link] is None:
[Link] = None
print(f"{[Link]} dequeued from queue.")

# Display operation
def display(self):
if [Link] is None:
print("Queue is empty.")
return
temp = [Link]
print("Queue elements:", end=" ")
while temp:
print([Link], end=" ")
temp = [Link]
print()

# Main Program
q = Queue()

while True:
print("\n--- Queue Operations ---")
print("1. Enqueue")
print("2. Dequeue")
print("3. Display")
print("4. Exit")
ch = int(input("Enter your choice: "))

if ch == 1:
val = int(input("Enter value to enqueue: "))
[Link](val)
elif ch == 2:
[Link]()
elif ch == 3:
[Link]()
elif ch == 4:
print("Exiting Program...")
break
else:
print("Invalid choice! Try again.")

⚙️ Explanation:
Node → Represents each element in the queue (stores data and next pointer).
Queue → Maintains front (for deletion) and rear (for insertion).
enqueue() → Adds an element at the rear.

dequeue() → Removes an element from the front.


display() → Shows all elements from front to rear.

Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 4/5
Would you like a short, exam-type version (without menu and inputs — just demonstration)?

Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 5/5

You might also like