【发布时间】:2018-01-21 12:23:44
【问题描述】:
我正在使用链接列表来存储由 3 个属性 id、bookName 和 authorName 组成的集合作为添加书籍的参数。对象在“Book”类中定义。
代码将像图书馆中的书架一样工作,将id、BookName 和AuthorName 三种类型的信息存储到“图书”类的链表节点中。
我现在正在研究 LinkedList 类中的 3 个不完整的方法。
他们是:
AddBookToPosition (newBook, n) - 将图书添加到所需位置“n”(例如 2) DeleteBookAtPosition (n) - 将 Book 删除到所需位置 'n'(例如 3) SortByAuthorName() - 将存储在链表中的作者姓名按升序排序。
问题:
编译时没有错误,但是 AddBookAtPosition 不起作用并且不显示在输出中添加的书。 DeleteBookAtPosition 也无法正常工作。
我需要帮助(对于 AddBookAtPosition)和删除(DeleteBookAtPosition),它们将分别添加或删除链表第 n 位置的节点,假设程序从位置 0、1、2 开始按顺序显示书籍,依此类推.
感谢您的建议。
class Node :
def __init__(self, data=None, next=None):
self.data = data
self.next = None
class Book :
def __init__(self,n,id,bookName,authorName):
self.n = n
self.id = id
self.bookName = bookName
self.authorName = authorName
def print(self):
print("Position: {}".format(self.n))
print("Book ID: {}".format(self.id))
print("Book Name: {}".format(self.bookName))
print("Author Name: {}".format(self.authorName))
class LinkedList :
def __init__(self):
self.head = None
def __iter__(self):
node = self.head
while node is not None:
yield node
node = node.next
def __len__(self):
return len(list(self))
def __getitem__(self, i):
node = self.head
if node is None:
raise IndexError('LinkedList index out of range')
for n in range(i-1):
node = node.next
if node is None:
raise IndexError('LinkedList index out of range')
return node
#this will add the book to the top of the list
def AddBookToFront (self, newBook):
new_node = Node(newBook)
new_node.next = self.head
self.head = new_node
def AddBookAtPosition (self, newBook, n):
counter = 1
if n == 0:
newBook.setNext(self.head)
self.head = newBook
else:
node = self.head
while node.next is not None :
if counter == n :
newBook.setNext(node.next)
node.setNext(newBook)
return
node = node.next
counter = counter + 1
def DeleteBookAtPosition(self, n):
# If linked list is empty
if self.head == None:
return
# Store head node
temp = self.head
# If head needs to be removed
if n == 0:
self.head = temp.next
temp = None
return
# Find previous node of the node to be deleted
for i in range(n -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 __delitem__(self, i):
if self.head is None:
raise IndexError('LinkedList index out of range')
if i == 0:
self.head = self.head.next
else:
node = self.head
for n in range(i-1):
if node.next is None:
raise IndexError('LinkedList index out of range')
node = node.next
if node.next is None:
raise IndexError('LinkedList index out of range')
node.next = node.next.next
BookList = LinkedList()
BookList.AddBookToFront(Book(1, "J.R.R. Tolkien", "Lord of the Rings"))
BookList.AddBookToFront(Book(2, "Lewis Carroll", "Alice in Wonderland"))
BookList.AddBookAtPosition(Book(3, "Star Wars: Aftermath", "Chuck Wendig"), 3)
for book in BookList:
print(book.data.n)
print(book.data.id)
print(book.data.bookName)
print(book.data.authorName)
电流输出
2
Lewis Carroll
Alice in Wonderland
1
J.R.R. Tolkien
Lord of the Rings
所需输出 - 按位置升序显示 (1,2,3)
Position : 1
Book ID : 3
Book Name: Star Wars: Aftermath
Author Name: Chuck Wendig
Position : 2
Book ID : 1
Book Name: Lord of the Rings
Author Name: J.R.R. Tolkien
【问题讨论】:
-
为什么不直接列出书籍对象呢?然后你可以添加/删除任何你想要的对象。
标签: python linked-list nodes