DEV Community

Divyanshu Shekhar
Divyanshu Shekhar

Posted on • Updated on

Golang Linked List | Data Structure

Linked lists are an important data structure used in computer programming. They are commonly used to implement data structures such as stacks, queues, and graphs. In Golang, linked lists can be created using pointers and structures. This blog post will provide a comprehensive guide on how to create linked lists in Golang.

What is a Linked List?
Before diving into how to create linked lists in Golang, it is important to understand what linked lists are and how they work. A linked list is a linear data structure that consists of nodes linked together. Each node contains a value and a pointer to the next node in the list. The first node is called the head, and the last node is called the tail.

Types of Linked List

There are three types of linked lists:

  • Singly Linked List: In a singly linked list, each node contains a reference to the next node in the sequence. The last node points to null, indicating the end of the list.
  • Doubly Linked List: A doubly linked list is similar to a singly linked list, except that each node contains a reference to both the next and previous nodes in the sequence.
  • Circular Linked List: In a circular linked list, the last node points to the first node, creating a loop in the sequence.

Advantages of linked list

  • Dynamic Size: Linked lists can grow or shrink dynamically, making them suitable for situations where the size of the data structure is not known in advance.
  • Efficient Insertion and Deletion: Linked lists allow for efficient insertion and deletion of nodes at any point in the list.
  • Memory Efficient: Linked lists are memory-efficient because they only allocate memory for the nodes they need.

Creating Linked List in Golang

To create a linked list in Golang, we first need to define a structure that represents a node in the list. The node structure should contain two fields: a value field and a next field, which is a pointer to the next node in the list.

A pointer is a variable that stores the memory address of another variable. Golang’s pointer syntax is easy to use and helps in implementing linked lists.

Here is an example of a node struct in Golang:

type Node struct {
    value int
    next *Node
}
Enter fullscreen mode Exit fullscreen mode

In this example, the Node structure has two fields: value and next. The value field represents the value of the node, and the next field is a pointer to the next node in the list. Note that the next field is a pointer to another Node structure, which allows us to link nodes together.

Once we have defined the Node structure, we can create a linked list by defining a structure that represents the list itself. This structure should contain a field that represents the head of the list.

Here is an example of a linked list structure in Golang:

type LinkedList struct {
    head *Node
}
Enter fullscreen mode Exit fullscreen mode

In this example, the LinkedList structure has one field: head. The head field is a pointer to the first node in the list. If the list is empty, the head field should be nil.

Read the full blog from the original post: Golang Linked List: Manual & Container list package.

Find more blogs on Golang.

Top comments (2)

Collapse
 
lemenendez profile image
leonidas menendez • Edited

Displaying the list but leaving the head as it is

func (l LinkedList) Display() {
n := l.head
for n != nil {
fmt.Printf("%v-->", n.data)
n = n.next
}
}

Collapse
 
msimeonov profile image
martin simeonov