Python Forum
How to create a linked list and call it?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to create a linked list and call it?
#7
I used itertools zip to zip the lists together. To allow for uneven sized lists I used the zip_longest version. Because the LList naturally builds backwards I need to reverse the list when done so it is in the correct order (least significant digit is first).

For printing I added a __repr__ method to LList. I also added some convenience functions that are commonly provided by collection types.
import itertools

class LList:
    def __init__(self, value=0, nxt=None):
        self.value = value
        self.nxt = nxt

    def values(self):
        """Generator for getting all the values"""
        ptr = self
        while ptr:
            yield ptr.value
            ptr = ptr.nxt

    def reverse(self):
        """Make new LList with order of values reversed"""
        result = None
        while self:
            result = LList(self.value, result)
            self = self.nxt
        return result

    def __len__(self):
        """Return number of values in LList"""
        count = 0
        ptr = self
        while ptr is not None:
            count += 1
            ptr = ptr.nxt
        return count

    def __getitem__(self, index):
        """Get value by index: LList[index]"""
        ptr = self
        for _ in range(index):
            ptr = ptr.nxt
        return ptr.value

    def __setitem__(self, index, value):
        """Set value by index: LList[index] = value"""
        ptr = self
        for _ in range(index):
            ptr = ptr.nxt
        ptr.value = value

    def __repr__(self):
        """Return a pretty string representation of LList"""
        return f'[{",".join([str(value) for value in self.values()])}]'

def LLNumber(value):
    """Make a LList from a number"""
    if value > 10:
        return LList(value % 10, LLNumber(value // 10))
    return LList(value)

def LLAdd(a, b):
    """Add to LList numbers"""
    carry = 0
    result = None
    for digits in itertools.zip_longest(a.values(), b.values(), fillvalue=0):
        value = sum(digits)+carry
        result = LList(value % 10, result)
        carry = value // 10
    if carry > 0:
        result = LList(carry, result)
    return result.reverse()
        
a = LLNumber(4321)
b = LLNumber(8765)
result = LLAdd(a, b)
print(a, '+', b, '=', result)
Output:
[1,2,3,4] + [5,6,7,8] = [6,8,0,3,1]
Reply


Messages In This Thread
How to create a linked list and call it? - by loves - Nov-20-2020, 09:42 PM
RE: How to create a linked list and call it? - by deanhystad - Nov-21-2020, 04:41 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  for loops break when I call the list I'm looping through Radical 4 1,057 Sep-18-2023, 07:52 AM
Last Post: buran
  How do I call sys.argv list inside a function, from the CLI? billykid999 3 943 May-02-2023, 08:40 AM
Last Post: Gribouillis
  Delete strings from a list to create a new only number list Dvdscot 8 1,831 May-01-2023, 09:06 PM
Last Post: deanhystad
  [split] why can't i create a list of numbers (ints) with random.randrange() astral_travel 7 1,721 Oct-23-2022, 11:13 PM
Last Post: Pedroski55
  how to easily create a list of already existing item CompleteNewb 15 4,008 Jan-06-2022, 12:48 AM
Last Post: CompleteNewb
  About linked lists ManoEl 2 1,711 Oct-17-2021, 03:21 PM
Last Post: ManoEl
  Create SQLite columns from a list or tuple? snakes 6 9,186 May-04-2021, 12:06 PM
Last Post: snakes
  Create variable and list dynamically quest_ 12 4,790 Jan-26-2021, 07:14 PM
Last Post: quest_
  list call problem in generator function using iteration and recursive calls postta 1 2,038 Oct-24-2020, 09:33 PM
Last Post: bowlofred
  How to create and define in one line a 2D list of class objects in Python T2ioTD 1 2,176 Aug-14-2020, 12:37 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020