Skip to main content

Python - Doubly Linked List Allows Append, Prepend, Insert at Index, Delete First, Delete Last, Delete at Index, Delete by Value, and View

Doubly Linked List in Python that allows append, prepend, insert at index, delete first element, delete last element, delete at index, delete by value, and view Linked List operations.
Try this code here: https://repl.it/@VinitKhandelwal/DoublyLinkedList
class DoublyLinkedList:

def __init__(self):
self.reset()
def reset(self):
self.length = 0
self.head = None
self.tail = None

def append(self, value):
new_node = Node(value)
if self.length == 0:
self.head = new_node
self.tail = self.head
else:
new_node.back_pointer = self.tail
self.tail.front_pointer = new_node
self.tail = new_node
self.length += 1

def prepend(self, value):
new_node = Node(value)
if self.length == 0:
self.head = new_node
self.tail = self.head
else:
new_node.front_pointer = self.head
self.head.back_pointer = new_node
self.head = new_node
self.length += 1

def insert_at(self, index, value):
new_node = Node(value)
if self.length == 0:
self.head = new_node
self.tail = self.head
elif index > self.length+1:
new_node.back_pointer = self.tail
self.tail.front_pointer = new_node
self.tail = new_node
print("Index not available. Appended at the end.")
else:
temp = self.head
if index == 0:
self.prepend(value)
self.length -= 1
elif index == self.length+1:
new_node.back_pointer = self.tail
self.tail.front_pointer = new_node
self.tail = new_node
else:
for i in range(0, index):
prev_temp = temp
temp = temp.front_pointer
prev_temp.front_pointer=new_node
new_node.back_pointer = prev_temp
new_node.front_pointer = temp
temp.back_pointer = new_node
self.length += 1

def popfirst(self):
if self.length < 2:
self.reset()
else:
self.head = self.head.front_pointer
self.head.back_pointer = None
self.length -= 1

def poplast(self):
if self.length < 2:
self.reset()
else:
self.tail = self.tail.back_pointer
self.tail.front_pointer = None
self.length -= 1

def delete_at(self, index):
if index == 0:
self.popfirst()
elif index == self.length-1:
self.poplast()
elif index < self.length:
temp = self.head
for i in range(0, index):
prev_temp = temp
temp = prev_temp.front_pointer
prev_temp.front_pointer = temp.front_pointer
temp.front_pointer.back_pointer = prev_temp
self.length -= 1

def delete(self, value):
front = self.head
back = self.tail
for i in range(0, int(self.length/2)):

if front.value == value:
if front == self.head:
self.popfirst()
else:
prev_front.front_pointer = front.front_pointer
prev_front.front_pointer.back_pointer = prev_front
self.length -= 1
prev_front = front
front = prev_front.front_pointer

if back.value == value:
if back == self.tail:
self.poplast()
else:
next_back.back_pointer = back.back_pointer
next_back.back_pointer.front_pointer = next_back
self.length -= 1
next_back = back
back = next_back.back_pointer
def view(self):
print("View")
temp = None
arr = []
for i in range(0, self.length):
if temp == None:
temp = self.head
else:
temp = temp.front_pointer
if temp is not None:
arr.append(temp.value)
print(arr)


class Node:
def __init__(self, value, back_pointer=None, front_pointer=None):
self.value = value
self.back_pointer = back_pointer
self.front_pointer = front_pointer

obj = DoublyLinkedList()
obj.view()
obj.prepend(10)
obj.view()
obj.append(1)
obj.view()
obj.append(10)
obj.view()
obj.prepend(2)
obj.view()
obj.insert_at(3, 7)
obj.view()
obj.prepend(3)
obj.view()
obj.prepend(10)
obj.view()
obj.prepend(4)
obj.view()
obj.prepend(10)
obj.view()
obj.delete(10)
obj.view()
OUTPUT
View
[]
View
[10]
View
[10, 1]
View
[10, 1, 10]
View
[2, 10, 1, 10]
View
[2, 10, 1, 7, 10]
View
[3, 2, 10, 1, 7, 10]
View
[10, 3, 2, 10, 1, 7, 10]
View
[4, 10, 3, 2, 10, 1, 7, 10]
View
[10, 4, 10, 3, 2, 10, 1, 7, 10]
View

[4, 3, 2, 1, 7]

Comments

Popular posts from this blog

Python - List - Append, Count, Extend, Index, Insert, Pop, Remove, Reverse, Sort

🐍 Advance List List is widely used and it's functionalities are heavily useful. Append Adds one element at the end of the list. Syntax list1.append(value) Input l1 = [1, 2, 3] l1.append(4) l1 Output [1, 2, 3, 4] append can be used to add any datatype in a list. It can even add list inside list. Caution: Append does not return anything. It just appends the list. Count .count(value) counts the number of occurrences of an element in the list. Syntax list1.count(value) Input l1 = [1, 2, 3, 4, 3] l1.count(3) Output 2 It returns 0 if the value is not found in the list. Extend .count(value) counts the number of occurrences of an element in the list. Syntax list1.extend(list) Input l1 = [1, 2, 3] l1.extend([4, 5]) Output [1, 2, 3, 4, 5] If we use append, entire list will be added to the first list like one element. Extend, i nstead of considering a list as one element, it joins the two lists one after other. Append works in the following way. Input l1 = [1, 2, 3] l1.append([4, 5]) Output...

Difference between .exec() and .execPopulate() in Mongoose?

Here I answer what is the difference between .exec() and .execPopulate() in Mongoose? .exec() is used with a query while .execPopulate() is used with a document Syntax for .exec() is as follows: Model.query() . populate ( 'field' ) . exec () // returns promise . then ( function ( document ) { console . log ( document ); }); Syntax for .execPopulate() is as follows: fetchedDocument . populate ( 'field' ) . execPopulate () // returns promise . then ( function ( document ) { console . log ( document ); }); When working with individual document use .execPopulate(), for model query use .exec(). Both returns a promise. One can do without .exec() or .execPopulate() but then has to pass a callback in populate.

Python Class to Calculate Distance and Slope of a Line with Coordinates as Input

🐍  Can be run on Jupyter Notebook #CLASS DESIGNED TO CREATE OBJECTS THAT TAKES COORDINATES AND CALCULATES DISTANCE AND SLOPE class Line:     def __init__(self,coor1,coor2):         self.coor1=coor1         self.coor2=coor2 #FUNCTION CALCULATES DISTANCE     def distance(self):         return ((self.coor2[0]-self.coor1[0])**2+(self.coor2[1]-self.coor1[1])**2)**0.5 #FUNCTION CALCULATES SLOPE         def slope(self):         return (self.coor2[1]-self.coor1[1])/(self.coor2[0]-self.coor1[0]) #DEFINING COORDINATES coordinate1 = (3,2) coordinate2 = (8,10) #CREATING OBJECT OF LINE CLASS li = Line(coordinate1,coordinate2) #CALLING DISTANCE FUNCTION li.distance() #CALLING SLOPE FUNCTION li.slope()