Data Structure (1)
Data Structure (1)
ractical no.01
A) Code:-
list1 = [10,20,30,40,50]
print("printing element in python by using different
ways") print("Print list by print()method",list1)
print(i,end="")
print()
print("print list by*
asterisk") print(*list1)
separator") print(list1,sep=",")
Output:-
1020304050
asterisk 10 20 30 40
50
print list by comma
list1 = [10,20,30,40,50]
print(list1[0])
print(list1[1:4]
)
print(list1[1:3]
) print(list1[-
1])
print(list1[3:-1])
print(list1[0:2:2])
print(list1[0:5])
Output:-
10
[20, 30, 40]
[20, 30]
50
[40]
[10]
[10, 20, 30, 40, 50]
C) Code:-
le = [10,20,30,40,50,60,70,80,90,100,110,120,130,140,150,160,170,180,190,200]
print(i,end=" ")
print("\nprinting the list element *
asterisk:") print(*le)
print("printing the list element by comma separator:")
print(le,sep=",")
Output:-
10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 170 180 190 200
printing the list element * asterisk:
10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 170 180 190 200
printing the list element by comma separator:
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200]
D) Code:-
le = [10,20,30,40,50]
print(le)
print(le[1::])
print(le[1:3])
print(le[4:])
print(le[3::])
print(le[::2])
print(le[::4])
print("printing in
reverse") print((le[::-1]))
print(le[-1:-5:-1])
print(le[-3:-5:-1])
print(le[4::])
print(le[::-1])
print(le[::2])
print(le[::4])
Output:-
[20, 30]
[50]
[40, 50]
[10, 30, 50]
[10, 50]
printing in reverse
[50, 40, 30, 20, 10]
[50, 40, 30, 20]
[30, 20]
[50]
[50, 40, 30, 20, 10]
[10, 50]
A) Code:-
import numpy as np
from numpy import*
a=np.array([["Mon",1,2,3,4],["Tue",5,6,7,8],["Wed",9,10,11,12],["Thu",13,14,15,16],["Fri",17,18
,19,20],["Sat",21,22,23,24]])
m = reshape(a,(6,5))
#printing array
print(m,end="")
Output:-
B) Code:-
import numpy as np
a=np.array([["Mon",1,2,3,4],["Tue",5,6,7,8],["Wed",9,10,11,12],["Thu",13,14,15,16],["Fri",17,18
,19,20],["Sat",21,22,23,24]])
m = reshape(a,(6,5))
#printing array
print(m[2])
Output:-
C) Code:-
import numpy as np
a=np.array([["Mon",1,2,3,4],["Tue",5,6,7,8],["Wed",9,10,11,12],["Thu",13,14,15,16],["Fri",17,18
,19,20],["Sat",21,22,23,24]])
m = reshape(a,(6,5))
#printing array
print(m,end="")
r = append(m,["Sun",25,26,27,28])
print(r)
Output:-
['Sat' '21' '22' '23' '24']]['Mon' '1' '2' '3' '4' 'Tue' '5' '6' '7' '8' 'Wed' '9' '10' '11' '12'
'Thu' '13' '14' '15' '16' 'Fri' '17' '18' '19' '20' 'Sat' '21' '22' '23'
D) Code:-
import numpy as np
from numpy import*
a=np.array([["Mon",1,2,3,4],["Tue",5,6,7,8],["Wed",9,10,11,12],["Thu",13,14,15,16],["Fri",17,18
,19,20],["Sat",21,22,23,24]])
m = reshape(a,(6,5))
d = np.delete(m,[3],0)
print(d,end="")
Output:-
E) Code:-
import numpy as np
from numpy import*
a=np.array([["Mon",1,2,3,4],["Tue",5,6,7,8],["Wed",9,10,11,12],["Thu",13,14,15,16],["Fri",17,18
,19,20],["Sat",21,22,23,24]])
m = reshape(a,(6,5))
print(m)
#Printing array
r = append(m,["Sun",25,26,27,28])
print(r)
d = np.delete(r,
[3],0) print(d)
print(a[2])
Output:-
'Thu' '13' '14' '15' '16' 'Fri' '17' '18' '19' '20' 'Sat' '21' '22' '23'
'13' '14' '15' '16' 'Fri' '17' '18' '19' '20' 'Sat' '21' '22' '23' '24'
'Sun' '25' '26' '27' '28']
F) Code:-
import numpy as np
from numpy import*
a=np.array([["Mon",1,2,3,4],["Tue",5,6,7,8],["Wed",9,10,11,12],["Thu",13,14,15,16],["Fri",17,18
,19,20],["Sat",21,22,23,24]])
print(a)
a[3] = ["N/a",0,0,0,0]
print(a)
Output:-
'24']]
P
ractical No.02
Code:-
class Node:
def init (self, data, next=None):
self.dataVal = data
self.nextVal = next
class SingleLinkList:
printval = self.headVal
while printval is not None:
print(printval.dataVal, end=",
") printval = printval.nextVal
def insertAtBeginning(self,
newdata): NewNode =
Node(newdata) NewNode.nextVal =
self.headVal self.headVal =
NewNode
def insertAtEnd(self,
newdata): NewNode =
Node(newdata)
if self.headVal is None:
self.headVal =
NewNode return
laste = self.headVal
while
laste.nextVal:
laste.nextVal = NewNode
NewNode = Node(newdata)
NewNode.nextVal = middle_node.nextVal
middle_node.nextVal = NewNode
HeadVal =
self.headVal if
HeadVal is not None:
if HeadVal.dataVal == Removekey:
self.headVal = HeadVal.nextVal
HeadVal
= None
return
prev = HeadVal
HeadVal = HeadVal.nextVal
if HeadVal is None:
return
prev.nextVal =
HeadVal.nextVal HeadVal =
None
def removeStart(self):
if self.headVal is not None:
self.headVal = self.headVal.nextVal
def
removeEnd(self):
last =
self.headVal
if last is None:
print("List is empty")
return
if last.nextVal is None:
self.headVal =
None return None
while last.nextVal.nextVal:
last = last.nextVal
if last.nextVal.nextVal is None:
last.nextVal = None
e1 = Node("Mon")
e2 = Node("Tue")
e3 = Node("Wed")
e4 = Node("Thu")
e5 = Node("Fri")
sli.headVal = e1
e1.nextVal = e2
e2.nextVal = e3
e3.nextVal = e4
e4.nextVal = e5
print("*** Printing Link List
***") sli.listprint()
sli.listprint()
print("\n*** Printing Link List After Inserting At End
***") sli.insertAtEnd("Sat")
sli.listprint()
sli.listprint()
***")
sli.removeNode("n/a")
sli.listprint()
sli.removeStart()
sli.listprint()
***")
sli.removeEnd()
sli.listprint()
Output:-
Code:-
#node structure
class Node:
# constructor to create a new
self.data = data
self.next = None
self.prev = None
# class Linked
List class
LinkedList:
# constructor to create an empty
self.head =
None # test the
code
# create an empty
LinkedList MyList =
LinkedList()
# Add first node.
first = Node(10)
# linking with head node
MyList.head = first
# Add second node.
second = Node(20)
second.prev = first
first.next =
node. third =
Node(30)
print("Linked List:")
print_linked_list(MyList
Output:-
A) Code:-
stack=[]
stack.append(300)
print(stack)
stack.pop() #the pop method is used to remove the
element print(stack)
print("no.of element in
stack:-",len(stack)) stack.pop()
stack.pop()
Output:-
[10, 20]
no.of element in stack:- 2
2. Using modules
A) Code:-
import collections
stack=collections.deque()
stack.append(10)
stack.append(3.14)
stack.append("value of p1")
stack.append("value of
radius") stack.append(10)
stack.append(10)
print(stack)
Output:-
count 10 in deque:- 3
B) Code:-
import queue
stack=queue.LifoQueue()
stack.put(10)
stack.put(200)
stack.put("SYBSCCS
Class")
print("check stack is empty or
not:-",stack.empty()) print("check stack is full or
not:-",stack.full()) print("no.of the item in
lifoqueue:-",stack.qsize())
print("element:-",stack.get())
print("element:-",stack.get())
print("element:-",stack.get())
element:- 200
element:- 10
A) Code:-
is:-",len(q)) q.pop(0)
q.pop(0)
print(q)
Output:-
[30]
B) Code:-
print(q)
Output:-
[30]
2. Using Modules
A) Code:-
import collections
q.appendleft(20)
q.appendleft(30)
print(q)
print("No of elements in deque is :-",len(q)) #printing length of stack
print("No.of elements in deque is:-",q.count(10)) #To count the
deque q.pop()
q.pop()
print(q)
Output:-
No of elements in deque is :- 3
No.of elements in deque is:- 1
deque([30])
B) Code:-
import collections
q=collections.deque() #right to left
q.appendleft(10)
q.appendleft(20)
q.appendleft(30)
print(q)
print("No of elements in deque is :-",len(q)) #printing length of stack
print("No.of elements in deque is:-",q.count(10)) #To count the
deque q.popleft()
q.popleft()
print(q)
Output:-
No of elements in deque is :- 3
No.of elements in deque is:- 1
deque([10])
P
ractical No.06
Q.6 write a program to implement Priority queue with insertion, deletion, traversal operations.
A) Code:-
#Increasing
order q=[]
q.append(150)
q.append(300)
q.append(500)
q.append(130)
print("Original Queue:",q)
q.sort()
print("After Sorting:",q)
Output:-
B) Code:-
#Decreasing
order q=[]
q.append(150)
q.append(300)
q.append(500)
q.append(130)
print("Original Queue:",q)
q.sort(reverse=True)
print("After Sorting:",q)
#Removing the highest priority
element. print("Removed
Element:",q.pop(0)) print("Queue after
pop:",q)
Output:-
C) Code:-
#Decreasing
order q=[]
q.append(150)
q.append(300)
q.append(500)
q.append(130)
print("Original Queue:",q)
q.sort(reverse=True)
print("After Sorting:",q)
#Removing the highest priority
element. print("Removed
Element:",q.pop(0))
print("Queue after pop:",q)
q.append(350)
q.append(380)
q.append(800)
q.append(750)
print("Queue Before
Sorting:",q)
q.sort(reverse=True)
print("After Sorting:",q)
Output:-
750] After Sorting: [800, 750, 380, 350, 300, 150, 130]
2. Using module
A) Code:-
import queue
q=queue.PriorityQueue()
q.put(150)
q.put(460)
q.put(20)
q.put(45)
q.put(10)
q.put(34)
q.put(100)
print("Elements in queue:",)
for n in list(q.queue):
print(n,end=" ")
#Removing elements from queue
print("/nRemoved Element:",q.get())
print("Removed Element:",q.get())
print("Removed Element:",q.get())
print("Removed Element:",q.get())
Output:-
Elements in queue:
Removed Element: 20
Removed Element: 34
Removed Element: 45
B) Code:-
# node structure
class Node:
self.head = newNode
temp = self.head
if(temp != None):
temp = temp.next
print()
else:
print("The list is empty.")
MyList =
LinkedList()
#Add three elements at the start of the list.
MyList.push_front(10
MyList.push_front(20
MyList.push_front(30
) MyList.PrintList()
Output:-
Code:-
class Node:
def
traversePreOrder(self):
print(self.val,end=' ')
if self.left:
self.left.traversePreOrder()
if self.right:
self.right.traversePreOrder(
) #traverse inorder
def
traverseInOrder(self): if
self.left:
self.left.traverseInOrder(
) print(self.val,end=' ')
if self.right:
self.right.traverseInOrder(
) #Traverse PostOrder
def
traversePostOrder(self): if
self.left:
self.left.traversePostOrder(
)
if self.right:
self.right.traversePostOrder()
print(self.val,end=' ')
root=Node(1)
root.left=Node(2)
root.right=Node(3)
root.left.left=Node(4)
root.left.right=Node(5)
print("pre order
Traversal:",end="")
root.traversePreOrder()
print("\nIn order
Traversal:",end="")
root.traverseInOrder()
print("\nPost order
Traversal:",end="")
root.traversePostOrder()
Output:-
In order Traversal:4 2 5 1 3
Post order Traversal:4 5 2 3 1
P
ractical No.08
if gdict is None:
gdict = {}
self.gdict =
gdict
def getVertices(self):
return list(self.gdict.keys())
def getEdges(self):
return
list(self.gdict.values())
graph_element = {
"a": ["b", "c"],
g = Graph(graph_element)
print("Representation of graph using
dictionary") print(graph_element)
print("\nKeys:")
print(g.getVertices())
print("\nEdges:")
print(g.getEdges())
Output:-
{'a': ['b', 'c'], 'b': ['a', 'd'], 'c': ['a', 'd'], 'd': ['b', 'c', 'e'], 'e': ['d']}
Keys:
Edges:
[['b', 'c'], ['a', 'd'], ['a', 'd'], ['b', 'c', 'e'], ['d']]
P
ractical No.09
string = 'BCAADDDCCACACAC'
class
NodeTree(object):
self.left = left
self.right = right
def children(self):
def nodes(self):
(l, r) = node.children()
d = dict()
# Calculating frequency
freq = {}
for c in string:
if c in freq:
freq[c] += 1
else:
freq[c] = 1
nodes = freq
2] nodes = nodes[:-2]
huffmanCode = huffman_code_tree(nodes[0][0])
print(' ')
Output:-
'C' | 0
'A' | 11
'D' | 101
'B' | 100
P
ractical No.10
Q.10 Write a program to create basic Hash Table for insertion, deletion, traversal
operations (assume that there are no collisions)
Code:-
class HashTable1:
def init (self, size):
self.size = size
index =
self._hash_function(key)
index = self._hash_function(key)
if self.table[index] and self.table[index][0] ==
index = self._hash_function(key)
if self.table[index] and self.table[index][0] ==
def traverse(self):
# Example usage:
ht = HashTable1(10)
ht.insert("apple", 5)
ht.insert("banana", 10)
ht.insert("cherry", 15)
print("HashTable:"
) ht.traverse()
ht.delete("banana") print("\
nAfter deleting 'banana':")
ht.traverse()
search_result =
ht.get("cherry") if
HashTable:
10