Jan-19-2018, 12:09 AM
Hi, I am currently trying to code for this particular question which is
Uncle Tim started his 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. He has approached you to develop a program (in Python) that could help him to keep track of the books.
My Current Codes Are :
Thanks
Uncle Tim started his 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. He has approached you to develop a program (in Python) that could help him to keep track of the books.
My Current Codes Are :
class Node: def __init__(self,data): self.data = data self.next = None def getData(self): return self.data def setData(self, newData): self.data = newData def getNext(self): return self.next def setNext(self,newNode): self.next=newNode class Book: def __init__(self, author, title, bookID): self.author = author self.title = title self.bookID = bookID def __str__(self): return 'Author: ' + self.author + ', Title: ' + \ self.title + ', Book ID: ' + str(self.bookID); def setAuthor(self, author): self.author = author def getAuthor(self): return self.author def setTitle(self, title): self.book = book def getTitle(self): return self.title def setBookID(self, bookID): self.bookID = bookID def getBookID(self): return self.bookID class LinkedList: def __init__(self): self.head = None self.size = 0 def AddBookToFront(self, data): print("1") newNode = Node(data) newNode.setNext(self.head) self.head = newNode self.size+=1 def DisplayBook(self): print("3") tempo = self.head while tempo is not None: print ("Author Name: " + tempo.getData().getAuthor() +" " +"Title: " + tempo.getData().getTitle()+ " "+"Book ID: "+tempo.getData().getBookID()) tempo = tempo.getNext() def RemoveBookAtPosition(self,position): # If linked list is empty if self.head == None: return # Store head node temp = self.head # If head needs to be removed if position == 0: head = temp.next temp = None return # Find previous node of the node to be deleted for i in range(position -1 ): temp = temp.next if temp is None: break # If position is more than number of nodes if temp is None: return if temp.next is None: return # Node temp.next is the node to be deleted # store pointer to the next of node to be deleted next = temp.next.next # Unlink the node from linked list temp.next = None temp.next = next def AddBookAtPosition(self, data , position): cursor= self.head temps = Node(data) index = 0 prev = None if (position ==0): temps.setNext(cursor) self.head = temp else: while (index < position): prev = cursor cursor = cursor.getNext() index +=1 prev.getNext() prev.setNext(temps) def bubbleSort(self): cursor= self.head one = cursor.getNext() two = one.getNext() for i in range(0,self.size): for j in range(i): if(one.getData().getAuthor() > two.getData().getAuthor()): temp2 = cursor one = two two =temp2 ll=LinkedList() book1 = Book("AAA","BBB","0001") #n1=Node(book1) book2 = Book("CCC", "BBB", "0002") #n2=Node(book2) book3 = Book("DDD", "CCC", "0003") #n3=Node(book3) book4 = Book("GGG", "FFF", "0004") book5 = Book("EEE","LLL", "0005") ll.AddBookToFront(book1) ll.AddBookToFront(book2) ll.AddBookToFront(book3) #ll.AddBookAtPosition(book4, 2) ll.DisplayBook() print("After Removing") ll.RemoveBookAtPosition(1) ll.DisplayBook() print("After Adding Value At Position 2") ll.AddBookAtPosition(book4,2) ll.DisplayBook() print("After Adding Value At Position 3") ll.AddBookAtPosition(book5,3) ll.DisplayBook() print("After Sorting") ll.bubbleSort() ll.DisplayBook()The Bubble Sort Code Is Here
def bubbleSort(self): cursor= self.head one = cursor.getNext() two = one.getNext() for i in range(0,self.size): for j in range(i): if(one.getData().getAuthor() > two.getData().getAuthor()): temp2 = cursor one = two two =temp2This Is My Output
Output:1
1
1
3
Author Name: DDD Title: CCC Book ID: 0003
Author Name: CCC Title: BBB Book ID: 0002
Author Name: AAA Title: BBB Book ID: 0001
After Removing
3
Author Name: DDD Title: CCC Book ID: 0003
Author Name: AAA Title: BBB Book ID: 0001
After Adding Value At Position 2
3
Author Name: DDD Title: CCC Book ID: 0003
Author Name: AAA Title: BBB Book ID: 0001
Author Name: GGG Title: FFF Book ID: 0004
After Adding Value At Position 3
3
Author Name: DDD Title: CCC Book ID: 0003
Author Name: AAA Title: BBB Book ID: 0001
Author Name: GGG Title: FFF Book ID: 0004
Author Name: EEE Title: LLL Book ID: 0005
After Sorting
3
Author Name: DDD Title: CCC Book ID: 0003
Author Name: AAA Title: BBB Book ID: 0001
Author Name: GGG Title: FFF Book ID: 0004
Author Name: EEE Title: LLL Book ID: 0005
[Done] exited with code=0 in 0.12 seconds
[Running] python "c:\Users\TP_baseline\Desktop\DSAG\ProjectTest9-2.py"
1
1
1
3
Author Name: DDD Title: CCC Book ID: 0003
Author Name: CCC Title: BBB Book ID: 0002
Author Name: AAA Title: BBB Book ID: 0001
After Removing
3
Author Name: DDD Title: CCC Book ID: 0003
Author Name: AAA Title: BBB Book ID: 0001
After Adding Value At Position 2
3
Author Name: DDD Title: CCC Book ID: 0003
Author Name: AAA Title: BBB Book ID: 0001
Author Name: GGG Title: FFF Book ID: 0004
After Adding Value At Position 3
3
Author Name: DDD Title: CCC Book ID: 0003
Author Name: AAA Title: BBB Book ID: 0001
Author Name: GGG Title: FFF Book ID: 0004
Author Name: EEE Title: LLL Book ID: 0005
After Sorting
3
Author Name: DDD Title: CCC Book ID: 0003
Author Name: AAA Title: BBB Book ID: 0001
Author Name: GGG Title: FFF Book ID: 0004
Author Name: EEE Title: LLL Book ID: 0005
Any Guidance Or Advice In Rectifying This Issue Would Be Greatly AppreciatedThanks
