How to implement linked list in Python??

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • akadri1010
    New Member
    • Aug 2006
    • 1

    How to implement linked list in Python??

    I am new to Python programming..

    Can anyone help me to write a simple Python program that creates a singly linked list and then prints the entries in it??
    I know how to do in C?? But Python does not support pointers, so how to get around with this issue??
  • kudos
    Recognized Expert New Member
    • Jul 2006
    • 127

    #2
    Hi
    actually, python variables do 'points' to things, so in a sense python does support pointers. Here is an example program, that fills a list with numbers 1 to 5. Each
    element in the list looks the following way: (value, pointer_to_next _element). Then it prints the content of the list
    [code=python]
    list = []
    list.append([1,0])
    current_element = list[0]

    # build a linked list 1,2,3,4,5

    for i in range(5):
    list.append([i+2,0])
    current_element[1] = list[i+1]
    current_element = list[i+1]

    # traverse the list
    # and print it to screen.

    current_element = list[0]
    while(current_e lement[1] != 0):
    print current_element[0]
    current_element = current_element[1]
    [/code]
    Hope that made sense
    -k


    Originally posted by akadri1010
    I am new to Python programming..

    Can anyone help me to write a simple Python program that creates a singly linked list and then prints the entries in it??
    I know how to do in C?? But Python does not support pointers, so how to get around with this issue??
    Last edited by pbmods; Oct 7 '07, 02:01 PM. Reason: Added CODE tags.

    Comment

    • mwillis
      New Member
      • Sep 2006
      • 1

      #3
      It isn't necessary to implement a linked list, since Python already has a build-in list data structure.

      [CODE=python]
      myList = []
      myList.append(" this")
      myList.append(" is")
      myList.append(" the")
      myList.append(" list")
      for item in myList:
      print item
      [/CODE]
      Last edited by pbmods; Oct 7 '07, 02:02 PM. Reason: Changed [CODE] to [CODE=python].

      Comment

      • kudos
        Recognized Expert New Member
        • Jul 2006
        • 127

        #4
        well...but you wouldn't have a linked list :)

        Comment

        • pbmods
          Recognized Expert Expert
          • Apr 2007
          • 5821

          #5
          Heya, kudos.

          Please use CODE tags when posting source code:

          [CODE=pytho n]
          Python code goes here.
          [/CODE]

          Comment

          • bartonc
            Recognized Expert Expert
            • Sep 2006
            • 6478

            #6
            Originally posted by pbmods
            Heya, kudos.

            Please use CODE tags when posting source code:

            [CODE=python]
            Python code goes here.
            [/CODE]
            Surely, you are not admonishing one of the Python Forum's experts.
            You must mean mwillis.?

            Comment

            • MojaveKid
              New Member
              • Sep 2007
              • 9

              #7
              maybe something like this....

              Code:
              class Node:
              	def __init__(self,value):
              		self.data = value
              		self.next = 0
              		
              class List:
              	def __init__(self):
              		self.firstNode = Node(0)
              		
              	def __ShowNodeData(self,aNode):
              		if aNode.next != 0:
              		   print aNode.data
              		   self.__ShowNodeData(aNode.next)
              		
              	def Dump(self):
              		self.__ShowNodeData(self.firstNode)
              		
              	def InsertAfter(self,aNode,aNewNode):
              		aNewNode.next = aNode.next
              		aNode.next = aNewNode
              	
              	def InsertBeginning(self,aNewNode):
              		aNewNode.next = self.firstNode
              		self.firstNode = aNewNode	
              	
              nodeA = Node("A")
              nodeB = Node("B")
              nodeC = Node("C")
              nodeD = Node("D")
              
              aList = List()
              
              aList.InsertBeginning(nodeB)
              aList.InsertAfter(nodeB,nodeD)
              aList.InsertAfter(nodeD,nodeC)
              aList.InsertAfter(nodeC,nodeA)
              
              aList.Dump()
              B
              D
              C
              A

              Comment

              • latte
                New Member
                • May 2009
                • 4

                #8
                Originally posted by MojaveKid
                maybe something like this....

                Code:
                class Node:
                	def __init__(self,value):
                		self.data = value
                		self.next = 0
                		
                class List:
                	def __init__(self):
                		self.firstNode = Node(0)
                		
                	def __ShowNodeData(self,aNode):
                		if aNode.next != 0:
                		   print aNode.data
                		   self.__ShowNodeData(aNode.next)
                		
                	def Dump(self):
                		self.__ShowNodeData(self.firstNode)
                		
                	def InsertAfter(self,aNode,aNewNode):
                		aNewNode.next = aNode.next
                		aNode.next = aNewNode
                	
                	def InsertBeginning(self,aNewNode):
                		aNewNode.next = self.firstNode
                		self.firstNode = aNewNode	
                	
                nodeA = Node("A")
                nodeB = Node("B")
                nodeC = Node("C")
                nodeD = Node("D")
                
                aList = List()
                
                aList.InsertBeginning(nodeB)
                aList.InsertAfter(nodeB,nodeD)
                aList.InsertAfter(nodeD,nodeC)
                aList.InsertAfter(nodeC,nodeA)
                
                aList.Dump()
                B
                D
                C
                A
                Hi MojaveKid - this is good! Thanks for doing it!
                I'm doing a public domain app at the moment which needs some code for a linked list, so I was wondering - may I use this in my app? ( You'll be credited as the author of it, no worries there... :) ).
                Thanks again - bye for now -
                - latte

                Comment

                • MojaveKid
                  New Member
                  • Sep 2007
                  • 9

                  #9
                  ahh...sure you can use it...
                  however, you must freely teach every python newbie who might also ask about implementing a linked list in Python...or no deal..ehehehehe he

                  Comment

                  • latte
                    New Member
                    • May 2009
                    • 4

                    #10
                    Originally posted by MojaveKid
                    ahh...sure you can use it...
                    however, you must freely teach every python newbie who might also ask about implementing a linked list in Python...or no deal..ehehehehe he
                    Heh.. sounds ok to me... :) Very many thanks - it's a very nice bit of code!
                    Bye for now -
                    - latte

                    Comment

                    • Sui Generis22
                      New Member
                      • Feb 2012
                      • 1

                      #11
                      Originally posted by kudos
                      Hi
                      actually, python variables do 'points' to things, so in a sense python does support pointers. Here is an example program, that fills a list with numbers 1 to 5. Each
                      element in the list looks the following way: (value, pointer_to_next _element). Then it prints the content of the list
                      [code=python]
                      list = []
                      list.append([1,0])
                      current_element = list[0]

                      # build a linked list 1,2,3,4,5

                      for i in range(5):
                      list.append([i+2,0])
                      current_element[1] = list[i+1]
                      current_element = list[i+1]

                      # traverse the list
                      # and print it to screen.

                      current_element = list[0]
                      while(current_e lement[1] != 0):
                      print current_element[0]
                      current_element = current_element[1]
                      [/code]
                      Hope that made sense
                      -k
                      What is the purpose of the i+ in the [i+2]? I ran the code and removed the i+ why does it continuously print 2?

                      Comment

                      Working...