1# Initialise the Node
2class Node:
3 def __init__(self, data):
4 self.item = data
5 self.next = None
6 self.prev = None
7# Class for doubly Linked List
8class doublyLinkedList:
9 def __init__(self):
10 self.start_node = None
11 # Insert Element to Empty list
12 def InsertToEmptyList(self, data):
13 if self.start_node is None:
14 new_node = Node(data)
15 self.start_node = new_node
16 else:
17 print("The list is empty")
18 # Insert element at the end
19 def InsertToEnd(self, data):
20 # Check if the list is empty
21 if self.start_node is None:
22 new_node = Node(data)
23 self.start_node = new_node
24 return
25 n = self.start_node
26 # Iterate till the next reaches NULL
27 while n.next is not None:
28 n = n.next
29 new_node = Node(data)
30 n.next = new_node
31 new_node.prev = n
32 # Delete the elements from the start
33 def DeleteAtStart(self):
34 if self.start_node is None:
35 print("The Linked list is empty, no element to delete")
36 return
37 if self.start_node.next is None:
38 self.start_node = None
39 return
40 self.start_node = self.start_node.next
41 self.start_prev = None;
42 # Delete the elements from the end
43 def delete_at_end(self):
44 # Check if the List is empty
45 if self.start_node is None:
46 print("The Linked list is empty, no element to delete")
47 return
48 if self.start_node.next is None:
49 self.start_node = None
50 return
51 n = self.start_node
52 while n.next is not None:
53 n = n.next
54 n.prev.next = None
55 # Traversing and Displaying each element of the list
56 def Display(self):
57 if self.start_node is None:
58 print("The list is empty")
59 return
60 else:
61 n = self.start_node
62 while n is not None:
63 print("Element is: ", n.item)
64 n = n.next
65 print("\n")
66# Create a new Doubly Linked List
67NewDoublyLinkedList = doublyLinkedList()
68# Insert the element to empty list
69NewDoublyLinkedList.InsertToEmptyList(10)
70# Insert the element at the end
71NewDoublyLinkedList.InsertToEnd(20)
72NewDoublyLinkedList.InsertToEnd(30)
73NewDoublyLinkedList.InsertToEnd(40)
74NewDoublyLinkedList.InsertToEnd(50)
75NewDoublyLinkedList.InsertToEnd(60)
76# Display Data
77NewDoublyLinkedList.Display()
78# Delete elements from start
79NewDoublyLinkedList.DeleteAtStart()
80# Delete elements from end
81NewDoublyLinkedList.DeleteAtStart()
82# Display Data
83NewDoublyLinkedList.Display()
1class Node:
2 def __init__(self, data = None, next_node = None):
3 self.data = data
4 self.nextNode = next_node
5
6 def get_data(self):
7 return self.data
8
9 def set_data(self, data):
10 self.data = data
11
12 def get_nextNode(self):
13 return self.nextNode
14
15 def set_nextNode(self, nextNode):
16 self.nextNode = nextNode
17
18
19class LinkedList:
20 def __init__(self, head = None):
21 self.head = head
22
23
24 def add_Node(self, data):
25 # if empty
26 if self.head == None:
27 self.head = Node(data)
28
29
30 # not empty
31 else:
32 curr_Node = self.head
33
34 # if node added is at the start
35 if data < curr_Node.get_data():
36 self.head = Node(data, curr_Node)
37
38 # not at start
39 else:
40 while data > curr_Node.get_data() and curr_Node.get_nextNode() != None:
41 prev_Node = curr_Node
42 curr_Node = curr_Node.get_nextNode()
43
44 # if node added is at the middle
45 if data < curr_Node.get_data():
46 prev_Node.set_nextNode(Node(data, curr_Node))
47
48
49 # if node added is at the last
50 elif data > curr_Node.get_data() and curr_Node.get_nextNode() == None:
51 curr_Node.set_nextNode(Node(data))
52
53
54
55 def search(self, data):
56 curr_Node = self.head
57 while curr_Node != None:
58 if data == curr_Node.get_data():
59 return True
60
61 else:
62 curr_Node = curr_Node.get_nextNode()
63
64 return False
65
66
67 def delete_Node(self, data):
68 if self.search(data):
69 # if data is found
70
71 curr_Node = self.head
72 #if node to be deleted is the first node
73 if curr_Node.get_data() == data:
74 self.head = curr_Node.get_nextNode()
75
76 else:
77 while curr_Node.get_data() != data:
78 prev_Node = curr_Node
79 curr_Node = curr_Node.get_nextNode()
80
81 #node to be deleted is middle
82 if curr_Node.get_nextNode() != None:
83 prev_Node.set_nextNode(curr_Node.get_nextNode())
84
85 # node to be deleted is at the end
86 elif curr_Node.get_nextNode() == None:
87 prev_Node.set_nextNode(None)
88
89 else:
90 return "Not found."
91
92 def return_as_lst(self):
93 lst = []
94 curr_Node = self.head
95 while curr_Node != None:
96 lst.append(curr_Node.get_data())
97 curr_Node = curr_Node.get_nextNode()
98
99 return lst
100
101 def size(self):
102 curr_Node = self.head
103 count = 0
104 while curr_Node:
105 count += 1
106 curr_Node = curr_Node.get_nextNode()
107 return count
108
109
110## TEST CASES #
111test1 = LinkedList()
112test2 = LinkedList()
113test1.add_Node(20)
114test1.add_Node(15)
115test1.add_Node(13)
116test1.add_Node(14)
117test1.delete_Node(17)
118print(test1.return_as_lst())
119print(test2.size())
1class doublelinkedlist{
2 Node head;
3 Node tail;
4
5 static class Node{
6 int data;
7 Node previous;
8 Node next;
9
10 Node(int data) { this.data = data; }
11 }
12
13 public void addLink(int data) {
14 Node node = new Node(data);
15
16 if(head == null) {
17 head = tail = node;
18 head.previous = null;
19 } else {
20 tail.next = node;
21 node.previous = tail;
22 tail = node;
23 }
24 tail.next = null;
25 }
26}
1class ListNode:
2 def __init__(self, value, prev=None, next=None):
3 self.prev = prev
4 self.value = value
5 self.next = next
6
7class DoublyLinkedList:
8 def __init__(self, node=None):
9 self.head = node
10 self.tail = node
11 self.length = 1 if node is not None else 0
12
13 def __len__(self):
14 return self.length
15
16 def add_to_head(self, value):
17 new_node = ListNode(value, None, None)
18 self.length += 1
19 if not self.head and not self.tail:
20 self.head = new_node
21 self.tail = new_node
22 else:
23 new_node.next = self.head
24 self.head.prev = new_node
25 self.head = new_node
26
27 def remove_from_head(self):
28 value = self.head.value
29 self.delete(self.head)
30 return value
31
32 def add_to_tail(self, value):
33 new_node = ListNode(value, None, None)
34 self.length += 1
35 if not self.tail and not self.head:
36 self.tail = new_node
37 self.head = new_node
38 else:
39 new_node.prev = self.tail
40 self.tail.next = new_node
41 self.tail = new_node
42
43
44 def remove_from_tail(self):
45 value = self.tail.value
46 self.delete(self.tail)
47 return value
48
49 def move_to_front(self, node):
50 if node is self.head:
51 return
52 value = node.value
53 if node is self.tail:
54 self.remove_from_tail()
55 else:
56 node.delete()
57 self.length -= 1
58 self.add_to_head(value)
59
60 def move_to_end(self, node):
61 if node is self.tail:
62 return
63 value = node.value
64 if node is self.head:
65 self.remove_from_head()
66 self.add_to_tail(value)
67 else:
68 node.delete()
69 self.length -= 1
70 self.add_to_tail(value)
71
72 def delete(self, node):
73 self.length -= 1
74 if not self.head and not self.tail:
75 return
76 if self.head == self.tail:
77 self.head = None
78 self.tail = None
79 elif self.head == node:
80 self.head = node.next
81 node.delete()
82 elif self.tail == node:
83 self.tail = node.prev
84 node.delete()
85 else:
86 node.delete()
87
88 def get_max(self):
89 if not self.head:
90 return None
91 max_val = self.head.value
92 current = self.head
93 while current:
94 if current.value > max_val:
95 max_val = current.value
96 current = current.next
97 return max_val