doubly linked list python

Full implementation of Doubly Linked List:

https://github.com/shreyasvedpathak/Data-Structure-Python/tree/master/LinkedList

3
1
Ryan Tenney 110 points

                                    class ListNode:
    def __init__(self, value, prev=None, next=None):
        self.prev = prev
        self.value = value
        self.next = next

class DoublyLinkedList:
    def __init__(self, node=None):
        self.head = node
        self.tail = node
        self.length = 1 if node is not None else 0

    def __len__(self):
        return self.length
   
  	def add_to_head(self, value):
        new_node = ListNode(value, None, None)
        self.length += 1
        if not self.head and not self.tail:
            self.head = new_node
            self.tail = new_node
        else:
            new_node.next = self.head
            self.head.prev = new_node
            self.head = new_node
 
    def remove_from_head(self):
        value = self.head.value
        self.delete(self.head)
        return value

    def add_to_tail(self, value):
        new_node = ListNode(value, None, None)
        self.length += 1
        if not self.tail and not self.head:
            self.tail = new_node
            self.head = new_node
        else:
            new_node.prev = self.tail
            self.tail.next = new_node
            self.tail = new_node
            

    def remove_from_tail(self):
        value = self.tail.value
        self.delete(self.tail)
        return value
            
    def move_to_front(self, node):
        if node is self.head:
            return
        value = node.value
        if node is self.tail:
            self.remove_from_tail()
        else:
            node.delete()
            self.length -= 1
        self.add_to_head(value)
        
    def move_to_end(self, node):
        if node is self.tail:
            return
        value = node.value
        if node is self.head:
            self.remove_from_head()
            self.add_to_tail(value)
        else:
            node.delete()
            self.length -= 1
            self.add_to_tail(value)

    def delete(self, node):
        self.length -= 1
        if not self.head and not self.tail:
            return
        if self.head == self.tail:
            self.head = None
            self.tail = None
        elif self.head == node:
            self.head = node.next
            node.delete()
        elif self.tail == node:
            self.tail = node.prev
            node.delete()
        else:
            node.delete()

    def get_max(self):
        if not self.head:
            return None
        max_val = self.head.value
        current = self.head
        while current:
            if current.value > max_val:
                max_val = current.value
            current = current.next
        return max_val

3 (1 Votes)
0
4.11
9

                                    # Initialise the Node
class Node:
    def __init__(self, data):
        self.item = data
        self.next = None
        self.prev = None
# Class for doubly Linked List
class doublyLinkedList:
    def __init__(self):
        self.start_node = None
    # Insert Element to Empty list
    def InsertToEmptyList(self, data):
        if self.start_node is None:
            new_node = Node(data)
            self.start_node = new_node
        else:
            print("The list is empty")
    # Insert element at the end
    def InsertToEnd(self, data):
        # Check if the list is empty
        if self.start_node is None:
            new_node = Node(data)
            self.start_node = new_node
            return
        n = self.start_node
        # Iterate till the next reaches NULL
        while n.next is not None:
            n = n.next
        new_node = Node(data)
        n.next = new_node
        new_node.prev = n
    # Delete the elements from the start
    def DeleteAtStart(self):
        if self.start_node is None:
            print("The Linked list is empty, no element to delete")
            return 
        if self.start_node.next is None:
            self.start_node = None
            return
        self.start_node = self.start_node.next
        self.start_prev = None;
    # Delete the elements from the end
    def delete_at_end(self):
        # Check if the List is empty
        if self.start_node is None:
            print("The Linked list is empty, no element to delete")
            return 
        if self.start_node.next is None:
            self.start_node = None
            return
        n = self.start_node
        while n.next is not None:
            n = n.next
        n.prev.next = None
    # Traversing and Displaying each element of the list
    def Display(self):
        if self.start_node is None:
            print("The list is empty")
            return
        else:
            n = self.start_node
            while n is not None:
                print("Element is: ", n.item)
                n = n.next
        print("\n")
# Create a new Doubly Linked List
NewDoublyLinkedList = doublyLinkedList()
# Insert the element to empty list
NewDoublyLinkedList.InsertToEmptyList(10)
# Insert the element at the end
NewDoublyLinkedList.InsertToEnd(20)
NewDoublyLinkedList.InsertToEnd(30)
NewDoublyLinkedList.InsertToEnd(40)
NewDoublyLinkedList.InsertToEnd(50)
NewDoublyLinkedList.InsertToEnd(60)
# Display Data
NewDoublyLinkedList.Display()
# Delete elements from start
NewDoublyLinkedList.DeleteAtStart()
# Delete elements from end
NewDoublyLinkedList.DeleteAtStart()
# Display Data
NewDoublyLinkedList.Display()

4.11 (9 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
python doubly linked list library doubly linked list all operations in python what is doubly linked list in python doubly linked list python gfg implement doubly linked list in python create a doubly linked list in python doubly linked list operations program in python doubly linked list python library implementing doubly linked list in python python doubly linked list package doubly linked list python medium doubly linked list python favtutor doubly linked list insert python implementation Doubly Linked List in Python in python doubly linked list in data structure in python doubly linked list python code create doubly linked list in python how to diagram a doubly linked list python dobuly linked list in python python doubly linked list implementation doubly linked list python implementation python make a doubly linked list python doubly link list built doubly linked list python python program for doubly linked list python code for doubly linked list implementation of doubly linked list how to get the value at the end of a doubly linked list doubly linked list struct inserting three vsalues adding to double linked list sethead double llinkedlist doubly linked list import implement a doubly linked list a simple doubly linked list insert into doubly linked list at the frint doublylinked list how to make a linked list in python structure for double linked list two way linked list in c two way linked list add a node double linked list in python Develop a program to implemetn following operations on a doubly linked list: i) insert before a given value doubly linked list adding node at defined position how to create a linked list in python does python have linked list python linked list gfg doubly linked list in java doubly linked list java implementation implement a doubly linked list in c++ insertion all in doubly linked list in c++ How many NULL pointers a doubly linked list can have at minimum? douply linked list in python doubly linked list in data structure python linked list python doubly linked null In doubly linked, Null value is in Create a double linked list, wherein the data elements are either multiple of 5 or 7 and less than 99. Hence write a code snippet to implement this linked list doubly linked list class implementation doubly linked list class java adding a node to the front of a doubly linked list java linked and doubly linked list python linked list dobly linked list in python s linked list. python linked lists in python doubly linked list implementation easier ? create doubly linked list double linked list create a double linked list of size n where the information part of each node contain an integer doubly linked list c++ insert function doubly linked list c++ insertion in doubly linked list in c++ doubly linked list in c++ two way linked list diagram doubly linked list input restrection double link list python doublly linked list doubly linked linked list how to insert data in doubly linked list linkedin list in python double linked list pyhton linked list inpython double linked list implement a linked list i python linked list program in python traversing over doubly linked list linkedlist python implementation for a doubly linked list python double linked list double linked lists insertion into doubly linked list add to doubly linked list how to avoid making a doubly linked list implementing doubly linked linked list add method how to set the head of a doubly linked list python how to code doubly linked list add a node before p in doubly linked list program for creation insertion and deletion in doubly linked list insert item into double linked list insert iterm using double linked list append doubly linked list double linked list in python implementation of doubly linked implementation double linked list code python data structures double doubly linkedlist python draw double linked list python doubly linked lists in python node class of doubly linked list doubly linked list implementation in python how to create a doubly linked list in python DLL traversal function from the end double linked list python rear insert doubly linked lists python doubly linked list using python doubly linked list in python 3 double linked list python add to head on a doubly linked list in python doubly linked list in data structure using python doubly linked list in python c++ linked list circular linked list cpp python doubly linked list doubly linked list python
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source