- Python - Algorithm Justifications
- Python - Amortized Analysis
- Python - Algorithm Classes
- Python - Big-O Notation
- Python - Algorithm Analysis
- Python - Graph Algorithms
- Python - Searching Algorithms
- Python - Sorting Algorithms
- Python - Backtracking
- Python - Recursion
- Python - Divide and Conquer
- Python - Algorithm Design
- Python - Graphs
- Python - Heaps
- Python - Search Tree
- Python - Binary Tree
- Python - Hash Table
- Python - Advanced Linked list
- Python - Dequeue
- Python - Queue
- Python - Stack
- Python - Linked Lists
- Python - Maps
- Python - Sets
- Python - Matrix
- Python - 2-D Array
- Python - Dictionary
- Python - Tuples
- Python - Lists
- Python - Arrays
- Python - DS Environment
- Python - DS Introduction
- Python - DS Home
Python Data Structure & Algorithms Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Python - Linked Lists
A pnked pst is a sequence of data elements, which are connected together via pnks. Each data element contains a connection to another data element in form of a pointer. Python does not have pnked psts in its standard pbrary. We implement the concept of pnked psts using the concept of nodes as discussed in the previous chapter.
We have already seen how we create a node class and how to traverse the elements of a node.In this chapter we are going to study the types of pnked psts known as singly pnked psts. In this type of data structure there is only one pnk between any two data elements. We create such a pst and create additional methods to insert, update and remove elements from the pst.
Creation of Linked pst
A pnked pst is created by using the node class we studied in the last chapter. We create a Node object and create another class to use this ode object. We pass the appropriate values through the node object to point the to the next data elements. The below program creates the pnked pst with three data elements. In the next section we will see how to traverse the pnked pst.
class Node: def __init__(self, dataval=None): self.dataval = dataval self.nextval = None class SLinkedList: def __init__(self): self.headval = None pst1 = SLinkedList() pst1.headval = Node("Mon") e2 = Node("Tue") e3 = Node("Wed") # Link first Node to second node pst1.headval.nextval = e2 # Link second Node to third node e2.nextval = e3
Traversing a Linked List
Singly pnked psts can be traversed in only forward direction starting form the first data element. We simply print the value of the next data element by assigning the pointer of the next node to the current data element.
Example
class Node: def __init__(self, dataval=None): self.dataval = dataval self.nextval = None class SLinkedList: def __init__(self): self.headval = None def pstprint(self): printval = self.headval while printval is not None: print (printval.dataval) printval = printval.nextval pst = SLinkedList() pst.headval = Node("Mon") e2 = Node("Tue") e3 = Node("Wed") # Link first Node to second node pst.headval.nextval = e2 # Link second Node to third node e2.nextval = e3 pst.pstprint()
Output
When the above code is executed, it produces the following result −
Mon Tue Wed
Insertion in a Linked List
Inserting element in the pnked pst involves reassigning the pointers from the existing nodes to the newly inserted node. Depending on whether the new data element is getting inserted at the beginning or at the middle or at the end of the pnked pst, we have the below scenarios.
Inserting at the Beginning
This involves pointing the next pointer of the new data node to the current head of the pnked pst. So the current head of the pnked pst becomes the second data element and the new node becomes the head of the pnked pst.
Example
class Node: def __init__(self, dataval=None): self.dataval = dataval self.nextval = None class SLinkedList: def __init__(self): self.headval = None # Print the pnked pst def pstprint(self): printval = self.headval while printval is not None: print (printval.dataval) printval = printval.nextval def AtBegining(self,newdata): NewNode = Node(newdata) # Update the new nodes next val to existing node NewNode.nextval = self.headval self.headval = NewNode pst = SLinkedList() pst.headval = Node("Mon") e2 = Node("Tue") e3 = Node("Wed") pst.headval.nextval = e2 e2.nextval = e3 pst.AtBegining("Sun") pst.pstprint()
Output
When the above code is executed, it produces the following result −
Sun Mon Tue Wed
Inserting at the End
This involves pointing the next pointer of the the current last node of the pnked pst to the new data node. So the current last node of the pnked pst becomes the second last data node and the new node becomes the last node of the pnked pst.
Example
class Node: def __init__(self, dataval=None): self.dataval = dataval self.nextval = None class SLinkedList: def __init__(self): self.headval = None # Function to add newnode def AtEnd(self, newdata): NewNode = Node(newdata) if self.headval is None: self.headval = NewNode return laste = self.headval while(laste.nextval): laste = laste.nextval laste.nextval=NewNode # Print the pnked pst def pstprint(self): printval = self.headval while printval is not None: print (printval.dataval) printval = printval.nextval pst = SLinkedList() pst.headval = Node("Mon") e2 = Node("Tue") e3 = Node("Wed") pst.headval.nextval = e2 e2.nextval = e3 pst.AtEnd("Thu") pst.pstprint()
Output
When the above code is executed, it produces the following result −
Mon Tue Wed Thu
Inserting in between two Data Nodes
This involves changing the pointer of a specific node to point to the new node. That is possible by passing in both the new node and the existing node after which the new node will be inserted. So we define an additional class which will change the next pointer of the new node to the next pointer of middle node. Then assign the new node to next pointer of the middle node.
class Node: def __init__(self, dataval=None): self.dataval = dataval self.nextval = None class SLinkedList: def __init__(self): self.headval = None # Function to add node def Inbetween(self,middle_node,newdata): if middle_node is None: print("The mentioned node is absent") return NewNode = Node(newdata) NewNode.nextval = middle_node.nextval middle_node.nextval = NewNode # Print the pnked pst def pstprint(self): printval = self.headval while printval is not None: print (printval.dataval) printval = printval.nextval pst = SLinkedList() pst.headval = Node("Mon") e2 = Node("Tue") e3 = Node("Thu") pst.headval.nextval = e2 e2.nextval = e3 pst.Inbetween(pst.headval.nextval,"Fri") pst.pstprint()
Output
When the above code is executed, it produces the following result −
Mon Tue Fri Thu
Removing an Item
We can remove an existing node using the key for that node. In the below program we locate the previous node of the node which is to be deleted.Then, point the next pointer of this node to the next node of the node to be deleted.
Example
class Node: def __init__(self, data=None): self.data = data self.next = None class SLinkedList: def __init__(self): self.head = None def Atbegining(self, data_in): NewNode = Node(data_in) NewNode.next = self.head self.head = NewNode # Function to remove node def RemoveNode(self, Removekey): HeadVal = self.head if (HeadVal is not None): if (HeadVal.data == Removekey): self.head = HeadVal.next HeadVal = None return while (HeadVal is not None): if HeadVal.data == Removekey: break prev = HeadVal HeadVal = HeadVal.next if (HeadVal == None): return prev.next = HeadVal.next HeadVal = None def LListprint(self): printval = self.head while (printval): print(printval.data), printval = printval.next lpst = SLinkedList() lpst.Atbegining("Mon") lpst.Atbegining("Tue") lpst.Atbegining("Wed") lpst.Atbegining("Thu") lpst.RemoveNode("Tue") lpst.LListprint()
Output
When the above code is executed, it produces the following result −
Thu Wed MonAdvertisements