Friday, October 25, 2024

Doubly Linked List Python

 


# singly linked list

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

class dl:
  def __init__(self):
    self.head = None
    self.tail = None
 
  def is_empty(self):
    return self.head is None
 
  def insertb(self,value):
    new_node = Node(value)
    if self.is_empty():
      self.head = self.tail = new_node
    else:
      new_node.next = self.head
      self.head.prev = new_node
      self.head = new_node
 
  def inserte(self,value):
    new_node = Node(value)
    if self.is_empty():
      self.head = self.tail = new_node
    else:
      new_node.prev = self.tail
      self.tail.next = new_node
      self.tail = new_node
 
  def forward(self):
    current = self.head
    while current:
      if current.data != None:
        print(current.data,'<->',end='')
      current = current.next
    print()

  def backward(self):
    current = self.tail
    while current:
      if current.data != None:
        print(current.data,'<->',end='')
      current = current.prev
    print()

  def delete(self,value):
    current = self.head
    while current:
      if current.data != None and current.data == value:
        current.data = None
      current = current.next
    print("deleted...")
    self.forward()
    self.backward()
    self.size()
 
  def update(self,value,replace):
    current = self.head
    while current:
      if current.data != None and current.data == value:
        current.data = replace
      current = current.next
    print("updated...")
    self.forward()
    self.backward()
   
  def size(self):
    current = self.head
    size=0
    while current:
      if current.data != None :
        size += 1
      current = current.next
    print(f"size is {size}...!")
 
l = dl()
l.insertb(1)
l.insertb(2)
l.insertb(3)
l.inserte(4)
l.inserte(5)
l.delete(1)
l.update(2,22)

Singly linked list python

 


# singly linked list

class Node:
  def __init__(self,data):
    self.data = data
    self.next = None

class ll:
  def __init__(self):
    self.head = None
 
  def is_empty(self):
    return self.head is None
 
  def insert(self,value):
    new_node = Node(value)
    if self.is_empty():
      self.head = new_node
    else:
      new_node.next = self.head
      self.head = new_node
 
  def show(self):
    current = self.head
    while current:
      if current.data != None:
        print(current.data,'->',end='')
      current = current.next
    print()
 
  def delete(self,value):
    current = self.head
    while current:
      if current.data != None and current.data == value:
        current.data = None
      current = current.next
    print("deleted...")
    self.show()
    self.size()

# if you wish to delete the node and don't want none
    def delete(self,value):
        current = self.head
        while current:
            if current.data == value:
                previous.next = current.next  # Skip over the node to delete it
                current = None  # Optional: free the node
                print(f"Node with value {value} deleted.")
                self.display()
                return
            previous = current
            current = current.next
 
  def update(self,value,replace):
    current = self.head
    while current:
      if current.data != None and current.data == value:
        current.data = replace
      current = current.next
    print("updated...")
    self.show()
   
  def size(self):
    current = self.head
    size=0
    while current:
      if current.data != None :
        size += 1
      current = current.next
    print(f"size is {size}...!")
 
l = ll()
l.insert(1)
l.insert(2)
l.insert(3)
l.insert(4)
l.insert(5)

l.show()

BST python



class Tree:
  def __init__(self,data):
    self.data = data
    self.left = None
    self.right = None
 
  def is_empty(self):
    return self.data is None
 
  def insert(self,value):
    if value < self.data:
      if self.left is None :
        self.left = Tree(value)
      else:
        self.left.insert(value)
   
    if value > self.data :
      if self.right is None:
        self.right = Tree(value)
      else:
        self.right.insert(value)
       
  def show_left(self):
    current = self.left
    while current:
      if current.data != None:
        print(current.data)
      current = current.left
    print()
   
  def show_right(self):
    current = self.right
    while current:
      if current.data != None:
        print(current.data)
      current = current.right
    print()
   
def delete(root):
  value=-3
  if root:
    delete(root.left)
    delete(root.right)
    if root.data == value and root.data != None:
      root.data = None
     
     
def preorder(root):
  if root and root.data != None:
    print(root.data)
    preorder(root.left)
    preorder(root.right)

def postorder(root):
  if root and root.data != None:
    postorder(root.left)
    postorder(root.right)
    print(root.data)

def inorder(root):
  if root and root.data != None:
    inorder(root.left)
    print(root.data)
    inorder(root.right)
   
   
   
   
root = Tree(1)
root.insert(2)
root.insert(3)
root.insert(4)
root.insert(-1)
root.insert(-2)
root.insert(-3)
root.show_left()
root.show_right()
delete(root)
root.show_right()
root.show_left()
preorder(root)
postorder(root)
inorder(root)

virtual environment on python on Ubuntu

 # 1. Update system and install Python tools sudo apt update && sudo apt install python3-pip python3-venv -y # 2. Setup Django in a ...