简体   繁体   中英

How to have a linked list within a linked list in Python 3.x

So my objective is to have two classes, a package class and a container class. The package class is going to have (owner, destination, weight) as parameters. Using linked list, I want to create new containers (max weight 2000lbs) based on the destination and weight of the package.

> For eg: if package class has the function add(owner, destination,
> weight)
>         package.add("Jason", "Antarctica", 56)
>         package.add("Julio", "Dubai", 84)
>         package.add("Mason", "Antarctica", 84)

The above packages should create two containers with destination Antarctica and Dubai with weights as 140 for Antarctica (56+84) and 84 for Dubai.This is what I have so far.

# Linked List Container
class Container:
    value = (self.own, self.dest, self.weight)

    # Queue class Initialization        
    def __init__(self):
        self._rear = None
        self._front = None
        #self._first = None
        self._size = 0

    # Defining the node
    class _Node:
        def __init__(self, elem, next):
            self._elem = elem
            self._next = next


    # Adding the first item into the queue through the rear    
    def enqueue(self, value):
        if self.isEmpty():
            self._front = self._Node(value, None)
            self._rear = self._front
            self._size += 1
            return
        self._rear._next = self._Node(value, None)
        self._rear = self._rear._next
        self._size += 1
        return

    class Package:
        def add(self, owner, destination, weight):
            self.own = owner
            self.dest = destination
            self.weight = weight

Any insights and help really would be appreciated. I just cant figure out how I can link two linked lists together.

I would structure it differently. A custom linked list or node probably isn't necessary since Python native lists offer the same features. You can iterate through them and add/remove items at any location within the list.

I would create packages and add them to a containers object that organizes them based on destination. Something like:

class Package:
    def __init__(self, owner, destination, weight):
        self.own = owner
        self.dest = destination
        self.weight = weight

class Container:
    def __init__(self, destination):
        self.packages = []
        self.destination = destination
        self.weight = 0

    def add(self, package):
        self.packages.append(package)
        self.weight = self.weight + package.weight

class Containers:
    def __init__(self):
        self.containers = []

    def enqueue(self, package):
        for self.containers as c:
            if c.destination = package.destination:
                c.add(package)
                return;
        # New destination
        c = Container(package.destination)
        c.add(package)
        self.containers.append(c)

Then to match your example usage:

containers = Containers()
containers.add("Jason", "Antarctica", 56)
containers.add("Julio", "Dubai", 84)
containers.add("Mason", "Antarctica", 84)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM