Delete Middle Node in Python [from a purely C background] - python

I have been playing around with Python more recently due to its power and syntax. I've decided to make a linked list from https://www.tutorialspoint.com but with my strong background in C and being use to its manual memory allocation as well as having pointers for one the delete middle function is somewhat confusing to me. I know that objects makes references to other objects and variables and that the Python interpreter automatically handles this:
class Node:
def __init__(self, data=None):
self.data = data
self.next = None
class SLinkedList:
def __init__(self):
self.head = None
def printlist(self):
cur = self.head
while cur is not None:
print(cur.data)
cur = cur.next
def insertAtHead(self, newdata):
NewNode = Node(newdata) #create new node
newNode.next = self.head #create its .next to point to the current node
self.head = newNode #set its .next to point the current head
#creates a linked list with 3 elements
def createOneTwoThree(self):
list = SLinkedList()
list.head = Node('Mon')
e2 = Node('Tues')
e3 = Node('Wed')
list.head.next = e2
e2.next = e3
#function to add at the end of a linked list
def insertAtEnd(self, newdata):
newNode = Node(newdata)#create new node
if self.head = newNode:#if empty insert the thing
self.head = newNode
return
laste = self.head
while(last.next):
last = last.next
last.next = newNode
def inBetween(self, middle_node,newdata):
if middle_node is None:
print('Error 0: The mentioned node is absent')
return
newNode = Node(newdata)
newNode.next = middle_node.next
middle_node.next = newNode
def delete(self, removeKey):
headVal = self.head
if(head is not None):
if (head.data == removeKey):
break
prev = head
head = None
while(head is not None):
if head.data == removeKey:
break
prev = head
head = head.next
if (head == None):
return
prev.next = head.next
head = None
#Link Firt node to second node: list1.head.next = n2*******
I just fail to see what's going on here from a memory point of view that justifies it finding the in-between two said values and having them linked together. I am still new to Python and its classes and structures but from an intuitive aspect the inBetween method makes little sense to me though I see it's most underlying logic. Can someone more versed in Python please explain to my WHY this is happening and what exactly is going on with the objects, references and pointers?

Related

Python : I am not able to prepend a Value in a Singly Linked list

Code written in python:
prepend Function is supposed to add a value to the start of the Linked List
display Function is supposed to display the linked list in a List format in python
fromarr Function is supposed to add new nodes to the linked list
class Node:
def __init__(self,value=None):
self.value = value
self.next = None
class SinglyLL:
def __init__(self):
self.head = Node()
self.tail = Node()
def prepend(self,value):
newnode = Node(value)
newnode.next = self.head
self.head = newnode
def append(self,data):
newnode = Node(data)
if self.head is None:
self.head = newnode
return
cur = self.head
while cur.next != None:
cur = cur.next
cur.next = newnode
newnode.next = None
def display(self):
lis = []
cur = self.head
while cur.next != None:
cur = cur.next
lis.append(cur.value)
return lis
def fromarr(self,list):
for i in list:
temp = Node(i)
self.append(temp.value)
newsll = SinglyLL()
newsll.fromarr([1,2,3,4,5])
newsll.prepend(10)
print(newsll.display())
Expected output : [10,1,2,3,4,5]
Actual Output: [None,1,2,3,4,5]
well you are initalising your head with a empty node, and displaying values from self.head.next, so instead of setting element at position 0 you need to set element at position 1.
Adding below code for prepend method
class Node:
def __init__(self,value=None):
self.value = value
self.next = None
class SinglyLL:
def __init__(self):
self.head = Node()
self.tail = Node()
def prepend(self,value):
newnode = Node(value)
if self.head is None:
self.head = newnode
else:
tmp = self.head.next
self.head.next = newnode
newnode.next = tmp
def append(self,data):
newnode = Node(data)
if self.head is None:
self.head = newnode
return
cur = self.head
while cur.next != None:
cur = cur.next
cur.next = newnode
newnode.next = None
def display(self):
lis = []
cur = self.head
while cur.next != None:
cur = cur.next
lis.append(cur.value)
return lis
def fromarr(self,list):
for i in list:
temp = Node(i)
self.append(temp.value)
newsll = SinglyLL()
newsll.fromarr([1,2,3,4,5])
newsll.prepend(10)
print(newsll.display())
Your code has a mix of several approaches to organise a linked list data structure, but they are not compatible:
Your constructor creates a dummy node for the head attribute, which could be an approach when you intend to append all data nodes after that dummy node. The display method is in line with that approach as it doesn't include the value of the dummy node in the result. But the prepend nor the append method use that approach as they treat the head node as a data node.
Your constructor creates a dummy node for the tail attribute. This is an approach that might be used for a doubly linked list, but is not useful for a singly linked list, as the process to append a new node at the end of the list cannot take benefit from this reference.
Although the constructor defines a tail attribute none of the other methods make use of it, nor update it when necessary.
Some other remarks:
It is odd that the display method does not display anything. It would be more appropriate to have an __iter__ method instead which would yield the linked list's values.
fromarr is not a very good name for a method that accepts a list.
It is not intuitive that this is an instance method, as its name suggests that it will create a new list from those values, but you actually need to first create a linked list instance yourself, and then call this method. I would suggest to drop this method and extend the constructor with optional arguments which will be used to populate the constructed linked list.
list is a bad name for a parameter as this name is already in use by native Python.
It would be nice if the Node constructor would accept an optional argument for defining the next attribute.
Here is an update of your code that takes those points into account, and which uses the approach where:
A tail attribute is set and used
No dummy nodes are created
class Node:
def __init__(self, value=None, nxt=None):
self.value = value
self.next = nxt
class SinglyLL:
def __init__(self, *values):
# Don't create nodes here
self.head = self.tail = None
# Allow immediate population from arguments
for value in values:
self.append(value)
def prepend(self,value):
self.head = Node(value, self.head)
self.tail = self.tail or self.head
def append(self, data):
if not self.tail:
self.prepend(data)
else:
self.tail.next = self.tail = Node(data)
def __iter__(self):
cur = self.head
while cur:
yield cur.value
cur = cur.next
newsll = SinglyLL(1,2,3,4,5)
newsll.prepend(10)
print(list(newsll)) # [10, 1, 2, 3, 4, 5]

Python - Removing a node from a linked-list at beginning, in between and at end

I learned about linked-lists today. I learned how to insert and remove nodes from them. In the following code, it teaches me how to insert nodes with three distinct functions: at beginning (the head node), in between and at the end. However, they teach me how to remove the node in and single function. I don't find the code in the remove function to be very clear. Can anyone help make an easier to understand code under the remove function? Is there a way to make it into three functions just like the inserting ones? I'm open to any suggestion or explanation. Thanks in advance.
Here's my code:
class Node:
def __init__(self, data=None):
self.data = data
self.nextnode = None
class LinkedList:
def __init__(self):
self.headnode = None
def printlist(self):
node = self.headnode
while node is not None:
print (node.data)
node = node.nextnode
def atbegining(self,new_node):
new_node.nextnode = self.headnode
self.headnode = new_node
# Function to add newnode
def AtEnd(self, newnode):
if self.headnode is None:
self.headnode = newnode
return
node = self.headnode
while(node.nextnode):
node = node.nextnode
node.nextnode=newnode
# Function to add node
def Inbetween(self,preNode,newNode):
if preNode is None:
print("The mentioned node is absent")
return
newNode.nextnode = preNode.nextnode
preNode.nextnode = newNode
# Function to remove node
def RemoveNode(self, RemoveVal):
node = self.headnode
if (node is not None):
if (node.data == RemoveVal):
self.headnode = node.nextnode
node = None
return
while (node is not None):
if node.data == RemoveVal:
break
prevnode = node
node = node.nextnode
if (node == None):
return
prevnode.nextnode = node.nextnode
node = None
list1 = LinkedList()
list1.headnode = Node("Mon")
n2 = Node("Tue")
n3 = Node("Wed")
# Link first Node to second node
list1.headnode.nextnode = n2
# Link second Node to third node
n2.nextnode = n3
n4 = Node("Sun")
n5 = Node("Tur")
n6 = Node("Newdate")
list1.atbegining(n4)
list1.AtEnd(n5)
list1.Inbetween(list1.headnode,n6)
list1.RemoveNode("Newdate")
list1.printlist()
RemoveNode is complicated by the fact that there are two structurally distinct kinds of LinkedLists: one whose head is None, and one whose head is not None. You can fix this by making sure every LinkedList contains at least one node. This is typically referred to as a dummy node, and you can use this node to store metadata (such as the length of the list).
The Node class itself does not change.
class Node:
def __init__(self, data=None):
self.data = data
self.nextnode = None
The LinkedList, however, simplifies by creating a dummy node. This provides
the guarantee that every node that stores real data is point to by another node.
class LinkedList:
def __init__(self):
self.headnode = Node(0)
def insert(self, preNode, newNode):
newNode.nextnode = preNode.nextnode
preNode.nextnode = newNode
self.headnode.data += 1
def append(self, newNode):
curr = self.headnode
while curr.nextNode is not None:
curr = curr.nextNode
self.insert(curr, newNode)
def prepend(self, newNode):
self.insert(self.headnode, newNode)
def _find_before(self, val):
pre = self.headnode
while pre.nextnode is not None:
curr = pre.nextnode
if curr.data == val:
return pre
pre = curr
def remove(self, RemoveVal):
pre = self._find_before(RemoveVal)
if pre is None:
return
pre.nextnode = pre.nextnode.nextnode
self.headnode.data -= 1
This simplifies all three insertions. The general case can always apply, since there is always a node that comes before the node you insert. append and prepend are simple wrappers that find the appropriate node to pass to insert.
Likewise, remove simply finds the node before the given value, and if the search succeeds, handles updating the prior node's nextnode attribute.
insert and remove also update the size of the list stored in the dummy node.
A find method becomes a simple wrapper around _find_before; if you find a node before the value you are looking for, just return the node that follows it.
I think that an alternative design will make the code much clearer. Consider for example the following:
class Node:
def __init__(self, data=None):
self.data = data
self.nextnode = None
def printlist(self):
print(self.data)
if self.nextnode is not None:
self.nextnode.printlist()
def push(self, node):
node.nextnode = self
return node
def insertafter(self, node):
node.nextnode = self.nextnode
self.nextnode = node
return self
def append(self, node):
lastnode = self
while lastnode.nextnode is not None:
lastnode = lastnode.nextnode
lastnode.nextnode = node
return self
def remove(self, value):
prev = None
walk = self
while walk is not None:
if walk.data == value:
if prev is None:
return walk.nextnode
else:
prev.nextnode = walk.nextnode
return self
else:
prev = walk
walk = walk.nextnode
return self
list1 = Node("Mon")
n2 = Node("Tue")
n3 = Node("Wed")
# Link first Node to second node
list1 = list1.insertafter(n2)
# Link second Node to third node
n2 = n2.insertafter(n3)
n4 = Node("Sun")
n5 = Node("Tur")
n6 = Node("Newdate")
list1 = list1.push(n4)
list1 = list1.append(n5)
list1 = list1.insertafter(n6)
list1 = list1.remove("Newdate")
list1.printlist()
The main idea is that a Node is the linked list. As long as you have the head of the list kept in a variable, you can have access to the entire list, without the need for a separate data structure.

Why does calling a function and writing statements explicitly work differently in Python?

I need an insert to head operation for a linked list that I implemented. However, doing this operation by function call (like insertToHead) and writing the statements explicitly where I need them produces different results. I wonder which property of Python leads to that difference but I couldn't figure it out.
To be more specific, let's say that I have the following class for the linked list:
class Node:
value = None
nextNode = None
def __init__(self, value):
self.value = value
def insertToHead(self, value):
newHead = Node(value)
newHead.nextNode = self
return newHead
For a linked list with a single element (say, 2) I want to insert a node (say, 0) to the head to make linked list 0 -> 2.
I created the linked list the following way
head = Node(2)
Then I tried to insert 0 to head two ways:
Writing the statements explicitly where I need them
newHead = Node(0)
newHead.next = head
head = newHead
Now head is 0, not 0 -> 2.
Calling insertToHead
head = head.insertToHead(0)
head is 0 -> 2 after this statement.
Does anyone know why these two approaches result in differently?
You have a typo. newHead.next should be newHead.nextNode.
A simple implementation of Singly Linked Lists:
class Node:
def __init__(self, value = None, nextNode = None):
self.value = value
self.nextNode = nextNode
class LinkedList:
def __init__(self):
self.head = None # will point to the head of the list
self.tail = None # will point to the tail of the list
self.size = 0 # size of the linked list
def insert_to_head(self, data):
# when push front, the head of the linked list will be Node()
self.head = Node(data, self.head)
if self.tail == None: # if tail is None, means it is a single element
self.tail = self.head
self.size += 1 # increase size by one
def __str__(self):
ret_str = ""
node = self.head
while node != None:
ret_str += str(node.value) + " -> "
node = node.nextNode
return ret_str
myLinkedList = LinkedList()
myLinkedList.insert_to_head(3)
myLinkedList.insert_to_head(2)
print(myLinkedList)

Insertion before a node in Doubly link list

i'm trying to insert a node before a node into doubly linked list.After performing the operation i'm not getting the correct results.
class createnode:
def __init__(self,data):
self.data=data
self.next=None
self.prev=None
class Unordered_list:
def __init__(self):
self.head=None
######Insertion at the starting###
def buildlst(self,data):
node=createnode(data)
if self.head is None:
self.head=node
else:
node.next=self.head
node.prev=None
self.head=node
#######INsertion at the end####
def buildlstend(self,data):
node=createnode(data)
ptr=self.head
while(ptr.next):
ptr=ptr.next
ptr.next=node
ptr=node.prev
#######INsertion before some node i.e searched node####
def insertbeforenode(self,data,srch_data):
node=createnode(data)
ptr=self.head
while(ptr):
if ptr.data==srch_data:
node.prev=ptr.prev
node.next=ptr
ptr.prev=node
ptr=ptr.next
########Printitng the list########
def printlist(self):
temp=self.head
while(temp):
print(temp.data)
temp=temp.next
A=Unordered_list()
A.buildlst(10)
A.buildlst(20)
A.buildlstend(30)
A.printlist()
print("Here i will insert a new node 50 before 10")
A.insertbeforenode(50,10)
A.printlist()
Mylist look some what like 20,10,30.I want to have the new node before 10.but it prints me the same results.Have been through this link Inserting a node before a given node in doubly linked list .My program is in python so couldn.t collect much from it.
insertbeforenode is my function from where i'm making a call.
Python supports a built-in mechanism to work with lists, you could easily do something like this:
list = []
list.append(10)
list.insert(0, 20)
list.append(30)
list.insert(1, 50)
See the official documentation for more examples https://docs.python.org/3/tutorial/datastructures.html.
Anyway if you want to program a double linked list by your self I have made some changes to your code:
class createnode:
def __init__(self, data):
self.data = data
self.next = None
self.prev = None
class Unordered_list:
def __init__(self):
self.head = None
def buildlst(self, data):
node = createnode(data)
if self.head is None:
self.head = node
else:
node.next = self.head
self.head.prev = node # <---
# node.prev = None <---- this is not necessary
self.head = node
def buildlstend(self, data):
node = createnode(data)
ptr = self.head
while ptr.next:
ptr = ptr.next
ptr.next = node
node.prev = ptr # <---
def insertbeforenode(self, data, srch_data):
if srch_data == self.head.data: # <-- if you try to insert before the head
self.buildlst(data)
else:
node = createnode(data)
ptr = self.head
while ptr:
if ptr.data == srch_data:
node.prev = ptr.prev
node.next = ptr
ptr.prev = node
node.prev.next = node # <--
break # <--- when you find your node you have to quit from the loop
else:
ptr = ptr.next
def printlist(self):
temp = self.head
while (temp):
print(temp.data)
temp = temp.next
I have put an arrow (# <--) where I made the changes. There were a couples of mistakes on working with pointers and others inaccuracies. Please try this and let me know!

Circular Linked list in python

def delete_node(head, value):
p=head
if p is None:
return None
while p.value!=value:
p=p.next
if p.next is head and p.value!=value:
return head
p.value=p.next.value
if p.next==head:
head=p
p.next=p.next.next
return head
The above is my code for deleting a node in a circular linked list based on value of the node!
The code doesn't gimme result for this case-- I've only 1 element in the list and i deleted it.. So the resultant should be an empty set.. But because i took p.value=p.next.value it points back again to itself and the same value is in the list! Can anyone help me out! Thanx in advance! :)
The easiest solution here is to have a dummy node that points to itself in case of an empty list. As a consequence in an empty list we have one node that points to itself (the dummy), in a list with one element the dummy points to the element and the element points to the dummy.
Avoids the need for any special cases and generally simplifies the code. To check if the list is empty you can then just do dummy.next is dummy, also nice.
enter link description here
class node:
def __init__(self, data):
self.data = data
self.next = None
class circularLinkedList:
def __init__(self):
self.head = None
self.tail = None
def insertNode(self, data):
newnode = node(data)
if self.tail is None:
self.head = self.tail = newnode
else:
newnode.next = self.head
self.tail.next = newnode
self.tail = self.tail.next
def printCLL(self):
head = self.head
tail = self.tail
while head.next is not None and head.next is not tail:
print(head.data, end="---")
head = head.next
print(head.data, end="---")
print(tail.data)
c = circularLinkedList()
c.insertNode(1)
c.insertNode(1)
c.insertNode(3)
c.insertNode(4)
c.insertNode(5)
c.printCLL()

Categories