Lab07 DSA
Lab07 DSA
Lab 07
1 Objective
The purpose of this lab session is to implement Linked List data structure and its applications.
2 Instructions
You have to perform the following tasks yourselves. Raise your hand if you face any difficulty in
understanding and solving these tasks. Plagiarism is an abhorrent practice and you should not
engage in it.
3 How to Submit
• Submit lab work in a single .py file on Google Classroom. (No other format will be accepted)
• Lab work file name should be saved with your roll number (e.g. 19a-001-SE_LW04.py)
• Submit home work in a single .py file on Google Classroom. (No other format will be accepted)
• Lab work file name should be saved with your roll number (e.g. 19a-001-SE_HW04.py)
class SparseMatrix :
# C r e a t e s a s p a r s e m a t r i x of size n u m R o w s x n u m C o l s i n i t i a l i z e d to 0.
def _ _ i n i t _ _ ( self , numRows , n u m C o l s ) :
self . _ n u m C o l s = n u m C o l s
self . _ l i s t O f R o w s = A r r a y ( n u m R o w s )
# R e t u r n s the n u m b e r of c o l u m n s in the m a t r i x .
def n u m C o l s ( self ) :
r e t u r n self . _ n u m C o l s
# R e t u r n s the v a l u e of e l e m e n t ( i , j ) : x [ i , j ]
def _ _ g e t i t e m _ _ ( self , n d x T u p l e ) :
row = n d x T u p l e [0]
col = n d x T u p l e [1]
Computer Science, Usman Institute of Technology
p r e d N o d e = None
c u r N o d e = self . _ l i s t O f R o w s [ row ]
w h i l e c u r N o d e is not None and c u r N o d e . col != col :
predNode = curNode
c u r N o d e = c u r N o d e . next
if c u r N o d e is not None and c u r N o d e . col == col :
return curNode . value
# S c a l e s the m a t r i x by the g i v e n s c a l a r .
def s c a l e B y ( self , s c a l a r ) :
for row in r a n g e ( self . n u m R o w s () ) :
c u r N o d e = self . _ l i s t O f R o w s [ row ]
w h i l e c u r N o d e is not None :
c u r N o d e . v a l u e *= s c a l a r
c u r N o d e = c u r N o d e . next
# M a t r i x a d d i t i o n : n e w M a t r i x = self + r h s M a t r i x .
def _ _ a d d _ _ ( self , r h s M a t r i x ) :
# Make sure the two m a t r i c e s have the c o r r e c t size .
a s s e r t r h s M a t r i x . n u m R o w s () == self . n u m R o w s () and \
r h s M a t r i x . n u m C o l s () == self . n u m C o l s () , \
" M a t r i x s i z e s not c o m p a t a b l e for a d d i n g . "
n e w M a t r i x [ row , c u r N o d e . col ] = v a l u e
c u r N o d e = c u r N o d e . next
# R e t u r n the new m a t r i x .
return newMatrix
# - - - M a t r i x s u b t r a c t i o n and m u l t i p l i c a t i o n - - -
def _ _ s u b _ _ ( self , r h s M a t r i x ) :
...
# def _ _ m u l _ _ ( self , r h s M a t r i x ) :
...
# S t o r a g e c l a s s for c r e a t i n g m a t r i x e l e m e n t n o d e s .
class _MatrixElementNode :
def _ _ i n i t _ _ ( self , col , v a l u e ) :
self . col = col
self . v a l u e = v a l u e
self . next = None
def d e g r e e ( self ) :
if self . _ p o l y H e a d is None :
r e t u r n -1
else :
r e t u r n self . _ p o l y H e a d . d e g r e e
def _ _ g e t i t e m _ _ ( self , d e g r e e ) :
a s s e r t self . d e g r e e () >= 0 , \
" O p e r a t i o n not p e r m i t t e d in e m p t y p o l y n o m i a l . "
c u r N o d e = self . _ p o l y H e a d
w h i l e c u r N o e is not None and c u r N o d e . d e g r e e >= d e g r e e :
c u r N o d e = c u r N o d e . next
if c u r N o d e is None or c u r N o d e . d e g r e e != d e g r e e :
r e t u r n 0.0
else :
return curNode . degree
def e v a l u a t e ( self , s c a l a r ) :
pass
def _ _ a d d _ _ ( self , r h s P o l y ) :
a s s e r t self . d e g r e e () >= 0 and r h s P o l y . d e g r e e () >= 0 , \
" A d d i t i o n only a l l o w e d in non - e m p t y P o l y n o m i a l s "
n e w P o l y = P o l y n o m i a l ()
n o d e A = self . _ p o l y H e a d
n o d e B = self . _ p o l y H e a d
else :
degree = nodeA . degree
value = nodeA . coefficient + nodeB . coefficient
# adds when d e g r e e is same
n o d e A = n o d e A . next
n o d e B = n o d e B . next
n e w P o l y . _ a p p e n d T e r m ( degree , v a l u e )
w h i l e n o d e A is not None :
n e w P o l y . _ a p p e n d T e r m ( n o d e A . degree , n o d e A . c o e f f i c i e n t )
n o d e A = n o d e A . next
# if the list i t s e l f is l o n g e r
w h i l e n o d e B is not None :
n e w P o l y . _ a p p e n d T e r m ( n o d e B . degree , n o d e B . c o e f f i c i e n t )
n o d e B = n o d e B . next
return newPoly
def _ _ s u b _ _ ( self , r h s P o l y ) :
pass
def _ _ m u l _ _ ( self , r h s P o l y ) :
pass
# H e l p e r m e t h o d for a p p e n d i n g t e r m s in the p o l y n o m i a l
def _ a p p e n d T e r m ( self , degree , c o e f f i c i e n t ) :
if c o e f f i c i e n t != 0.0:
n e w T e r m = _ P o l y T e r m N o d e ( degree , c o e f f i c i e n t )
if self . _ p o l y H e a d is None :
self . _ p o l y H e a d = n e w T e r m
else :
self . _ p o l y T a i l . next = n e w T e r m
self . _ p o l y T a i l = n e w T e r m