Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Book:
- def __init__(self, author="", title="", nextBook=None):
- self.author = author.capitalize()
- self.title = title.capitalize()
- self.nextBook = nextBook
- # def getAuthor(self):
- # return self.author
- # def setAuthor(self, author):
- # self.author = author
- # def getTitle(self):
- # return self.title
- # def setTitle(self, title):
- # self.title = title
- # def getNextBook(self):
- # return self.nextBook
- # def setNextBook(self, book):
- # self.nextBook = book
- def displayBook(self):
- print("Author:", self.author + ", Title:", self.title)
- def displayAllBooks(self):
- self.displayBook()
- if self.nextBook != None:
- self.nextBook.displayAllBooks()
- # def deleteBookByAuthor(self, author):
- # if author == self.author:
- # self.nextBook = self.nextBook.nextBook
- # else:
- # self.nextBook.deleteBookByAuthor(author)
- def deleteBookAtPosition(self, position):
- if position:
- self.nextBook.deleteBookAtPosition(position - 1)
- else:
- self.nextBook = self.nextBook.nextBook
- def insertBook(self, book, position):
- if position:
- self.nextBook.insertBook(book, position - 1)
- else:
- book.nextBook = self.nextBook
- self.nextBook = book
- def SortByAuthorName(self, lastBook=None):
- bubbleSort(self, lastBook)
- # if self.nextBook == None:
- # return 0
- # if self.author > self.nextBook.author:
- # x = self
- # y = x.nextBook
- # z = y.nextBook
- # self.nextBook.nextBook = x
- # self.nextBook = z
- # # if lastBook != None:
- # # lastBook.nextBook = y
- # lastBook.nextBook = y
- # lastBook = y
- # else:
- # lastBook = self
- # lastBook.nextBook.SortByAuthorName(lastBook)
- class LinkedList:
- def __init__(self, head=None):
- self.head = head
- if head == None:
- self.size = 0
- else:
- self.size = 1
- def getSize(self):
- return self.size
- def addBookToFront(self, newBook):
- newBook.nextBook = self.head
- self.head = newBook
- self.size += 1
- def addBookAtPosition(self, newBook, n):
- self.head.insertBook(newBook, n - 1)
- self.size += 1
- def removeBookAtPosition(self, n):
- self.head.deleteBookAtPosition(n - 1)
- self.size -= 1
- def displayBook(self):
- self.head.displayAllBooks()
- def sortByAuthorName(self):
- for index in range(self.size - 1):
- self.head = bubbleSort(self.head, self.head)
- # if self.head.nextBook == None:
- # return 0
- # if self.head.author > self.head.nextBook.author:
- # x = self.head
- # y = x.nextBook
- # z = y.nextBook
- # self.head.nextBook.nextBook = x
- # self.head.nextBook = z
- # self.head = y
- # self.head.nextBook.SortByAuthorName(self.head)
- def bubbleSort(current, last):
- if current.nextBook == None:
- return current
- if current.author > current.nextBook.author:
- c = current # the current book
- nxt = c.nextBook # 2nd book from the current
- nnxt = nxt.nextBook # 3rd book from the current
- # c, nxt, nnxt are temporary varibles?
- current.nextBook.nextBook = c
- current.nextBook = nnxt
- if last != current:
- last.nextBook = nxt
- last = nxt
- else:
- last = current
- last.nextBook.SortByAuthorName(last)
- return last
- # it will return back the value
- a = Book("Eins", "A")
- b = Book("Zwei", "B")
- c = Book("Drei", "C")
- d = Book("Vier", "D")
- bookList = LinkedList(d)
- bookList.displayBook()
- print()
- bookList.addBookToFront(b)
- bookList.displayBook()
- print()
- bookList.addBookToFront(a)
- bookList.displayBook()
- print()
- bookList.addBookAtPosition(c, 2)
- bookList.displayBook()
- print()
- bookList.removeBookAtPosition(1)
- bookList.displayBook()
- print()
- bookList.sortByAuthorName()
- bookList.displayBook()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement