Advertisement
Guest User

Untitled

a guest
Jan 21st, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.29 KB | None | 0 0
  1. class Book:
  2. def __init__(self, author="", title="", nextBook=None):
  3. self.author = author.capitalize()
  4. self.title = title.capitalize()
  5. self.nextBook = nextBook
  6.  
  7. # def getAuthor(self):
  8. # return self.author
  9.  
  10. # def setAuthor(self, author):
  11. # self.author = author
  12.  
  13. # def getTitle(self):
  14. # return self.title
  15.  
  16. # def setTitle(self, title):
  17. # self.title = title
  18.  
  19. # def getNextBook(self):
  20. # return self.nextBook
  21.  
  22. # def setNextBook(self, book):
  23. # self.nextBook = book
  24.  
  25. def displayBook(self):
  26. print("Author:", self.author + ", Title:", self.title)
  27.  
  28. def displayAllBooks(self):
  29. self.displayBook()
  30. if self.nextBook != None:
  31. self.nextBook.displayAllBooks()
  32.  
  33. # def deleteBookByAuthor(self, author):
  34. # if author == self.author:
  35. # self.nextBook = self.nextBook.nextBook
  36. # else:
  37. # self.nextBook.deleteBookByAuthor(author)
  38.  
  39. def deleteBookAtPosition(self, position):
  40. if position:
  41. self.nextBook.deleteBookAtPosition(position - 1)
  42. else:
  43. self.nextBook = self.nextBook.nextBook
  44.  
  45. def insertBook(self, book, position):
  46. if position:
  47. self.nextBook.insertBook(book, position - 1)
  48. else:
  49. book.nextBook = self.nextBook
  50. self.nextBook = book
  51.  
  52. def SortByAuthorName(self, lastBook=None):
  53. bubbleSort(self, lastBook)
  54. # if self.nextBook == None:
  55. # return 0
  56.  
  57. # if self.author > self.nextBook.author:
  58. # x = self
  59. # y = x.nextBook
  60. # z = y.nextBook
  61. # self.nextBook.nextBook = x
  62. # self.nextBook = z
  63. # # if lastBook != None:
  64. # # lastBook.nextBook = y
  65. # lastBook.nextBook = y
  66. # lastBook = y
  67. # else:
  68. # lastBook = self
  69.  
  70. # lastBook.nextBook.SortByAuthorName(lastBook)
  71.  
  72.  
  73. class LinkedList:
  74. def __init__(self, head=None):
  75. self.head = head
  76. if head == None:
  77. self.size = 0
  78. else:
  79. self.size = 1
  80.  
  81. def getSize(self):
  82. return self.size
  83.  
  84. def addBookToFront(self, newBook):
  85. newBook.nextBook = self.head
  86. self.head = newBook
  87. self.size += 1
  88.  
  89. def addBookAtPosition(self, newBook, n):
  90. self.head.insertBook(newBook, n - 1)
  91. self.size += 1
  92.  
  93. def removeBookAtPosition(self, n):
  94. self.head.deleteBookAtPosition(n - 1)
  95. self.size -= 1
  96.  
  97. def displayBook(self):
  98. self.head.displayAllBooks()
  99.  
  100. def sortByAuthorName(self):
  101. for index in range(self.size - 1):
  102. self.head = bubbleSort(self.head, self.head)
  103. # if self.head.nextBook == None:
  104. # return 0
  105.  
  106. # if self.head.author > self.head.nextBook.author:
  107. # x = self.head
  108. # y = x.nextBook
  109. # z = y.nextBook
  110.  
  111. # self.head.nextBook.nextBook = x
  112. # self.head.nextBook = z
  113. # self.head = y
  114.  
  115. # self.head.nextBook.SortByAuthorName(self.head)
  116.  
  117.  
  118. def bubbleSort(current, last):
  119. if current.nextBook == None:
  120. return current
  121.  
  122. if current.author > current.nextBook.author:
  123. c = current # the current book
  124. nxt = c.nextBook # 2nd book from the current
  125. nnxt = nxt.nextBook # 3rd book from the current
  126. # c, nxt, nnxt are temporary varibles?
  127. current.nextBook.nextBook = c
  128. current.nextBook = nnxt
  129. if last != current:
  130. last.nextBook = nxt
  131. last = nxt
  132. else:
  133. last = current
  134.  
  135. last.nextBook.SortByAuthorName(last)
  136.  
  137. return last
  138. # it will return back the value
  139.  
  140.  
  141. a = Book("Eins", "A")
  142. b = Book("Zwei", "B")
  143. c = Book("Drei", "C")
  144. d = Book("Vier", "D")
  145.  
  146.  
  147. bookList = LinkedList(d)
  148. bookList.displayBook()
  149. print()
  150. bookList.addBookToFront(b)
  151. bookList.displayBook()
  152. print()
  153. bookList.addBookToFront(a)
  154. bookList.displayBook()
  155. print()
  156. bookList.addBookAtPosition(c, 2)
  157. bookList.displayBook()
  158. print()
  159. bookList.removeBookAtPosition(1)
  160. bookList.displayBook()
  161. print()
  162. bookList.sortByAuthorName()
  163. bookList.displayBook()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement