Advertisement
Guest User

iuto

a guest
Jan 22nd, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. class book:
  2. def __init__(self, bookId, bookName, author):
  3. self.bookId = bookId
  4. self.bookName = bookName
  5. self.author = author
  6. ##################################
  7. def getBookInfo(self):
  8. print("The bookId is : " + str(self.bookId))
  9. print("The name of the book is " + self.bookName)
  10. print("The author is " + self.author)
  11. ######################################
  12. class Node:
  13. def __init__(self, data, nextNode=None):
  14. self.data = data
  15. self.nextNode = nextNode
  16.  
  17. def getData(self):
  18. return self.data
  19.  
  20. def setData(self, val):
  21. self.data = val
  22.  
  23. def getNextNode(self):
  24. return self.nextNode
  25.  
  26. def setNextNode(self, val):
  27. self.nextNode = val
  28.  
  29. def getInfo(self):
  30. info = self.data
  31. print(info.getBookInfo())
  32.  
  33. class bookCollection:
  34. def __init__(self, head=None):
  35. self.head = head
  36. self.size = 0
  37.  
  38. def getSize(self):
  39. return self.size
  40.  
  41. def addBookToFront(self, data):
  42. newNode = Node(data,self.head)
  43. self.head = newNode
  44. self.size += 1
  45. return True
  46. ##############################
  47. def printAll(self):
  48. curr = self.head
  49. while curr:
  50. nodeInfo = curr.getData()
  51. curr = curr.getNextNode()
  52. bookInfo = nodeInfo.getData()
  53. print(bookInfo.getBookInfo())
  54. ######################################
  55. def removeBookAtPosition(self, position):
  56. counter = 1
  57.  
  58. if position == 0:
  59. self.head = getNextNode()
  60. else:
  61. book = self.head
  62. toBeRemoved = book.getNextNode()
  63. while book.getNextNode() is not None:
  64. if counter == position - 1:
  65. book.setNextNode(toBeRemoved.getNextNode())
  66.  
  67. book = book.getNextNode()
  68. toBeRemoved = toBeRemoved.getNextNode()
  69. counter = counter + 1
  70.  
  71. def insertNode(node, position):
  72. counter = 1
  73. if position == 0:
  74. newNode.setNextNode(self.head)
  75. self.head = newNode
  76.  
  77. else:
  78. node = self.head
  79. while node.getNextNode() is not None:
  80. if counter == position:
  81. newNode.setNextNode(node.getNextNode())
  82. node.setNextNode(newNode)
  83. node = node.getNextNode()
  84. counter = counter + 1
  85.  
  86. boy = book(1, "boy", "roald dahl")
  87. mathstextbook = book(2,"Maths Textbook", "Mathematician")
  88. harrypotter = book(3, "Harry Potter", "J.K Rowling")
  89. codingInPython = book(4, "Coding In Python for Dummies", "Aahz Maruch")
  90.  
  91. nodeBoy = Node(boy)
  92. nodeMathsTextbook = Node(mathstextbook)
  93. nodeHarryPotter = Node(harrypotter)
  94. nodeCodingInPython = Node(codingInPython)
  95.  
  96. bookshelf = bookCollection()
  97. bookshelf.addBookToFront(nodeBoy)
  98. nodeBoy.setNextNode(nodeMathsTextbook)
  99. nodeMathsTextbook.setNextNode(nodeHarryPotter)
  100. nodeHarryPotter.setNextNode(nodeCodingInPython)
  101.  
  102. bookshelf.printAll()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement