Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Linked list within a linked list

$
0
0
im completely stuck on how to make a linked list within a linked list.

i know how to create a linked list:

#A linked list class
class SingleSortedLinkedList :
    #constructor
    def __init__ (self) :
        self._size = 0
        self._head = None
    
    #check for empty list
    def isEmpty(self) :
        return self._size == 0
    
    #overloaded length
    def __len__(self) :
        return self._size
    
    #prints out the data
    def traversal(self) :
        current = self._head
        while current is not None :
            print (current._data)
            current = current._next
            
    #insets (sorted) a new data item into the list
    def insert(self, item) :
        newNode = ListNode(item)
        self._size=self._size+1
        #when our list is new (or reduced to no items)
        if self._head is None :
            self._head = newNode
        #If our new node goes first:
        elif item < self._head._data :
            newNode._next = self._head
            self._head = newNode
        #not at the head, so we will be inserting later into the list    
        else: 
            node = self._head
            #loop to advance pointer until insertion spot located
            while node is not None and node._data < item :
                prev = node
                node = node._next
            if node is None:
                #at end, so newNode already points to None like it should.
                prev._next=newNode
            else:
                #otherwise, update the node
                prev._next=newNode
                newNode._next=node
                
            
            

#class for a single node            
class ListNode :
    def __init__ ( self, data) :
        self._data = data
        self._next = None



i just dont know how to modify it so that each node points to a separate linked list

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>