Skip to content

Instantly share code, notes, and snippets.

@DixieKorley
Last active June 1, 2023 17:43
Show Gist options
  • Save DixieKorley/ee8f55c4c695a2894ba8fbd6b0dd9a81 to your computer and use it in GitHub Desktop.
Save DixieKorley/ee8f55c4c695a2894ba8fbd6b0dd9a81 to your computer and use it in GitHub Desktop.
pseudocode for doubly linked list

Pseudocode

Node class

class Node
  **Initialize with input - value**
     value -> value
     prev -> null
     next -> null

Doubly linked list class

class DoublyLinkedList
  **Initialize with no input**
    head -> null
    taill -> null

   **Method to set node as head**
    *Input - node*
      if head is null
        head -> node
        tail -> node
        return
      (insert before node method with input head and node)

   **Method to set node as tail**
    *Input - node*
      if tail is null
        (set node as head method with input node)
      (insert after node method with input tail and node)

     
   **Method to insert node before**
    *Input - node before, node to insert*
      if node to insert = head and node to insert = tail
         return
      (remove node method with input node to insert)
      node to insert.prev -> node before.prev
      node to insert.next -> node before
      if node before.prev is null
         head -> node to insert
      else
         node before.prev.next -> node to insert
      node before.prev -> node to insert
    
    **Method to insert node after**
    *Input - node after, node to insert*
      if node to insert = head and node to insert = tail
         return
      (remove node method with input node to insert)
      node to insert.prev -> node after
      node to insert.next -> node after.next
      if node after.next is null
         tail -> node to insert
      else
         node after.next.prev -> node to insert
      node after.next -> node to insert
      
    **Method to insert at position**
      *Input - position, node to insert*
      if position = 1
          (set as head method with input node to insert)
          return
      node -> head
      curr_position -> 1
      while node is not null and curr_position != position
         node -> node.next
         curr_position += 1
      if node is not null
         (insert node before method with input node and node to insert)
      else
         (set as tail method with input node to insert) 
         
     **Method to remove a node with value**
       *Input - value*
       node -> head
       while node is not null
           node to remove -> node
           node -> node.next
           if node to remove.value = value
               (remove node method with input node to remove)    
               
    **Method to remove a node**
      *Input - node to remove*
      if node to remove = head
          head -> head.next
      if node to remove = tail
          tail -> tail.prev
      (remove node bonds method with input node to remove)
      
    **Method to check if linked list contains a node with value**
      *Input - value*
      node -> head
      while node is not null
          if node.value = value
              return True
          node -> node.next
      return False
      
      or 
      
      node -> head
      while node is not null and node.value != value
           node -> node.next
      return node is not null
      
     **Method to remove node bonds**
     *Input - node to remove*
     if node to remove.prev is not null
        node to remove.prev.next -> node.next
     if node to remove.next is not null
        node to remove.next.prev -> node.prev
     node to remove.prev -> null
     node to remove.next -> null
     
      
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment