【问题标题】:Help with Circular Linked List in Python帮助 Python 中的循环链表
【发布时间】:2011-07-24 00:28:23
【问题描述】:

我正在尝试制作一个循环单链表。我希望能够修改我的代码以获得一个单独喜欢的列表,但我遇到了一些麻烦。

对于我的链接列表,我有:

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


class LinkedList(object):
  def __init__(self):
    self.first = None

  def __str__(self):
    a = "["
    current = self.first
    while current != None:
      a += str(current.data) + ', ' 
      current = current.next
    a = a[:-2] + ']'  
    return a  

  def __iter__(self):
    current = self.first
    a = []
    while current != None:
      a += [current.data]
      current = current.next
    return iter(a)

  def __len__ (self):
    current = self.first
    a = []
    while current != None:
      a += [current.data]
      current = current.next
    return len(a)

  def InsertFirst(self, item):
    NewLink = Link(item, self.first)
    self.first = NewLink

  def InsertLast(self, item):
    NewLink = Link(item)
    current = self.first

    if current == None:
      self.first = NewLink  
      return 

    while current.next != None:
      current = current.next
    current.next = NewLink 

  def Search(self, item):
    count = 0
    current = self.first
    while current != None:
      count += 1
      if current.data == item:
        return count
      else:
        pass
        current = current.next
    return -1

  def Delete(self, item):
    current = self.first
    previous = self.first

    if (current == None):
      return None

    while (current.data != item):
      if (current.next == None):
        return None
      else:
        previous = current
        current = current.next

    if (current == self.first):
      self.first = self.first.next
    else:
      previous.next = current.next

    return current

到目前为止,我的循环列表有:

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


class CircularList(object):
  def __init__(self):
    self.first = Link(None, None)
    self.head = Link(None, self.first)

  def __str__(self):
    a = "["
    current = self.first
    while current != None:
      a += str(current.data) + ', ' 
      current = current.next
    a = a[:-2] + ']'  
    return a  

  def InsertLast(self, item):
    NewLink = Link(item)
    current = self.first

    if current == None:
      self.first = NewLink  
      return 

    while current.next != None:
      current = current.next
    current.next = Link(item)

我的问题是如何将最后一个元素链接回第一个元素以便我可以横向?

【问题讨论】:

  • 我没有仔细看过它,但它会像 last.next = first。

标签: python linked-list circular-list


【解决方案1】:

last.next = 创建时的第一个?

class Link (object):
  def __init__ (self, data, next = None):
    self.data = data
    self.next = self.first

可能不是有效代码。但既然你在创建时保证在列表的最后一部分,那么你也可以。

【讨论】:

  • 领先我 5 秒;该死的你! :)
【解决方案2】:

循环链表的要点是跳过所有“if next is not None”的逻辑。一开始,head 指向自己,表示列表为空。没有必要创建一个空的“第一个”——在一开始就做:

self.head = Link(None, None)
self.head.next = self.head

然后要在其他节点之后插入一个节点,您只需:

def insert_after(insert_node, after_node):
    insert_node.next = after_node.next
    after_node.next = insert_node

要在列表的开头插入,请执行以下操作:

insert_after(node, head)

Insert before 需要反复查找“before”节点,因为列表只是单链接的:

def insert_before(node, before_node):
    loc = head
    while loc.next is not before_node:
        loc = loc.next
    insert_after(insert_node, loc)

要在列表末尾插入,请执行以下操作:

insert_before(node, head)

要获取列表的所有元素,请执行以下操作:

current = self.head.next
while current is not self.head:
    # do something with current.data

    # advance to next element
    current = current.next

但循环列表的真正威力在于使其具有双重链接,因此您可以在不迭代的情况下插入之前。

【讨论】:

  • while current != self.head 可能比while current is not self.head 更好,不是吗?
  • 谢谢,多么及时!我将移除我的 cmets :D
【解决方案3】:
class cirlist(list):
    def __init__(self,*arg):
        super(cirlist,self).__init__(*arg)
        self.m=super(cirlist,self).__getitem__(0)
        self.Index=0
    def next(self):
        if self.Index>=super(cirlist,self).__len__()-1:
            self.m=super(cirlist,self).__getitem__(0)
        else:
            self.m=super(cirlist,self).__getitem__(self.Index+1)
        if self.Index>super(cirlist,self).__len__()-1:    
            self.Index=super(cirlist,self).index(self.m)+1
        else:
            self.Index=super(cirlist,self).index(self.m)
        return self.m

【讨论】:

  • next 属性每次都会给出列表中的下一个元素。
【解决方案4】:
class Link (object):

def __init__(self, first=None, rest=None):
    self.first = first
    self.rest = rest
def get_data(self):
    return self.first
def get_next(self):
    return self.rest
def __getitem__(self, i):
    if i ==  0:
        return self.first
    get = self
    while i > 0:
        get = get.rest
        i -= 1
    if get == None:
        raise IndexError('The Sentence Index is Out of Range.')
    return get.first

类 Circular_Link(对象):

def __init__(self, things):
    assert len(things) > 2
    last = Link(things[len(things)-1])
    first = Link(things[len(things)-2], last)
    index = len(things)-2
    while index > 0:
        index -= 1
        first = Link(things[index], first)
    last.rest = first
    self.circle = first
def __getitem__(self, i):
    return self.circle[i]

此示例从普通列表初始化循环列表。

【讨论】:

    【解决方案5】:

    Node 类将创建一个空节点,insert 函数将通过调用 Node 类创建一个节点。然后我们检查头节点,如果为空,则创建新节点作为头节点。否则搜索节点指针为 Null 并插入新节点。

    class clinkedlist:
      def __init__(self):
        self.head = None
    
      def insertat(self,data):
        new_node = Node(data)
    
        if self.head is None:
            self.head = new_node
        else:
            new_node.next = self.head
            self.head = new_node
      def traverse(self):
        printval = self.head
        print(printval.data)
        
        while (True):
            printval = printval.next
            print(printval.data)
            if printval == self.head:
                break
    
    
        print(printval)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-31
      • 2021-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-21
      相关资源
      最近更新 更多