Instagram
youtube
Facebook
Twitter

Nodes Implementation

Nodes Implementation

  • The data contained within nodes can be a variety of types, it can be an integer, string, or an array or it could be nothing null.
  • The links within the node are referred to as pointers as they point to another node.
  • In our python code for nodes first, we are going to make a node class that has a value parameter and links to other nodes and we have 3 methods set_link_node(), get_link_node() and get_value() to link a node, get the linked node and get the value.
    class Node:
      def __init__(self, value, link_node=None):
        self.value = value
        self.link_node = link_node
        
      def set_link_node(self, link_node):
        self.link_node = link_node
        
      def get_link_node(self):
        return self.link_node
      
      def get_value(self):
        return self.value

    code test:

  • A = Node(10)
    B = Node(100)
    C = Node(1000)
    
    A.set_link_node(B)
    B.set_link_node(C)
    
    B_data = A.get_link_node().get_value()
    C_data =  B.get_link_node().get_value()
    
    print("node B data: ", B_data)
    print("node C data: ", C_data)

    Output:                                                                                                                                                                                     

    node B data: 100
    node C data: 1000