0% found this document useful (0 votes)
466 views

Exp 3 - 6 (A)

The document describes a Python program to implement balancing of symbols using a stack. It defines opening and closing brackets/braces/parentheses. The program pushes opening symbols to a stack and pops them, checking for matching closing symbols. If the stack is empty after traversing the string, it is balanced, otherwise it is unbalanced. Examples testing balanced and unbalanced strings are provided. The program successfully checks for balanced symbols using a stack implementation.

Uploaded by

Mr Movie
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)
466 views

Exp 3 - 6 (A)

The document describes a Python program to implement balancing of symbols using a stack. It defines opening and closing brackets/braces/parentheses. The program pushes opening symbols to a stack and pops them, checking for matching closing symbols. If the stack is empty after traversing the string, it is balanced, otherwise it is unbalanced. Examples testing balanced and unbalanced strings are provided. The program successfully checks for balanced symbols using a stack implementation.

Uploaded by

Mr Movie
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/ 10

EX.

NO: 3 Implement List ADT using Python arrays

Aim: Write a Python program to implement List ADT using Python arrays.

Algorithm :

Step-1 Start the Program


Step-2 Import the copy library
Step-3 Create an array using range function
Step-4 Append the array
Step-5 Insert an element in a specified position
Step-6 Remove an element
Step-7 Printing the array in reverse order
Step-8 Stop the program

Program:

import array
arr = array.array('i', [1, 2, 3])
print ("The new created array is : ",end=" ")
for i in range (0, 3):
print (arr[i], end=" ")
print("\r")
arr.append(4)
print("The appended array is : ", end="")
for i in range (0, 4):
print (arr[i], end=" ")
arr.insert(2, 5)
print("\r")
print ("The array after insertion is : ", end="")
for i in range (0, 5):
print (arr[i], end=" ")
print("\r")
print ("The popped element is : ",end="")
print (arr.pop(2));
# printing array after popping
print ("The array after popping is : ",end="")
for i in range (0,4):
print (arr[i],end=" ")
print("\r")
# using remove() to remove 1st occurrence of 1
arr.remove(1)
# printing array after removing
print ("The array after removing is : ",end="")
for i in range (0,3):
print (arr[i],end=" ")
arr.reverse()
# printing array after reversing
print("\r")
print ("The array after reversing is : ",end="")
for i in range (0,3):
print (arr[i],end=" ")

Output :

The new created array is : 1 2 3

The appended array is : 1 2 3 4

The array after insertion is : 1 2 5 3 4

The popped element is : 5


The array after popping is : 1

The array after removing is : 2 3 4

The array after reversing is : 4 3 2

Result:
Thus, the above program for implementing List ADT using Python arrays was
successfully completed.
EX. NO: 4 Linked list Implementations of List

Aim: Write a Python Program for Linked list Implementations of List.

Algorithm :

Step-1 Start the Program


Step-2 Declare the class
Step-3 Insert the elements in the list
Step-4 Display the elements present in the list
Step-5 Creating a header node
Step-6 Convert the given array to list
Step-7 Stop the program

Program:

class Node:
def __init__(self, data):
self.data = data
self.next = next
# Function to insert Node
def insert(root, item):
temp = Node(0)
temp.data = item
temp.next = root
root = temp
return root
def display(root):
while (root != None):
print(root.data, end=" ")
root = root.next
def arrayToList(arr, n):
root = None
for i in range(n - 1, -1, -1):
root = insert(root, arr[i])
return root
# Driver code
if __name__ == '__main__':
arr = [1, 2, 3, 4, 5];
n = len(arr)
root = arrayToList(arr, n);
display(root)
Output :

12345

Result:
Thus, the above program for program for Linked list implementation of List was
successfully completed.
EX. NO: 5 a) Implementation of Stack & Queue ADTs
Implementation of Stack ADT

Aim: Write a Python Program to implement Stack ADT.

Algorithm:

Step 1: Start the program


Step 2: Create a class Stack with instance variable items initialized to an empty list.
Step 3: Define methods push, pop and is_empty inside the class Stack.
Step 4: The method push appends data to items.
Step 5: The method pop pops the first element in items.
Step 6: The method is_empty returns True only if items is empty.
Step 7: Create an instance of Stack and present a menu to the user to perform
operations on thestack.
Step 8: Stop the program

Program:

class Stack:
def __init__(self):
self.items = []
def is_empty(self):
return self.items == []
def push(self, data):
self.items.append(data)
def pop(self):
return self.items.pop()
s = Stack()
while True:
print('push <value>')
print('pop')
print('quit')
do = input('What would you like to do? ').split()
operation = do[0].strip().lower()
if operation == 'push':
s.push(int(do[1]))
elif operation == 'pop':
if s.is_empty():
print('Stack is empty.')
else:
print('Popped value: ', s.pop())
elif operation == 'quit':
break
Output :
push <value>
pop
quit
What would you like to do? push 5
push <value>
pop
quit
What would you like to do? push 3
push <value>
pop
quit
What would you like to do? pop
Popped value: 3
push <value>
pop
quit
What would you like to do? pop
Popped value: 5
push <value>
pop
quit
What would you like to do? pop
Stack is empty.
push <value>
pop
quit
What would you like to do? quit

Result:
Thus, the above program for Stack ADT was successfully completed.
EX. NO: 5 b) Implementation of Stack & Queue ADTs
Implementation of Queue ADT

Aim: Write a Python Program to implement Queue ADT.

Algorithm:

Step 1: Start the program


Step 2: Create a class Queue with instance variable items initialized to an empty list.
Step 3: Define methods enqueue, dequeue and is_empty inside the class Queue.
Step 4: The method enqueue appends data to items.
Step 5: The method dequeue dequeues the first element in items.
Step 6: The method is_empty returns True only if items is empty.
Step 7: Create an instance of Queue and present a menu to the user to perform
operations on thequeue.
Step 8: Stop the program

Program:

class Queue:
def __init__(self):
self.items = []
def is_empty(self):
return self.items == []
def enqueue(self, data):
self.items.append(data)
def dequeue(self):
return self.items.pop(0)
q = Queue()
while True:
print('enqueue <value>')
print('dequeue')
print('quit')
do = input('What would you like to do? ').split()
operation = do[0].strip().lower()
if operation == 'enqueue':
q.enqueue(int(do[1]))
elif operation == 'dequeue':
if q.is_empty():
print('Queue is empty.')
else:
print('Dequeued value: ', q.dequeue())
elif operation == 'quit':
break
Output :

enqueue <value>
dequeue
quit
What would you like to do? enqueue5
enqueue <value>
dequeue
quit
What would you like to do? enqueue 4
enqueue <value>
dequeue
quit
What would you like to do? enqueue 3
enqueue <value>
dequeue
quit
What would you like to do? enqueue 6
enqueue <value>
dequeue
quit
What would you like to do? dequeue
Dequeued value: 4
enqueue <value>
dequeue
quit
What would you like to do? quit

Result:
Thus, the above program for Queue ADT was successfully completed.
EX. NO: 6 a) Application of Stack & Queue ADTs
Implementation of Balancing Symbols

Aim: Write a Python Program to implement Balancing Symbols using Stack.

Algorithm:

Step 1: Start the program


Step 2: Declare a character stack S.
Step 3: Now traverse the expression string exp.
Step 4: If the current character is a starting or then push it to stack.
Step 5: If the current character is a closing bracket or then pop from stack
and if thepopped character is the matching starting bracket then fine else brackets are
not balanced.
Step 6: After

Step 7: Stop the program

Program:

open_list = ["[","{","("]
close_list = ["]","}",")"]
# Function to check parentheses
def check(myStr):
stack = []
for i in myStr:
if i in open_list:
stack.append(i)
elif i in close_list:
pos = close_list.index(i)
if ((len(stack) > 0) and
(open_list[pos] == stack[len(stack)-1])):
stack.pop()
else:
return "Unbalanced"
if len(stack) == 0:
return "Balanced"
else:
return "Unbalanced"
# Driver code
string = "{[]{()}}"
print(string,"-", check(string))
string = "[{}{})(]"
print(string,"-", check(string))
string = "((()"
Output :

{[]{()}} - Balanced
[{}{})(] Unbalanced
((() - Unbalanced

Result:
Thus, the above program for implementing Balancing Symbols using Stack was
successfully completed.

You might also like