Exp 3 - 6 (A)
Exp 3 - 6 (A)
Aim: Write a Python program to implement List ADT using Python arrays.
Algorithm :
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 :
Result:
Thus, the above program for implementing List ADT using Python arrays was
successfully completed.
EX. NO: 4 Linked list Implementations of List
Algorithm :
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
Algorithm:
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
Algorithm:
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
Algorithm:
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.