Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Double-Linked List Python

# node structure
class Node:
  def __init__(self, data):
    self.data = data
    self.next = None
    self.prev = None

#class Linked List
class LinkedList:
  def __init__(self):
    self.head = None

  #Add new element at the start of the list
  def push_front(self, newElement):
    newNode = Node(newElement)
    if(self.head == None):
      self.head = newNode
      return
    else:
      self.head.prev = newNode
      newNode.next = self.head
      self.head = newNode

  #display the content of the list
  def PrintList(self):
    temp = self.head
    if(temp != None):
      print("The list contains:", end=" ")
      while (temp != None):
        print(temp.data, end=" ")
        temp = temp.next
      print()
    else:
      print("The list is empty.")

# test the code                  
MyList = LinkedList()

#Add three elements at the start of the list.
MyList.push_front(10)
MyList.push_front(20)
MyList.push_front(30)
MyList.PrintList()
Comment

double linked list python

Doubly Linked List carry a link element that is called first and last. Every link carries a data field(s) and two link fields called next and prev. Each link is linked with its next link using its next link. Each link is linked with its previous link using its previous link.
Comment

PREVIOUS NEXT
Code Example
Python :: django admin.py all fields 
Python :: convert all items in list to string python 
Python :: python numpy array change axis 
Python :: how to get month name from date in pandas 
Python :: apply same shuffle to two arrays numpy 
Python :: discord.py how to print audit logs 
Python :: making lists with loops in one line python 
Python :: delete dictionary key python 
Python :: py mean 
Python :: get name of a file in python 
Python :: how to file in python 
Python :: string formatting in python 
Python :: how to make an ai 
Python :: plot using matplotlib 
Python :: print variable name 
Python :: default flask app 
Python :: loop through list of dictionaries python 
Python :: reportlab python draw line 
Python :: linspace python 
Python :: python ordereddict reverse 
Python :: pandas profile report python 
Python :: how to add for loop in python 
Python :: dict typing python 
Python :: python private 
Python :: How to store password in hashlib in python 
Python :: how to iterate over columns of pandas dataframe 
Python :: how to send file using socket in python 
Python :: split word python 
Python :: pathlib path get filename with extension 
Python :: newsapi in python 
ADD CONTENT
Topic
Content
Source link
Name
9+7 =