为什么“追加”方法无法正常工作?

时间:2019-02-13 16:16:23

标签: python data-structures linked-list

“ append”方法无法正常工作。 它只进入“ append”方法的“ if”语句中,而没有进入while循环。

class Node:
    def __init__(self,data=None):
        self.data=data
        self.next=None

class Linkedlist:
    def __init__(self):
        self.head=Node()

    def append(self,data):
        new_node=Node(data)
        if self.head.data is None:
            self.head=new_node          
        cur_node=self.head
        while cur_node.next is not None:
            cur_node=cur_node.next
        cur_node=new_node

    def insert_after_node(self,prev_node,data):
        new_node=Node(data)
        if prev_node is None:
            print("node that you have entered does not exist")
        new_node.next=prev_node.next
        prev_node.next=new_node

    def display(self):
        current=self.head
        while current.next is not None:
            print(current.data)
            current=current.next

List=Linkedlist()
List.append("A")
List.append("B")
List.append("C")
List.insert_after_node(List.head,"g")
List.display()

预期输出:AgBC
实际输出:A

3 个答案:

答案 0 :(得分:1)

您的.append()方法只需将局部变量设置为cur_node即可指向new_node。这根本不会改变链表;链接中先前分配给该局部变量的最后一个节点不会更改。

您想分配给最后一个节点的.next属性:

cur_node.next = new_node

该方法中的while循环可以正常工作。

当列表为空时,您也应该两次使用new_node。当尚无带有数据的头节点时退出:

if self.head.data is None:
    self.head=new_node
    return

我个人将设置self.head = None而不是self.head = Node(),然后使用if self.head is None:

接下来,您的显示功能会忘记打印最后一个元素。而不是测试current.next,而是检查current是否为None;在这里,将self.head设置为None以获得空列表会更好:

def display(self):
    current = self.head
    while current is not None
        print(current.data)
        current = current.next

答案 1 :(得分:1)

我和您有完全相同的问题,但是我的实现方式有所不同,而且似乎工作正常。

首先,我创建了一个具有所有不同方法的节点类:

class Node:
    def __init__(self, init_data):
        self.data = init_data
        self.next = None

    def get_data(self):
        return self.data

    def get_next(self):
        return self.next

    def set_data(self, new_data):
        self.data = new_data

    def set_next(self, new_next):
        self.next= new_next

我也创建了UnorderedList类及其方法,包括添加方法。插入,索引和弹出,我仍在努力...

class UnorderedList:
    """
    An unordered list class built from a collection of nodes.
    """
    
    def __init__(self):
        self.head = None

    def is_empty(self):
        return self.head == None
    
    def add(self, item):
        temp = Node(item)
        temp.set_next(self.head)
        self.head = temp

    def size(self):
        current = self.head
        count = 0
        while current != None:
            count += 1
            current = current.get_next()

        return count

    def search(self, item):
        current = self.head
        found = False
        while current != None and not found:
            if current.get_data() == item:
                found = True
            else:
                current = current.get_next()

        return found


    def remove(self, item):
        current = self.head
        previous = None
        found = False
        while not found:
            if current.get_data() == item:
                found = True
            else:
                previous = current
                current = current.get_next()

        if previous == None:
            self.head = current.get_next()
        else:
            previous.set_next(current.get_next())

    def print_list(self):
        current = self.head
        while current != None:
            print(current.data)
            current = current.get_next()

    def append(self, item):
        new_node = Node(item)

        if self.head == None:
            self.head = new_node
            return 
            
        current = self.head
        found_last = False
        while not found_last:
            if current.get_next() == None:
                found_last = True
                current.set_next(new_node)
                
            else:
                current = current.get_next()


    def insert(self, item, pos):
        pass


    def index(self, item):
        pass


    def pop(self):
        pass

我意识到我的append版本更加冗长,但是我使用了以前作为size方法一部分使用的遍历方法来使其正常工作,

我发现使用其方法分别创建Node类更为容易,因为它使可视化如何设置下一个节点并从当前节点获取数据变得更加容易。希望这会有所帮助!

仅供参考,David Ranum和Brad Miller的 Python中的算法和数据结构问题解决方法直接说明了节点类和许多UnorderedList,所以我知道它们可以正常工作!

答案 2 :(得分:0)

my.list
$d1
  y1 y2
1  1  4
2  2  5
3  3  6

$d2
  y1 y2
1  3  6
2  2  5
3  1  4

为您修复了一些错误:

  1. 类名class Node: def __init__(self,data=None): self.data=data self.next=None class LinkedList(): def __init__(self): self.head=Node() def append(self,data): new_node=Node(data) if self.head.data is None: self.head=new_node else: cur_node=self.head while cur_node.next is not None: cur_node=cur_node.next cur_node.next=new_node def insert_after_node(self,prev_node,data): new_node=Node(data) if prev_node is None: print("node that you have entered does not exist") new_node.next=prev_node.next prev_node.next=new_node def display(self): current=self.head while current.next is not None: print(current.data) current=current.next print(current.data) List=LinkedList() List.append("A") List.append("B") List.append("C") List.insert_after_node(List.head,"g") 不匹配问题

  2. LinkedListappend中,应设置if self.head.data is None然后返回。

  3. self.head的{​​{1}}部分中,让最后一个节点通过else指向新节点

  4. append中,也应打印最后一个节点。