Week-6 Data Structure
Week-6 Data Structure
Creating a node
Step 1: First we create a class to represent the linked list. In this class
include empty head or first reference and initialize it with null.
class LinkedList:
def init ( self) : self.head = None
Step 2: Next, create another class to represent each node of the linked list.
The objects of this class contain one-member variable to store the data of
the node and another member variable to store the reference(address) of
the next node.
class Node :
def init ( self, data ) : self.data = data self.next = None
Step 4: Insert node in the linked list, since we don’t have any elements in
the linked list so far, we will link head to this node by assigning this node
to head field of linked list object
llist.head = first_node
llist
Output: 50 None
Like this, we can create nodes as required and set the references. Create
another new node and link first_node to this second_node by assigning its
reference address to first_node next field.
second_node = Node(60)
first_node.next = second_node
llist
Output:
50
60
None
while curNode!=None:
print (curNode.data)
curNode=curNode.next
The loop iteration continues until every node in the list has been
accessed.
A complete list traversal requires O(n) time since each node must be
accessed and each access only requires constant time.
The linked list search operation requires O(n) in the worst case, which
occurs when the target item is not in the list.
Prepending Nodes
Fig.6.3 Prepending a node to the linked list: (a) the original list (b) link
modifications required to prepend the node (c) the result after prepending
96.
Removing Nodes
Consider the linked list shown in Fig. 6.4 and assume we want to remove
the node containing 18.
First, we must find the node containing the target value and
position an external reference variable pointing to it, as illustrated
in Fig. 6.5(a).
After finding the node, it has to be unlinked from the list, which
entails adjusting the link field of the node’s predecessor to point to
its successor as shown in Fig. 6.5(b).
Fig. 6.5 Deleting a node from a linked list: (a) finding the node to be
removed and assigning an external reference variable and (b) the link
modifications required to unlink and remove a node.
Accessing the node’s successor is very simple using the next link of
the node. But we must also access the node’s predecessor in order
to change its link.
The only way we can do this is to position another external
reference simultaneously during the search for the given node, as
illustrated in Fig. 6.6(a). The result after removing the node
containing value 18 is shown in Fig. 6.6(b).
Step 1: The curNode external reference is initially set to the first node in
the list.
The predNode external reference is set to None since there is no
predecessor to the first node in the list.
Step 3: After positioning the two external references, there are three
possible conditions:
If the target is not in the list, curNode will be null, assigned None
via the link field of the last node.
To determine if the target is in the first node, we can simply
compare curNode to head and determine if they reference the same
node.
If they do, we set head to point to the next node in the list.
If the target is elsewhere in the list, we simply adjust the link field
of the node referenced by predNode to point to the node following
the one referenced by curNode.
This step is performed in the else clause of the condition
Removing a node from a linked list requires O(n) time since the node
could be at the end of the list, in which case a complete traversal is
required to locate the node.
List Iterator