ML
    • Recent
    • Categories
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    Linked List in Python

    Scheduled Pinned Locked Moved Developer Discussion
    pythonlinked list
    2 Posts 1 Posters 1.4k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • scottalanmillerS
      scottalanmiller
      last edited by scottalanmiller

      Working on building a generic Linked List in Python from the Linked List article at CodeFellows.

      Here it is...

      class Node(object):
          def __init__(self, data):
              self.data = data
              self.nextNode = None
      
      class linkedList(object):
          def __init__(self, head=None):
              self.head = head
      
          def insert(self, node):
              if not self.head:
                  self.head = node
              else:
                  # set new nodes pointer to old thread
                  node.nextNode = self.head
                  # reset head to new node
                  self.head = node
      
          def search(self, lList, Node):
              if self.head == Node:
                  return self.head
              else:
                  if lList.head.nextNode:
                      self.search(linkedList(lList.head.nextNode), Node)
                  else:
                      raise ValueError("Node not in Linked List")
      
          def size(self):
              current = self.head
              size = 0
              while current is not None:
                  size += 1
                  current = current.nextNode
              return size
      
          def delete(self, node):
              if self.size() == 0:
                  raise ValueError("List is empty")
              else:
                  current = self.head
                  previous = None
                  found = False
                  while not found:
                      if current == node:
                          found = True
                      elif current is None:
                          raise ValueError("Node not in Linked List")
                      else:
                          previous = current
                          current = current.nextNode
                  if previous is None:
                      self.head = current.nextNode
                  else:
                      previous.nextNode = current.nextNode
      
      1 Reply Last reply Reply Quote 1
      • scottalanmillerS
        scottalanmiller
        last edited by

        Here is the code for using the above list to make a LinkedList and to insert some test data:

        myList = linkedList()
        for x in range(1,10):
            y = Node(x)
            myList.insert(y)
        
        print(myList.size())
        
        1 Reply Last reply Reply Quote 1
        • 1 / 1
        • First post
          Last post