Skip to content

Crack SDE

Most of the content are generated by AI, with human being reviewed, edited, and revised

Menu
  • Home
  • Daily English Story
  • Tech Interviews
  • Cloud Native
  • DevOps
  • Artificial Intelligence
Menu

List library in golang

Posted on 11/10/202311/10/2023 by user

In Go (Golang), list.New() is used to create a new doubly linked list. The container/list package provides the implementation for this data structure. A doubly linked list allows you to store a collection of elements with efficient insertion and removal from both ends or even the middle of the list. Here are some examples of using list.New():

Example 1: Basic Usage

This example demonstrates creating a list, adding elements to it, and iterating over it.

package main

import (
    "container/list"
    "fmt"
)

func main() {
    // Create a new linked list
    linkedList := list.New()

    // Add elements to the list
    linkedList.PushBack(1) // Add to the end
    linkedList.PushFront(2) // Add to the front

    // Iterate over the list and print its elements
    for e := linkedList.Front(); e != nil; e = e.Next() {
        fmt.Println(e.Value)
    }
    // Output:
    // 2
    // 1
}

Example 2: Removing Elements

This example shows how to remove elements from a list.

package main

import (
    "container/list"
    "fmt"
)

func main() {
    // Create a new linked list and add elements
    linkedList := list.New()
    linkedList.PushBack("a")
    linkedList.PushBack("b")
    linkedList.PushBack("c")

    // Remove the second element ("b")
    for e := linkedList.Front(); e != nil; e = e.Next() {
        if e.Value == "b" {
            linkedList.Remove(e)
            break
        }
    }

    // Print the updated list
    for e := linkedList.Front(); e != nil; e = e.Next() {
        fmt.Println(e.Value)
    }
    // Output:
    // a
    // c
}

Example 3: Using a List to Implement a Queue or Stack

A linked list can be used to implement other data structures like a stack.

package main

import (
    "container/list"
    "fmt"
)

func main() {
    stack := list.New()

    // Push elements (like stack.push in other languages)
    stack.PushBack("world")
    stack.PushBack("hello")

    // Pop elements (like stack.pop)
    for stack.Len() > 0 {
        e := stack.Back() // Get the last element
        fmt.Println(e.Value)
        stack.Remove(e) // Remove the element
    }
    // Output:
    // hello
    // world
}

A linked list can be used to implement other data structures like a queue.

package main

import (
	"container/list"
	"fmt"
)

func main() {
	q := list.New()

	// Enqueue
	q.PushBack("apple")
	q.PushBack("banana")
	q.PushBack("orange")

	for q.Len() > 0 {
		// Dequeue
		x := q.Front()
		q.Remove(x)
		fmt.Println(x.Value)
	}
	// Output:
	// apple
	// banana
	// orange
}

In these examples, PushBack is used to add elements to the end of the list (which can also act like pushing onto a stack), and PushFront is used to add elements to the front of the list (useful for queues). The Remove method deletes an element from the list. You can traverse the list using Front() and Next() or Back() and Prev() for forward and backward iteration, respectively.

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related

22

Recent Posts

  • LC#622 Design Circular Queue
  • Started with OpenTelemetry in Go
  • How Prometheus scrap works, and how to find the target node and get the metrics files
  • How to collect metrics of container, pods, node and cluster in k8s?
  • LC#200 island problem

Recent Comments

  1. another user on A Journey of Resilience

Archives

  • May 2025
  • April 2025
  • February 2025
  • July 2024
  • April 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • June 2023
  • May 2023

Categories

  • Artificial Intelligence
  • Cloud Computing
  • Cloud Native
  • Daily English Story
  • Database
  • DevOps
  • Golang
  • Java
  • Leetcode
  • Startups
  • Tech Interviews
©2025 Crack SDE | Design: Newspaperly WordPress Theme
Manage Cookie Consent
To provide the best experiences, we use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Manage options Manage services Manage {vendor_count} vendors Read more about these purposes
View preferences
{title} {title} {title}