2

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

对于我的链表,我有:

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)

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

4

5 回答 5

7

循环链表的要点是跳过所有“如果下一个不是无”逻辑。一开始,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)

插入之前需要迭代以找到“之前”节点,因为列表只是单链接的:

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

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

于 2011-03-27T19:22:20.357 回答
2

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

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

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

于 2011-03-27T19:02:18.180 回答
0
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
于 2015-07-20T11:12:06.813 回答
0
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]

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

于 2015-08-22T06:12:02.067 回答
-1

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)
于 2020-09-29T17:59:41.737 回答