Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.42 KB | None | 0 0
  1.  
  2.  
  3. class Node:
  4.  
  5.     def __init__(self):
  6.         self.previous = None
  7.         self.next = None
  8.         self.name = None
  9.         self.points = None
  10.  
  11.     def __init__(self, name, points):
  12.         self.name = name
  13.         self.points = points
  14.  
  15.     def print_data(self):
  16.         print('Name: ' + self.name)
  17.         print('Points: ' + str(self.points))
  18.         print()
  19.  
  20.  
  21. class BidirectionalList:
  22.  
  23.     def __init__(self):
  24.         self.head = None
  25.         self.size = 0
  26.  
  27.     def add_node_at_end(self, name, points):
  28.         if self.head is None:
  29.             self.head = Node(name, points)
  30.             self.head.next = self.head
  31.             self.head.previous = self.head
  32.         else:
  33.             temp = self.head
  34.             while temp.next != self.head:
  35.                 temp = temp.next
  36.             temp.next = Node(name, points)
  37.             temp.next.next = self.head
  38.             temp.next.previous = temp
  39.             self.head.previous = temp.next
  40.         self.size += 1
  41.  
  42.     def add_node_at_begin(self, name, points):
  43.         if self.head is None:
  44.             self.head = Node(name, points)
  45.             self.head.next = self.head
  46.             self.head.previous = self.head
  47.         else:
  48.             temp = Node(name, points)
  49.             self.head.previous.next = temp
  50.             temp.next = self.head
  51.             temp.previous = self.head.previous
  52.             temp.next.previous = temp
  53.             self.head = temp
  54.         self.size += 1
  55.  
  56.     def add_node_at_position(self, name, points, position):
  57.         try:
  58.             if position > self.size or position < 0:
  59.                 raise ValueError('Position have to be greater than 0 and lesser than list size!')
  60.             else:
  61.                 temp = self.head
  62.                 for i in range(position):
  63.                     temp = temp.next
  64.                 temp2 = Node(name, points)
  65.                 temp.previous.next = temp2
  66.                 temp2.next = temp
  67.                 temp.previous = temp2
  68.                 self.size += 1
  69.  
  70.         except ValueError as ve:
  71.             print('Exception: ' + repr(ve))
  72.  
  73.     def delete_node_at_end(self):
  74.         if self.head is None:
  75.             print('Nothing to delete')
  76.         else:
  77.             self.head.previous.previous.next = self.head
  78.             self.head.previous = self.head.previous.previous
  79.             self.size -= 1
  80.  
  81.     def delete_node_at_begin(self):
  82.         if self.head is None:
  83.             print('Nothing to delete')
  84.         else:
  85.             self.head.previous.next = self.head.next
  86.             self.head.next.previous = self.head.previous
  87.             self.head = self.head.next
  88.             self.size -= 1
  89.  
  90.     def print_list_downwards(self):
  91.         temp = self.head
  92.         temp.print_data()
  93.         temp = temp.next
  94.         while temp != self.head:
  95.             temp.print_data()
  96.             temp = temp.next
  97.  
  98.     def print_list_upwards(self):
  99.         temp = self.head.previous
  100.         temp.print_data()
  101.         temp = temp.previous
  102.         while temp != self.head.previous:
  103.             temp.print_data()
  104.             temp = temp.previous
  105.  
  106.     def find_max(self):
  107.         if self.head is None:
  108.             print('List empty!')
  109.         else:
  110.             temp = self.head
  111.             max_node = temp
  112.             for i in range(self.size - 1):
  113.                 temp = temp.next
  114.                 if temp.points > max_node.points:
  115.                     max_node = temp
  116.             print('Node with max points value:')
  117.             max_node.print_data()
  118.  
  119.  
  120. def main():
  121.     blist = BidirectionalList()
  122.     blist.add_node_at_end('A', 19)
  123.     blist.add_node_at_end('B', 23)
  124.     blist.add_node_at_end('C', 29)
  125.     blist.add_node_at_begin('D', 31)
  126.     blist.add_node_at_begin('E', 37)
  127.     blist.print_list_upwards()
  128.     print('deleting begin...')
  129.     blist.delete_node_at_begin()
  130.     blist.print_list_downwards()
  131.     print('deleting end...')
  132.     blist.delete_node_at_end()
  133.     blist.print_list_downwards()
  134.     print('adding at the end...')
  135.     blist.add_node_at_end('new_end', 44)
  136.     blist.print_list_downwards()
  137.     print('adding at the begin...')
  138.     blist.add_node_at_begin('new_begin', 55)
  139.     blist.print_list_downwards()
  140.     # print('... and upwards...')
  141.     # blist.print_list_upwards()
  142.     print('... adding node at position...')
  143.     blist.add_node_at_position('new_position', 77, 3)
  144.     blist.print_list_downwards()
  145.     print('finding max...')
  146.     blist.find_max()
  147.  
  148.  
  149. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement