Professional Documents
Culture Documents
DSP Record 2 (1)
DSP Record 2 (1)
Code
'''class Node:
def __init__(self, data=None, next=None):
self.data = data
self.next=next
class LinkedList:
def __init__(self):
self.head = None
def insert(self, data):
newNode = Node(data)
if (self.head):
current = self.head
while (current.next):
current = current.next
current.next = newNode
else:
self.head = newNode
def searchlist(self, value):
position = 0
found = 0
if self.head is None:
print("The linked list does not exist")
else:
temp_node = self.head
while temp_node is not None:
position = position + 1
if temp_node.data == value:
print("the required value was found at position:" + str(position))
found = 1
temp_node = temp_node.next
if found == 0:
print("the required value does not exist in the list")
def printLL(self):
current = self.head
while (current):
print(current.data)
current = current.next
def atBeg(self,value):
new_node = Node(value)
new_node.next = self.head
self.head = new_node
def delEnd(self):
if (self.head == None):
return
elif (self.head.next == None):
self.head = None
return
else:
temp_node = self.head
while ((temp_node.next).next is not None):
temp_node = temp_node.next
print('deleted item = ', (temp_node.next).data)
temp_node.next = None
return
LL = LinkedList()
LL.insert(9)
LL.insert(98)
LL.insert("welcome")
LL.insert(23)
print("contents of list:")
LL.printLL()
LL.searchlist(98)
LL.atBeg(5)
print("Contest of list:")
LL.printLL()
LL.delEnd()
LL.delEnd()
print("Contest of list:")
LL.printLL()
Output
contents of list:
9
98
welcome
23
the required value does not exist in the list
the required value was found at position:2
Contest of list:
5
9
98
welcome
23
deleted item = 23
deleted item = welcome
Contest of list:
5
9
98
list:
9
98
welcome
23
5
def push():
n=int(input("enter the number:"))
if len(stack)==0:
stack.append(n)
else:
stack.insert(0,n)
print(n,"is inserted")
def pop():
if len(stack)==0:
print("stack is empty:")
else:
print(stack[0],"is deleted:")
del stack[0]
def display():
if len(stack)==0:
print("stack is empty:")
else:
print("elemnet of stack one:")
for ele in stack:
print(ele)
stack=list()
while(1):
print("enter 1 for push,2 for pop,3 for display,4 for exit")
str=input()
if str=="1":
print("push")
push()
elif str=="2":
pop()
elif str=="3":
display()
elif str=='4':
exit()
else:
print("invalid input")
break
Output
Output
{[ ]{()}} - Unbalanced
[{}{})( ] - Unbalanced
((())) - Unbalanced
Output
enter the number:
5
fibonnaci series:01123
#Tower of Hanoi
Code
def tower_of_hanoi(disks,source,auxiliary,target):
if (disks==1):
print("move disk 1 from rod{}to rod {}.".format(source,target))
return
tower_of_hanoi(disks-1,source,target,auxiliary)
print("move disk{}from rod {}to rod {}.".format(disks,source, target))
disks=int(input("enter the number of disks:"))
tower_of_hanoi(disks,'A','B','C')
Output
enter the number of disks:5
move disk 1 from rodAto rod C.
move disk2from rod Ato rod B.
move disk3from rod Ato rod C.
move disk4from rod Ato rod B.
move disk5from rod Ato rod C.
#Implement queue
Code
def enqueue():
n=int(input("enter the number"))
queue.append(n)
print()
def dequeue():
if len(queue)==0:
print("queue is empty")
print()
else:
print(queue[0],"is the element to be deleted")
del queue[0]
print()
def display():
if len (queue)==0:
print("queue is empty")
else:
print("elements of the queue are:")
for ele in queue:
print(ele,end="")
print("front of the queue is:",queue[0])
print()
queue=list()
while(1):
print("1 to queue,2 to dequeue,3 to display,other to exit")
str=input()
if str=='1':
print("enqueue")
enqueue()
elif str=='2':
print("dequeue")
dequeue()
elif str=='3':
print("display")
display()
else:
print("exit")
break
Output
1 to queue,2 to dequeue,3 to display,other to exit
1
enqueue:
enter the number:13
Output
pq: 12101198
12
while queue:
i = queue.popleft()
print(i, end=' ')
for x in graph[i]:
if x not in d:
d.add(x)
queue.append(x)
print("BFS Traversal:")
bfs(graph, 2)
Output
BFS Traversal:
2031
Code
def dfs(h, start, d=None):
if d is None:
d = set()
d.add(start)
print(start, end=' ')
for x in h[start]:
if x not in d:
dfs(h, x, d)
h={
'A': ['B', 'C'],
'B': ['A', 'D', 'E'],
'C': ['A', 'F'],
'D': ['B'],
'E': ['B', 'F'],
'F': ['C', 'E']
}
print("DFS Traversal:")
dfs(h, 'A')
Output
DFS Traversal:
ABDEFC