Python:Linklist deque addfront& addrear

时间:2013-10-08 12:52:10

标签: python

我是python的初学者,现在我在这个程序中遇到了问题。

首先是NodeList:< -linklist

class Node:

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

    def getData(self):
        return self.data

    def getNext(self):
        return self.next

    def setData(self,newdata):
        self.data = newdata

    def setNext(self,newnext):
        self.next = newnext

这是我的计划:

from NodeList import Node
class DequeLL:

    def __init__(self):
        self.head = None
        self.last = None
        self.length = 0

    def addFrontDLL(self, item):
        temp = Node(str(item))
        temp.setNext(self.head)
        self.head = temp
        length = max(len(node.data) for node in self.allNodes()) if self.head else 0
        print('\u2510{}\u250c'.format(' '*length))
        for node in self.allNodes():
            print('\u2502{:<{}}\u2502'.format(node.data, length))
        print('\u2514{}\u2518'.format('\u2500'*length))

    def addRearDLL(self, item):
        node = Node(str(item))
        node.next = None
        if self.length == 0:
            self.head = self.last = node
        else:
            last = self.last
            last.next = node
            self.last = node
        self.length = self.length + 1 
        length = max(len(node.data) for node in self.allNodes()) if self.head else 0
        print('\u2510{}\u250c'.format(' '*length))
        for node in self.allNodes():
            print('\u2502{:<{}}\u2502'.format(node.data, length))
        print('\u2514{}\u2518'.format('\u2500'*length))

    def allNodes(self):
        node = self.head
        while node is not None:
            yield node
            node = node.next

d = DequeLL()

当我在addFrontDLL中添加项目时,一切都很好。但是在addRearDLL中添加一个项目之后,就像该项目被添加到另一个容器中一样。然后我尝试再次使用addFrontDLL添加它的工作方式我想要它(该项目被添加到另一个容器)。见下图:

enter image description here

我希望有人为我解释并修复它。任何帮助将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:1)

问题是,首次在前端添加DequeLL.last的节点时,您不会设置addFrontDLL字段。您的addRearDLL可以正确处理添加新节点,这就是为什么在后面添加后一切正常的原因。

试试这个:

class DequeLL:

    def __init__(self):
        self.head = None
        self.last = None
        self.length = 0

    def __str__(self): # Refactored for clarity
        width = (max(len(node.data) for node in self.allNodes()) 
                    if self.head else 0) # width to avoid ambiguity with self.length
        s = ['\u2510{}\u250c'.format(' '*width)]
        for node in self.allNodes():
            s.append('\u2502{:<{}}\u2502'.format(node.data, width))
        s.append('\u2514{}\u2518'.format('\u2500'*width))
        return '\n'.join(s)

    def addFrontDLL(self, item):
        temp = Node(str(item))
        temp.setNext(self.head)
        self.head = temp
        if self.last is None: # if adding to an empty deque, set the last node, too
            self.last = temp
        self.length += 1
        print(self) # refactored the pretty-printing into __str__ for clarity

    def addRearDLL(self, item):
        node = Node(str(item))
        node.next = None
        if self.length == 0:
            self.head = self.last = node
        else:
            last = self.last
            last.next = node
            self.last = node
        self.length = self.length + 1 
        print(self)

    def allNodes(self):
        node = self.head
        while node is not None:
            yield node
            node = node.next

您的示例的输出现在是:

In [97]: d = DequeLL()

In [98]: d.addFrontDLL('circle')
┐      ┌
│circle│
└──────┘

In [99]: d.addFrontDLL('arc')
┐      ┌
│arc   │
│circle│
└──────┘

In [100]: d.addRearDLL('123')
┐      ┌
│arc   │
│circle│
│123   │
└──────┘

In [101]: d.addFrontDLL('arc')
┐      ┌
│arc   │
│arc   │
│circle│
│123   │
└──────┘

In [102]: d.addFrontDLL('circle')
┐      ┌
│circle│
│arc   │
│arc   │
│circle│
│123   │
└──────┘

In [103]: d.addRearDLL('123')
┐      ┌
│circle│
│arc   │
│arc   │
│circle│
│123   │
│123   │
└──────┘