Golang Program to Insert a Node in a Sorted Doubly Linked List


A doubly linked list could be an information structure where each node contains a reference to both its previous and following nodes. The program points to preserve the sorted arrangement of the linked list whereas inserting an unused node. In this article, we will learn to create a Golang program that focuses on inserting a node into a sorted doubly linked list. Here we are going to use the method insertNode along with examples to elaborate on the concept.

Syntax

func insertNode(head *Node, value int) *Node

This Syntax represents a function named "insertNode" that takes a pointer to the head node of a doubly linked list and an integer value as parameters. It will insert a new node with the given value into the list and return the updated head node.

head.prev

head represent a node that is a starting point of a list., the prev represent the last node of the list.

current = next.prev

This is used to update the current node by moving to its previous node.

current.value

This represents the value stored in the current node of the list.

Algorithm

  • Create a new node with the given data that needs to be inserted.

  • If the doubly linked list is empty, make the new node the head of the list and return it.

  • Traverse the doubly linked list starting from the head until you find the appropriate position to insert the new node.

  • Compare the data of the current node with the data of the new node to determine the correct position.

  • Once the correct position is found, update the pointers of the previous and next nodes to include the new node.

  • Adjust the pointers of the new node to connect it with the previous and next nodes in the doubly linked list.

  • Return the updated doubly linked list with the new node inserted at the appropriate position

Example

In this example, we have implemented a method called insertNode for inserting a node in a sorted doubly linked list. The method takes the head of the list and the value of the node to be inserted as input parameters. In the main function, we create a sample doubly linked list with values 10, 20, and 30. We print the original list, then call the insertNode method to insert a node with a value of 25. Finally, we print the updated list after the insertion. This code demonstrates how the insertNode method can be used to insert a node in a sorted doubly linked list while maintaining the sorted order.

package main

import "fmt"

type Node struct {
   value      int
   prev, next *Node
}

func insertNode(head *Node, value int) *Node {
   newNode := &Node{value: value}

   if head == nil {
      return newNode
   }

   if value < head.value {
      newNode.next = head
      head.prev = newNode
      return newNode
   }

   current := head
   for current.next != nil && current.next.value < value {
      current = current.next
   }

   newNode.next = current.next
   if current.next != nil {
      current.next.prev = newNode
   }
   current.next = newNode
   newNode.prev = current

   return head
}

func printList(head *Node) {
   current := head
   for current != nil {
      fmt.Printf("%d ", current.value)
      current = current.next
   }
   fmt.Println()
}

func main() {
   head := &Node{value: 10}
   node1 := &Node{value: 20}
   node2 := &Node{value: 30}

   head.next = node1
   node1.prev = head
   node1.next = node2
   node2.prev = node1

   fmt.Println("Original List:")
   printList(head)

   head = insertNode(head, 25)

   fmt.Println("Updated List:")
   printList(head)
}

Output

Original List:
10 20 30
Updated List:
10 20 25 30

Conclusion

In the Conclusion, we discussed The Golang program for inserting a node in a sorted doubly linked list permits the user to preserve the sorted arrangement of the list whereas inserting unused nodes. By navigating the list and altering the pointers, the program validates that the unused node is inserted at the proper position. This calculation gives a proficient way to oversee sorted doubly connected nodes in Golang.

Updated on: 20-Jul-2023

103 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements