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:
- Import the
container/list
andfmt
packages. - Inside the
main
function, create a new doubly linked list usinglist.New()
. - 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. - Iterate over the list using a loop, starting from the front (
l.Front()
) and moving to the next element usinge.Next()
. Print the value of each element usingfmt.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 ornil
if it is the last element.Prev() *Element
: Returns the previous list element ornil
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://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/