Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class book:
- def __init__(self, bookId, bookName, author):
- self.bookId = bookId
- self.bookName = bookName
- self.author = author
- ##################################
- def getBookInfo(self):
- print("The bookId is : " + str(self.bookId))
- print("The name of the book is " + self.bookName)
- print("The author is " + self.author)
- ######################################
- class Node:
- def __init__(self, data, nextNode=None):
- self.data = data
- self.nextNode = nextNode
- def getData(self):
- return self.data
- def setData(self, val):
- self.data = val
- def getNextNode(self):
- return self.nextNode
- def setNextNode(self, val):
- self.nextNode = val
- def getInfo(self):
- info = self.data
- print(info.getBookInfo())
- class bookCollection:
- def __init__(self, head=None):
- self.head = head
- self.size = 0
- def getSize(self):
- return self.size
- def addBookToFront(self, data):
- newNode = Node(data,self.head)
- self.head = newNode
- self.size += 1
- return True
- ##############################
- def printAll(self):
- curr = self.head
- while curr:
- nodeInfo = curr.getData()
- curr = curr.getNextNode()
- bookInfo = nodeInfo.getData()
- print(bookInfo.getBookInfo())
- ######################################
- def removeBookAtPosition(self, position):
- counter = 1
- if position == 0:
- self.head = getNextNode()
- else:
- book = self.head
- toBeRemoved = book.getNextNode()
- while book.getNextNode() is not None:
- if counter == position - 1:
- book.setNextNode(toBeRemoved.getNextNode())
- book = book.getNextNode()
- toBeRemoved = toBeRemoved.getNextNode()
- counter = counter + 1
- def insertNode(node, position):
- counter = 1
- if position == 0:
- newNode.setNextNode(self.head)
- self.head = newNode
- else:
- node = self.head
- while node.getNextNode() is not None:
- if counter == position:
- newNode.setNextNode(node.getNextNode())
- node.setNextNode(newNode)
- node = node.getNextNode()
- counter = counter + 1
- boy = book(1, "boy", "roald dahl")
- mathstextbook = book(2,"Maths Textbook", "Mathematician")
- harrypotter = book(3, "Harry Potter", "J.K Rowling")
- codingInPython = book(4, "Coding In Python for Dummies", "Aahz Maruch")
- nodeBoy = Node(boy)
- nodeMathsTextbook = Node(mathstextbook)
- nodeHarryPotter = Node(harrypotter)
- nodeCodingInPython = Node(codingInPython)
- bookshelf = bookCollection()
- bookshelf.addBookToFront(nodeBoy)
- nodeBoy.setNextNode(nodeMathsTextbook)
- nodeMathsTextbook.setNextNode(nodeHarryPotter)
- nodeHarryPotter.setNextNode(nodeCodingInPython)
- bookshelf.printAll()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement