Help with Circular Linked List in Python -


I am trying to create a circular single link list. I want to be able to modify my code for a list of a single choice but I have some problems.

I have a list for my linked list:

  class link (object): def __init__ (self, data, next = none): self.data = data Self.next = Next category linked object (object): def __init __ (self): self.first = none def __str __ (self): a = "[" current = itself.First is ON! = None: A + = str (current data) + ',' current = current.exe a = a [: - 2] + ']' df __iter __ return (self): current = self.first a = [ ] While on! = None: a + = [current.data] current = current.next return iter (a) def __len__ (self): current = self.first a = [] while present! = None: a + = [current.data] current = current.next (A) DIF entry first (auto, item): new link = link (item, auto itself) first include self.first = NewLink def ( Self, items): new link = link (item) current = self.first if present == none: self.first = NewLink returns while current.next! = None: current = current.next current.next = newlink def search (self, item): count = 0 current = self.first while running! = None: calculation + = 1 if current.data == item: return count: current = current. Delete first pass -1 def (self, item): current = self.first previous = self.first if (current == None): None back (current.data! = Item): if (current.next == none): return no other: last = current current = current if the current (current == self.first): Self.first = self.first.next else: previous.next = current.next return current   

So far I have for my circular list:

 < Code> class link (object): def __init__ (self, data, Next = none): self.data = data self.next = Next class circular list (object): def __init __ (self): self.first = link (none, none) self.head = link (none , Self. First) def __str __ (self): a = "[" current = self.first while running! = None: a + = str (Current.data) + ',' current = current.next a = a [: - 2] + ']' return a def insert (self, items): new link = link ( Item) current = self.first if present == none: self T = new link while current.next! = None: current = current.next current.next = link (item)   

My question is how can I index if I add the last element first?

A circular linked list means "if there is no next" argument in the beginning, head Indicates itself, it indicates that the list is empty There is no need to make empty "first" - very early:

  self.head = link (none, none) Self.head.next = self.head   

Then to insert a node after another node, you just do it:

  def insertafter (Insert_node, after_node): insert_node.next = after_node.next after_node.next = Insert_node   

at the beginning of the list, do the following: insert_after (node, head)

First need to find the "first" node, because the list is only related to singly:

  def insert_before (node, first_net): loc = head while loc.next! = Before_node: To insert at the end of the list, do this:  
  Before inserting (node, head)   

/ code>

To get all the elements of the list:

  current = self.head.next while current! = Self.head: # Do something with the current data # Next to the next element current = current.next   

But the real power in a circular list is linked to it doubling, so that You can insert first without restarting it.

Comments