doubly linked list

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

showing results for - "doubly linked list"
Jana
30 Sep 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()
84
queries leading to this page
doubly linkedl listwhen to use doubly linked listdoubly linked circular listdoubly linked lisdoubly linked listthe use of doubly linked listlinked list doubly linked listdoubly linked list graphcreate doubly linked listdoubly linked vs singly linked listdoubly linked list c doubly singly linked listdiubly linked listwhy we use doubly linked listimplement doubly linked listwhen to use a doubly linked listdoubly linked list exampledoubly linked list applicationsdoubly linked list implementationdoubly linked list applicationdoublyy link listdoubly linked circular linked listdoubly linked list diagramdoubly linked listdoubly linkedlist in java inbuiltwhy doubly linked list is usedwhat are doubly linked listdoubly linked listin javadoubly linked list in schemedoubly lined listworking of doubly linked listdoubly linked list to create and displaywhat is doubly linked listdoubly linked list stldoubly linked list circularwhich is false about doubly linked listdoublly linked listdoubly linked list stldoubly linked list v linked listwhat is false about doubly linked listimplement a doubly linked listdoubly linked listcreate a doubly linked listdoubly linked list and singly linked listdoubly linked list headdobuly linked listdoubly linked list explainedwhat is a doubly linked listdoubly linked list codeuses of doubly linked listdoubly linkedi listdoubly linked list onlinedoubly linked list usesuse of doubly linked listdoubly linked list using linkedlist javawhy do we require doubly linked listdoubel linked listcircularly doubly linked listdoubly linked list