I am currently a student just starting to learn python and I need help improving my code for a school assignment. I've already written the code for the program, just need suggestions on how to improve it or make it better.
Uncle Joe started the Greenville Secondhand Bookstore in 2000. As years passed, the number of books in his store increased significantly. He can no longer keep track of their location in his store. Knowing you are from School of informatics & IT, he has approached you to develop a program (in Python) that could help him to keep track of the books.
Your program should fulfil the following requirement:
a.
AddBookToFront(newBook)
: This method will create a newNode
with the newBook
object as its data value and then add the newly created node to the front of the linked list.b.
AddBookAtPosition(newBook, n)
: This method will create a newNode
with the new Book object as its data value and then add the newly created node at position n of the linked list. Assume that the first node of the linked list has a position number of 0 and the second node has a position number of 1 and so on.c.
RemoveBookAtPosition(n)
: This method will remove the node at position n in the linked list. Assume that the first node of the linked list has a position number of 0 and the second node has a position number of 1 and so on.d.
DisplayBook()
: This method will traverse the linked list from its first node to its last node and print the data value (i.e., theid
,bookName
andauthorName
of theBook
object) of each node.e.
SortByAuthorName()
: This method will sort the linked list by the book author’s name in ascending order.Notes:
You are allowed to make changes to the
LinkedList
andNode
classes as you deemed fit.You are not allowed to use List for this question.
You are not allowed to use any existing Python libraries to do sorting.
Your solution should use objects and classes effectively.
So here is my code, I'm not too sure if I did it correctly or not so please do tell me if there are any mistakes I made or what I should do that would be better.
class Book:
def __init__(self,id,bookName,authorName,nextNode=None):
self.id = id
self.bookName = bookName
self.authorName = authorName
self.nextNode = nextNode
def getId(self):
return self.id
def getBookName(self):
return self.bookName
def getAuthorName(self):
return self.authorName
def getNextNode(self):
return self.nextNode
def setNextNode(self,val):
self.nextNode = val
class LinkedList:
def __init__(self,head = None):
self.head = head
self.size = 0
def getSize(self):
return self.size
def AddBookToFront(self,newBook):
newBook.setNextNode(self.head)
self.head = newBook
self.size+=1
def DisplayBook(self):
curr = self.head
while curr:
print(curr.getId(),curr.getBookName(),curr.getAuthorName())
curr = curr.getNextNode()
def RemoveBookAtPosition(self,n):
prev = None
curr = self.head
curPos = 0
while curr:
if curPos == n:
if prev:
prev.setNextNode(curr.getNextNode())
else:
self.head = curr.getNextNode()
self.size = self.size - 1
return True
prev = curr
curr = curr.getNextNode()
curPos = curPos + 1
return False
def AddBookAtPosition(self,newBook,n):
curPos = 1
if n == 0:
newBook.setNextNode(self.head)
self.head = newBook
self.size+=1
return
else:
currentNode = self.head
while currentNode.getNextNode() is not None:
if curPos == n:
newBook.setNextNode(currentNode.getNextNode())
currentNode.setNextNode(newBook)
self.size+=1
return
currentNode = currentNode.getNextNode()
curPos = curPos + 1
if curPos == n:
newBook.setNextNode(None)
currentNode.setNextNode(newBook)
self.size+=1
else:
print("cannot add",newBook.getId(),newBook.getBookName(),"at that position")
def SortByAuthorName(self):
for i in range(1,self.size):
node1 = self.head
node2 = node1.getNextNode()
while node2 is not None:
if node1.authorName > node2.authorName:
temp = node1.id
temp2 = node1.bookName
temp3 = node1.authorName
node1.id = node2.id
node1.bookName = node2.bookName
node1.authorName = node2.authorName
node2.id = temp
node2.bookName = temp2
node2.authorName = temp3
node1 = node1.getNextNode()
node2 = node2.getNextNode()
myLinkedList = LinkedList()
nodeA = Book("#1","cool","Isaac")
nodeB = Book("#2","amazing","Alfred")
nodeC = Book("#3","hello","John")
nodeD = Book("#4","why","Chase")
nodeE = Book("#5","good","Mary")
nodeF = Book("#6","hahaha","Radin")
myLinkedList.AddBookToFront(nodeA)
myLinkedList.AddBookToFront(nodeB)
myLinkedList.AddBookToFront(nodeC)
myLinkedList.AddBookAtPosition(nodeD,1)
myLinkedList.AddBookAtPosition(nodeE,1)
myLinkedList.AddBookAtPosition(nodeF,1)
myLinkedList.RemoveBookAtPosition(2)
myLinkedList.RemoveBookAtPosition(2)
myLinkedList.DisplayBook()
myLinkedList.SortByAuthorName()
print(myLinkedList.getSize())
myLinkedList.DisplayBook()
AddBookToFront
that are so not PEP8 (addBookToFront
is infuriatingly java, but no one should capitalize method names, this isn't golang). \$\endgroup\$