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

Final Output Program

The document contains code for several Python programs covering topics like arrays, linked lists, stacks, queues, binary trees, and basic arithmetic operations. The code includes functions to print array elements, add/remove linked list nodes, push/pop from a stack, enqueue/dequeue from a queue, perform inorder, preorder and postorder tree traversals, and calculate basic arithmetic expressions based on user input. A menu is provided to run different programs by selecting the corresponding option.

Uploaded by

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

Final Output Program

The document contains code for several Python programs covering topics like arrays, linked lists, stacks, queues, binary trees, and basic arithmetic operations. The code includes functions to print array elements, add/remove linked list nodes, push/pop from a stack, enqueue/dequeue from a queue, perform inorder, preorder and postorder tree traversals, and calculate basic arithmetic expressions based on user input. A menu is provided to run different programs by selecting the corresponding option.

Uploaded by

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

def program_1():

###ONE DIMENSIONAL PROGRAMS IN PYTHON###

#1. A program that will print all the elements of the array one by one.

arr = [100, 300, 500, 700]

for element in arr:

print(element)

#2. This program in python will ask the user if how many names to store in the list and will ask again
to input those names and will print it out.

print("How many name to store in the list ? ", end="")

n = input()

arr = []

print("\nEnter", n, "Names: ", end="")

n = int(n)

for i in range(n):

Names = input()

arr.append(Names)

print("\nThe list is:")

for i in range(n):

print(arr[i], end=" ")

### MULTIDIMENSIONAL PROGRAMS IN PYTHON

#1. A program in python that will create a 2D array with 4 rows and 5 columns and will display the first
and third row. And will print also the first row's third element and the third row's forth element.
array=[[5,10,15,20,25],[30,35,40,45,50],[55,60,65,70,75,],[80,85,90,95,100]]

print(array)

print(array[0])

print(array[2])

print(array[0][2])

print(array[2][3])

#2. Another 2D program in python that has three rows and five columns and will delete the row
values in the Third and second row and will print out the remaining rows.

array=[[23,45,43,23,45],[45,67,54,32,45],[89,90,87,65,44]]

del array[2]

del array[1]

print(array)

def program_2():

#LINKEDLIST

class Node:

def __init__(self, data):

self.data = data

self.next = None

class insdelion:

def __init__(self):

self.head = None

self.last_node = None
def append(self, data):

if self.last_node is None:

self.head = Node(data)

self.last_node = self.head

else:

self.last_node.next = Node(data)

self.last_node = self.last_node.next

def deleteNode(self, position):

if self.head is None:

return

index = 0

current = self.head

while current.next and index < position:

previous = current

current = current.next

index += 1

if index < position:

print("\nIndex is out of range.")

elif index == 0:

self.head = self.head.next

else:

previous.next = current.next

# current = None #Optional statement

def display(self):
current = self.head

while current is not None:

print(current.data, end = ' ')

current = current.next

while True:

print()

lstchoice=(input("press'a' for insertion of element\n press 'b' for deletion \n press 'c' to quit"))

if lstchoice=='a':

a_llist = insdelion()

n = int(input('How many elements would you like to add? '))

for i in range(n):

data = (input('Enter an element: '))

a_llist.append(data)

print('The linked list: ', end = '')

a_llist.display();

elif lstchoice=='b':

llist = insdelion()

llist.display()

position=(int(input("enter the index of element to be deleted: ")))

llist.deleteNode(position)

print('\nLinked List after Deletion: ')


llist.display()

elif lstchoice=="c":

break;

else:

print("NO Match")

def program_3():

# Stack implementation in python

# Creating a stack

def create_stack():

stack = []

return stack

# Creating an empty stack

def check_empty(stack):

return len(stack) == 0

# Adding items into the stack

def push(stack, item):

stack.append(item)

print("pushed item: " + item)

# Removing an element from the stack


def pop(stack):

if (check_empty(stack)):

return "stack is empty"

return stack.pop()

stack = create_stack()

push(stack, str(7))

push(stack, str(9))

push(stack, str(13))

push(stack, str(21))

print("popped item: " + pop(stack))

print("stack after popping an element: " + str(stack))

def program_4():

#1. Queue as a list

print("name all your favorite animals")

print("press enter after each animals, r to remove, q to quit")

favs = []

while True:

data = input()

if str.lower(data) == "q":

break

elif str.lower(data) == "r":

print("removing:", favs.pop(0))
else:

favs.append(data)

print(favs)

for animals in favs:

print("You said:", animals)

#2. Queue as a list

queue=[]

def enqueue():

element = input("Enter the element:")

queue.append(element)

print(element, "is added to queue!")

def dequeue():

if not queue:

print("queue is empty!")

else:

e = queue.pop(0)

print("remove element:", e)

def display():

print(queue)

while True:

print("Select the operation 1.add 2.remove 3.show 4.quit")

choice = int(input())

if choice==1:
enqueue()

elif choice==2:

dequeue()

elif choice==3:

display()

elif choice==4:

break

else:

print("Enter the correct operation!")

#1) Basic calculator based on users choice

def program_5():

#Binary tree

def inorder():

# Binary Tree

class Node:

def __init__(self, key):

self.left = None

self.right = None

self.val = key

# A function to do inorder tree traversal

def printInorder(root):

if root:
# First recur on left child

printInorder(root.left)

# then print the data of node

print(root.val),

# now recur on right child

printInorder(root.right)

# Driver code

if __name__ == "__main__":

root = Node(1)

root.left = Node(2)

root.right = Node(3)

root.left.left = Node(4)

root.left.right = Node(5)

# Function call

print ("\nInorder traversal of binary tree is")

printInorder(root)

def preorder():

# Binary Tree

class Node:

def __init__(self, key):

self.left = None

self.right = None
self.val = key

# A function to do preorder tree traversal

def printPreorder(root):

if root:

# First print the data of node

print(root.val),

# Then recur on left child

printPreorder(root.left)

# Finally recur on right child

printPreorder(root.right)

# Driver code

if __name__ == "__main__":

root = Node(1)

root.left = Node(2)

root.right = Node(3)

root.left.left = Node(4)

root.left.right = Node(5)

# Function call

print ("Preorder traversal of binary tree is")

printPreorder(root)
def postorder():

# Binary Tree

class Node:

def __init__(self, key):

self.left = None

self.right = None

self.val = key

# A function to do postorder tree traversal

def printPostorder(root):

if root:

# First recur on left child

printPostorder(root.left)

# the recur on right child

printPostorder(root.right)

# now print the data of node

print(root.val),

# Driver code

if __name__ == "__main__":

root = Node(1)

root.left = Node(2)
root.right = Node(3)

root.left.left = Node(4)

root.left.right = Node(5)

# Function call

print ("\nPostorder traversal of binary tree is")

printPostorder(root)

while True:

print()

case = int(input("choose one\n 1. inorder,\n 2. preorder,\n 3, postorder\n"))

if case==1:

preorder()

break;

elif case==2:

inorder()

break;

elif case==3:

postorder()

break;

else :

print("choose from the list below!!!")

def program_6():

#1) Basic calculator based on users choice

print("Enter Two Numbers: ", end="")

nOne = int(input())

nTwo = int(input())

print("Enter the Operator (+,-,*,/): ", end="")


ch = input()

if ch=='+':

print("\n" +str(nOne)+ " + " +str(nTwo)+ " = " +str(nOne+nTwo))

elif ch=='-':

print("\n" +str(nOne)+ " - " +str(nTwo)+ " = " +str(nOne-nTwo))

elif ch=='*':

print("\n" +str(nOne)+ " * " +str(nTwo)+ " = " +str(nOne*nTwo))

elif ch=='/':

print("\n" +str(nOne)+ " / " +str(nTwo)+ " = " +str(nOne/nTwo))

else:

print("\nInvalid Operator!")

while True:

print()

case = int(input("Select a program to run \n1.array \n2.linked list \n3.stacks \n4.queues \n5.binary
trees\n6.arithmetic operations\n"))

if case==1:

program_1()

break;

elif case==2:

program_2()

break;

elif case==3:

program_3()

break;

elif case==4:

program_4()

break;

elif case==5:
program_5()

break;

elif case==6:

program_6()

break;

else :

print("choose from the list below!!!")

You might also like