doubly linked list python

Solutions on MaxInterview for doubly linked list python by the best coders in the world

showing results for - "doubly linked list python"
Trace
24 Feb 2018
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()
Linus
14 Sep 2016
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
queries leading to this page
python doubly link listinsertion all in doubly linked list in c 2b 2bdouble linked listlinked list pythondoubly linked list in data structure in pythoncreate doubly linked listhow to avoid making a doubly linked listinsert into doubly linked listprogram to implement doubly linked list in python doubly linked list c 2b 2bdoubly linked list class javainsert iterm using double linked listpython doubly linked list libraryinsert function doubly linked list c 2b 2bdoubly linked list implementation in pythonprogram for creation insertion and deletion in doubly linked listpython linked listhow to diagram a doubly linked list pythondoubly linked list importlinked and doubly linked listhow to set the head of a doubly linked list pythondoubly linkedlist pythondoubly linked list using pythonlinked list inpythonhow to get the value at the end of a doubly linked listdoubly linked list python example simplecreate a double linked list 2c wherein the data elements are either multiple of 5 or 7 and less than 99 hence write a code snippet to implement this linked listbuilt doubly linked list pythondoubly linked list in data structure pythondoubly linked list implementation easier 3fhow many null pointers a doubly linked list can have at minimum 3fadd to doubly linked listhow to insert data in doubly linked listdoubly linked list in pythondoubly linked list class implementation create a double linked list of size n where the information part of each node contain an integercircular linked list cpphow to create a linked list in pythonimplementation of doubly linked implementationdoubly linked list in python 3double link list pythondll traversal function from the endadd to head on a doubly linked list in pythonimplementation of doubly linked listpython doubly linked listdoubly link list in pythonimplementing doubly linked linked list add methodat the frint doublylinked listnode class of doubly linked listdoubly linked list python codepython double linked listlinked lists in pythoninsert item into double linked listpython doubly linked list packageimplement a doubly linked listadd a node double linked list in pythondouble linked listsdraw double linked list pythondoubly linked list struct inserting three vsaluesdoubly linked nulllinkedlist pythonpython program for doubly linked listdouble linked list python rear insertc 2b 2b linked listdoubly linked list all operations in pythoninsertion in doubly linked list in c 2b 2bdoubly linked list pythondoubly linked list python favtutorpython code for doubly linked listdobly linked list in python show to make a linked list in pythondouble linked list in pythondobuly linked list in pythoninsertion into doubly linked listimplementation of a doubly linked list in pythonimplementation for a doubly linked listlinked list program in pythondevelop a program to implemetn following operations on a doubly linked list 3a i 29 insert before a given valuelinkedin list in pythontwo way linked listdoubly linked list python mediumlinked list i pythonpython program to implement doubly linked listdoubly linked list insert python implementationwhat is doubly linked list in pythondoubly linked lists in pythona simple doubly linked liststructure for double linked listcreate a doubly linked list in pythondouble linked list pyhtonpython doubly linked list implementationdoes python have linked listdoubly linked list operations program in pythondoubly linked list in python in pythonappend doubly linked listlinked list pythonhow to code doubly linked listpython make a doubly linked listimplementing doubly linked list in pythondoubly linked list input restrectionadding to double linked listdoubly linked linked listhow to create a doubly linked list in pythondoubly linked list adding node at defined positionimplement a doubly linked list in c 2b 2bsethead double llinkedlistcreate doubly linked list in pythonpython data structures doubleadding a node to the front of a doubly linked list javafind doubly linked list pythonadd a node before p in doubly linked listpython linked list gfgdoubly linked list python implementationtwo way linked list in ctraversing over doubly linked listtwo way linked list diagramdouble linked list codedouble linked list implement adoubly linked lists pythondoublly linked listdoubly linked list python libraryimplement doubly linked list in pythondoubly linked list in c 2b 2bsimple program to implement doubly linked list in python in doubly linked 2c null value is indoubly linked list in data structure using pythondouble linked list pythondoubly linked list java implementationdoubly linked list in javadouply linked list in pythondoubly linked list python gfgdoubly linked list python