MCPcopy Index your code
hub / github.com/BeeBombshell/Python-DSA / DoublyLinkedList

Class DoublyLinkedList

DSA/DoublyLinkedList.py:8–92  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

6
7#class to create the Doubly Linked List
8class DoublyLinkedList:
9
10 #constructor to initialise the doubly linked list, head and tail initialised to 'None'
11 def __init__(self):
12 self.head = self.tail = None
13
14 #method to check if the doubly linked list is empty or not
15 def isEmpty(self):
16 if self.head == self.tail == None:
17 return True
18 return False
19
20 #method to add an element in front the list i.e. head
21 def addFront(self, x):
22 new_node = Node(x)
23 if (self.isEmpty()):
24 self.head = self.tail = new_node
25 new_node.prev = None
26 return
27 new_node.next = self.head
28 self.head.prev = new_node
29 self.head = new_node
30 new_node.prev = None
31
32 #method to add an element in the last of the list i.e. tail
33 def addTail(self, x):
34 new_node = Node(x)
35 if (self.isEmpty()):
36 self.head = self.tail = new_node
37 new_node.next = None
38 return
39 self.tail.next = new_node
40 new_node.prev = self.tail
41 new_node.next = None
42 self.tail = new_node
43
44 #method to insert an element at a specific position
45 def insert(self, x, pos):
46 new_node = Node(x)
47 temp = self.head
48 i = 1
49 while i < pos-1:
50 temp = temp.next
51 i += 1
52 new_node.prev = temp
53 new_node.next = temp.next
54 temp.next.prev = new_node
55 temp.next = new_node
56
57 #method to remove the element at head
58 def removeHead(self):
59 temp = self.head
60 self.head = temp.next
61 temp.next.prev = None
62 temp = None
63
64 #method to remove element at tail
65 def removeTail(self):

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected