Menu Close

Go – PushFront() function in list package in go golang

In this tutorial, we are going to learn about PushFront() function in list package in go golang. PushFront() function is used to insert a new element at the front of list in go golang.

Introduction

Go, or Golang, is a powerful and efficient programming language designed for concurrent and networked programming. It has an extensive standard library, which provides various packages for different tasks. One such package is the container/list package, which implements a doubly-linked list.

Overview of the PushFront Function

The PushFront function is a method of the list.List type. It is used to insert a new element at the front of the list. The function returns a pointer to the newly added element, which is of the list.Element type.

Function proto type:

func (l *List) PushFront(value interface{}) *Element 

Parameters:

  • value: The value to be stored in the new element of the list.

Return Value:

  • A pointer to the newly created Element in the list.

The PushFront method is convenient when you need to maintain a list where new elements are added to the front, such as in certain types of caching algorithms or when implementing a stack data structure with a linked list.

Example Usage of PushFront:

    list_1 = 1 -> 2 -> 3 -> 4
    
    list_1.PushFront(5)
    
    list_1 = 5 -> 1 -> 2 -> 3 -> 4
    package main
    
    import (
    	"container/list"
    	"fmt"
    )
    
    func main() {
    	// Create a new doubly linked list
    	l := list.New()
    
    	// Add elements to the front of the list using PushFront
    	l.PushFront(1)
    	l.PushFront(2)
    	l.PushFront(3)
    
    	// Iterate over the list and print the elements
    	for e := l.Front(); e != nil; e = e.Next() {
    		fmt.Println(e.Value)
    	}
    }
    

    Output:

    3
    2
    1

    As you can see, the elements are added to the front of the list in reverse order.

    Explanation:

    1. Import the container/list and fmt packages.
    2. Inside the main function, create a new doubly linked list using list.New().
    3. Use the PushFront method to insert elements (1, 2, and 3) at the front of the list. The final list will have the elements in the following order: 3 -> 2 -> 1.
    4. Iterate over the list using a loop, starting from the front (l.Front()) and moving to the next element using e.Next(). Print the value of each element using fmt.Println.

    Understanding the Element Type

    The list.Element type represents an element in the doubly-linked list. It contains pointers to the next and previous elements in the list, as well as the element’s value.

    Here are the fields of the list.Element type:

    • Next() *Element: Returns the next list element or nil if it is the last element.
    • Prev() *Element: Returns the previous list element or nil if it is the first element.
    • Value interface{}: Holds the element’s value.

    Conclusion

    The PushFront function in Go’s container/list package is a helpful method for inserting elements at the beginning of a doubly linked list. It allows you to manage your list efficiently and maintain the desired order of elements. The container/list package offers a range of functions to manipulate and interact with doubly linked lists, making it a valuable resource for various applications and data structure implementations in Go.

    To learn more about golang, Please refer given below link:

    https://www.techieindoor.com/go-lang-tutorial/
    https://www.techieindoor.com/go-lang-tutorial/

    References:

    https://golang.org/doc/
    https://golang.org/pkg/
    https://golang.org/pkg/fmt/
    https://golang.org/pkg/fmt/#Println
    https://golang.org/pkg/container/list/
    Posted in golang, list package

    Leave a Reply

    Your email address will not be published. Required fields are marked *