0% found this document useful (0 votes)
71 views9 pages

Week-6 Data Structure

The document discusses linked lists and their implementation in Python. It describes how to create nodes, link nodes together to form a linked list, traverse the linked list, search for a node, prepend nodes to the front of the list, and remove nodes from the linked list. Key steps and time complexities are provided for each linked list operation.

Uploaded by

jetkingbelgaum
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views9 pages

Week-6 Data Structure

The document discusses linked lists and their implementation in Python. It describes how to create nodes, link nodes together to form a linked list, traverse the linked list, search for a node, prepend nodes to the front of the list, and remove nodes from the linked list. Key steps and time complexities are provided for each linked list operation.

Uploaded by

jetkingbelgaum
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

DATA STRUCTURES WITH PYTHON CODE:20CS41P

The Singly Linked List

 A linked list is a linear collection of data elements where each


data element stored at a separate location.
 Each data element called node.
 Linked list consists of a group of nodes where each node point
to the next node through a pointer.
 A Linked list is made up of nodes, where a node is made up of
two parts:
Data Part: Holds the data
Address Part: Holds the address of the next node.

 A Singly Linked List is a linked list in which each node points


to only the next node.
 Singly linked lists can be traversed in only forward direction
starting from the first data element.
Fig. 6.1 provides an example of a singly linked list consisting of four
nodes.

Fig. 6.1 A Singly Linked List consisting of four nodes.

Creating a node

To create a singly linked list in python below listed steps are


followed.

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

#creating llist object to LinkedList class llist = LinkedList()


llist
Output: None

SEARCH EDUCATIONS Page 1


DATA STRUCTURES WITH PYTHON CODE:20CS41P

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 3: Create a new node and assign it a value.


first_node = Node(50)
when Node object is created self.data member will be assigned 50 and
self. next member will be assigned None(null).
The node is referred by first_node

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

SEARCH EDUCATIONS Page 2


DATA STRUCTURES WITH PYTHON CODE:20CS41P

Traversing the Nodes

Traversing the Nodes


 One of the most common things you will do with a linked list is to
traverse it.
 Traversing means going through every single node, starting with
the head of the linked list and ending on the node that has a next
value of None.
 A singly Linked list can only be traversed in forward direction from
the first element to the last.
 We get the value of the next data element by simply iterating with
the help of the reference address.
 We can use a temporary external reference, curNode to traverse
through the list, moving the reference along as we access the
individual nodes.
 The process starts by assigning a temporary external reference
curNode to point to the first node of the list, as illustrated in
Fig. 6.2 (a).curNode=self.head
 After entering the loop, the value stored in the first node is printed
by accessing the data component stored in the node using the
external reference.
 The external reference is then advanced to the next node by
assigning it the value of the current node’s link field, as illustrated
in Fig. 6.2(b).

while curNode!=None:
print (curNode.data)
curNode=curNode.next

 The loop iteration continues until every node in the list has been
accessed.

 The completion of the traversal is determined when curNode


becomes null, as illustrated in Fig. 6.2(f).

SEARCH EDUCATIONS Page 3


DATA STRUCTURES WITH PYTHON CODE:20CS41P

SEARCH EDUCATIONS Page 4


DATA STRUCTURES WITH PYTHON CODE:20CS41P

Fig.6.2 Traversing a linked list by using temporary external reference


variable.

A complete list traversal requires O(n) time since each node must be
accessed and each access only requires constant time.

Searching for a Node

 A linear search operation can be performed on a linked list.


 It is very similar to the traversal demonstrated earlier.
 The only difference is that the loop can terminate early if we find
the target value within the list.
 The implementation of the linear search is illustrated below

def unorderedSearch( head, target ): curNode = head


while curNode!=None and curNode.data!=target :
curNode= curNode.next
return curNode!=None

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

Prepend operation will insert element at beginning of linked list.


Suppose we want to add the value 96 to our example list shown in
Fig. 6.3 (a). Adding an item to the front of the list requires
following steps.
 We must create a new node to store the new value
newNode = ListNode( item )
 Set its next field to point to the node currently at the front of the
list. We then adjust head to point to the new node since it is now
the first node in the list.

newNode.next = head head = newNode

SEARCH EDUCATIONS Page 5


DATA STRUCTURES WITH PYTHON CODE:20CS41P

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

An item can be removed from a linked list by removing or unlinking the


node containing that item.

Consider the linked list shown in Fig. 6.4 and assume we want to remove
the node containing 18.

Fig. 6.4 The original list before removing node

 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).

 The node’s link field is also cleared by setting it to None.

SEARCH EDUCATIONS Page 6


DATA STRUCTURES WITH PYTHON CODE:20CS41P

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).

SEARCH EDUCATIONS Page 7


DATA STRUCTURES WITH PYTHON CODE:20CS41P

Fig. 6.6 Using a second temporary reference to remove a node from a


linked list
(a) positioning the second temporary reference variable predNode
(b) the resulting
list after removing 18 from the linked list.

We use two reference nodes predNode which represents predecessor node


and curNode which represents current node.

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.

curNode = head predNode = None

Step2: A loop is used to position the two temporary external reference


variables.
 As the curNode reference is moved along the list in the body of the
loop, the predNode reference follows behind.
 Thus, predNode must be assigned to reference the same node as
curNode before advancing curNode to reference the next node.

while curNode!=None and curNode.


data != target :
predNode = curNode
curNode = curNode.next

Step 3: After positioning the two external references, there are three
possible conditions:

(1)The item is not in the list


(2)The item is in the first node
(3)The item is somewhere else in the list.

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.

SEARCH EDUCATIONS Page 8


DATA STRUCTURES WITH PYTHON CODE:20CS41P

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

if curNode is not None : if curNode is head :


head = curNode.next else :
predNode.next = curNode.next

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

An iterator is an object that provides a mechanism for performing


generic traversals through a container without having to expose the
underlying implementation.
Iterator in python is an object that is used to iterate over iterable
objects like lists, tuples, dicts, and sets.
An iterator guarantees that each element is visited exactly once.
To use Python’s traversal mechanism with our own abstract data
types, we must define an iterator class, which is a class in Python
containing two special methods, iter and next.
The iterator object is initialized using the iter() method. It uses the
next() method for iteration.
The iter method simply returns a reference to the object itself
def iter ( self ): return self
The next method is called to return the next item in the container.
The method first saves a reference to the current item indicated by
the loop variable.
The loop variable is then incremented by one to prepare it for the
next invocation of the next method.

SEARCH EDUCATIONS Page 9

You might also like