Chapter 13: Data Structures and Applications in c++

Program Source Code 13.1, page no: 415

In [1]:
(SORTEDASCENDING,SORTEDDESCENDING,UNSORTED) = (0,1,2)    #enum declared

INSERT_AT_END = -1
INSERT_SORT_ASCENDING = -2
INSERT_SORT_DESCENDING = -3

class NODE:               #class declared
    
    def __init__(self,d=None):         #constructor
        self.__data = d
        self.__next = None
        
    def __del__(self):                #destructor
        self.purge()
        
    def search(self,d):
        
        if self.__data == d:
            return self
        
        elif self.__next == None:
            return None
        
        else:
            return self.__next.search(d)
        
    def remove(self,d):             #remove function
        
        n = NODE()       #NODE *n
        
        if self.__data == d:
            return self.__next
        
        else:
            
            if not self.__next == None:
                n = self.__next.remove(d)
                
                if not n==self.__next:
                    del self.__next
            return self
    
    def insertAtEnd(self,n):                #insert function
        
        if self.__next == None:
            self.__next = n
            
        else:
            self.__next.insertAtEnd(n)
            
        return False
    
    
    def insert(self,n,pos):                    #insert function
        
        if pos == INSERT_AT_END:
            self.insertAtEnd(n)
            
        elif pos == INSERT_SORT_ASCENDING:
            
            if (((pos == INSERT_SORT_ASCENDING)and(n.__data<self.__data)) or ((pos == INSERT_SORT_DESCENDING)and(n.__data>self.__data))):
                n.__next = self
                return True
                
            else:
                
                if self.__next == None:
                    self.__next = n
                    
                else:
                    
                    if self.__next.insert(n,pos) == True:
                        self.__next = n
                        
                        
        elif pos == INSERT_SORT_DESCENDING:
            
            if (((pos == INSERT_SORT_ASCENDING)and(n.__data<self.__data)) or ((pos == INSERT_SORT_DESCENDING)and(n.__data>self.__data))):
                n.__next = self
                return True
                
            else:
                
                if self.__next == None:
                    self.__next = n
                    
                else:
                    
                    if self.__next.insert(n,pos) == True:
                        self.__next = n
                        
        else:
            
            if pos == 0:
                n.__next = self
                return True
            
            else:
                
                if self.__next == None:
                    self.__next = n
                    
                else:
                    if self.__next.insert(n,pos-1) == True:
                        self.__next = n
                        
        return False
    
    def Print(self):               #print function
        print self.__data,'->',
        
        if self.__next == None:
            print 'NULL'
        else:
            self.__next.Print()
            
    def purge(self):                #purge function
        
        if not self.__next == None:
            del self.__next
            self.__next = None
            
    
class LINKEDLIST:                   #class declared
    
    def __init__(self,Ord = UNSORTED):                  #constructor
        
        self.__header = NODE()
        self.__order = Ord
        
    def __del__(self):                  #destructor
        self.purge()
        
        
    def search(self,d):                        #search 
        
        if (not self.__header == None) and (not self.__header.search(d) == None):
            return True
        return False
    
    def remove(self,d):             #remove
        
        n = NODE()
        
        if not self.__header == None:
            n = self.__header.remove(d)
            
            if not n==self.__header:
                del self.__header
                self.__header = n
    
    
    def insert(self,d,pos = UNSORTED):
        
        if self.__order == SORTEDASCENDING:      #considering all cases and position
            pos = INSERT_SORT_ASCENDING
            
        elif self.__order == SORTEDDESCENDING:
            pos = INSERT_SORT_DESCENDING
            
        n=[None]*d
            
        n = NODE(d)
        
        if self.__header == None or self.__header.insert(n,pos) == True:
            self.__header = n
            
    
    def Print(self):                              #printing the list
        print '---------------------------------'
        
        if self.__header._NODE__next == None:
            print 'Empty Linked List.....'
            
        else:
            self.__header.Print()
            
        print '----------------------------------'
        
        
    def purge(self):
        
        if not self.__header == None:
            del self.__header
            self.__header = None
            
 #variable declaration
            
print 'Empty LinkedListCreated'

aList = LINKEDLIST()
bList = LINKEDLIST(SORTEDASCENDING)
cList = LINKEDLIST(SORTEDDESCENDING)

aList.Print()

print 'Inserting 3, 5, 2 to be inserted at end'

aList.insert(3, INSERT_AT_END)
aList.insert(5, INSERT_AT_END)
aList.insert(2, INSERT_AT_END)

aList.Print()

print 'Inserting 3, 5, 2 to be inserted in sort ascending order'

bList.insert(3)
bList.insert(5)
bList.insert(2)

bList.Print()


print 'Inserting 3, 5, 2 to be inserted in sort descending order'

cList.insert(3)
cList.insert(5)
cList.insert(2)

cList.Print()


print 'Inserting 3, 5, 2 to be inserted always (like stack) at the beginning'
aList.purge()
aList.insert(3,0)
aList.insert(5,0)
aList.insert(2,0)
aList.Print()


print 'Inserting 3, 5, 2 to be inserted at position 0, 1, 1 respectively'
aList.purge()
aList.insert(3,0)
aList.insert(5,1)
aList.insert(2,1)
aList.Print()
Empty LinkedListCreated
---------------------------------
Empty Linked List.....
----------------------------------
Inserting 3, 5, 2 to be inserted at end
---------------------------------
None -> 3 -> 5 -> 2 -> NULL
----------------------------------
Inserting 3, 5, 2 to be inserted in sort ascending order
---------------------------------
None -> 2 -> 3 -> 5 -> NULL
----------------------------------
Inserting 3, 5, 2 to be inserted in sort descending order
---------------------------------
5 -> 3 -> 2 -> None -> NULL
----------------------------------
Inserting 3, 5, 2 to be inserted always (like stack) at the beginning
---------------------------------
2 -> 5 -> 3 -> NULL
----------------------------------
Inserting 3, 5, 2 to be inserted at position 0, 1, 1 respectively
---------------------------------
3 -> 2 -> 5 -> NULL
----------------------------------

Program Source Code 13.2, page no: 423

In [2]:
(FALSE, TRUE)= (0,1)          #enum declaration

class Employee:         #class employee declared
    def __init__(self, nm=None, sal=0):          #constructor
        self.__name=None
        if(nm!=None):
            self.__name=[]
            
            self.__name.append(nm)
            nm=self.__name
        self.__salary=sal
            
    def __del__(self):                     #destructor
        if(self.__name!=None):
           del self.__name
        
    def display(self):                  #display function
        if(self.__name==None):
            print 'No Name available for employee'
            print ''
        else:
            print 'Name of the employee: ',self.__name
            
            
class Manager(Employee):              #derived class declared
    def __init__(self, nm, sal):                      #constructor
         Employee.__init__(self,nm,sal)
            
    def __del__(self):             #destructor
        pass
    def display(self):
        Employee.display(self)
        print 'Employee type: Manager'
        
class Programmer(Employee):              #derived class declared
    def __init__(self, nm, sal,lang):              #constructor
        Employee.__init__(self,nm,sal)
        self.__language=None
        if lang!=None:
            self.__language=[len(lang)+1]
            self.__language=lang
            
    def __del__(self):                 #destructor
        pass
            
    def display(self):             #display
        Employee.display(self)
        print 'Employee type: programmer'
        if self.__language==None:
            print 'Language known: None specific'
        else:
            print 'language known: ',self.__language
        
        
class Secretary(Employee):                  #derived class declared
    def __init__(self, nm, sal,Isteno,typespeed):  #constructor
        Employee.__init__(self,nm,sal)
        self.__steno=Isteno
        self.__Typingspeed=typespeed
        
    def __del__(self):                 #destructor
        pass
        
    def display(self):                #displaying the characteristic of secretary
        Employee.display(self)
        print 'Employee type: Secretary'
        if self.__steno== False:
            print 'Cannot perform job of a steno'
        else:
            print 'can perform job of a steno'
        print 'typing speed is: ',self.__Typingspeed
        
        
        
 #objects declared and initialised 
        
e=[Employee() for j in range(5)]
e[0]=Manager('A. Pal',1000)
e[1]=Programmer('D.Ghosh',6000,'C++')
e[2]=Programmer('R.Das',5000,'Java')
e[3]=Secretary('S.Ray',3000,False,40)
e[4]=Secretary('A.Chatterjee',2500,True,50)
for i in range(5):
    e[i].display()
del e[0]
Name of the employee:  ['A. Pal']
Employee type: Manager
Name of the employee:  ['D.Ghosh']
Employee type: programmer
language known:  C++
Name of the employee:  ['R.Das']
Employee type: programmer
language known:  Java
Name of the employee:  ['S.Ray']
Employee type: Secretary
Cannot perform job of a steno
typing speed is:  40
Name of the employee:  ['A.Chatterjee']
Employee type: Secretary
can perform job of a steno
typing speed is:  50
In [ ]: