-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
48 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
--- | ||
tags: | ||
- golang | ||
--- | ||
|
||
# `container` | ||
|
||
- [heap](https://pkg.go.dev/container/heap) provides heap operations for any type that implements heap.Interface. | ||
- [list](https://pkg.go.dev/container/list) implements a doubly linked list. | ||
- [ring](https://pkg.go.dev/container/ring) implements operations on circular lists. | ||
|
||
## `container/list` | ||
|
||
### Construct a Double-Linked List | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"container/list" | ||
"fmt" | ||
) | ||
|
||
func insertListElements(n int) *list.List { // add elements in list from 1 to n | ||
lst := list.New() | ||
for i := 1; i <= n; i++ { | ||
lst.PushBack(i) // insertion here | ||
} | ||
return lst | ||
} | ||
|
||
func main() { | ||
n := 5 | ||
myList := insertListElements(n) | ||
for e := myList.Front(); e != nil; e = e.Next() { | ||
fmt.Println(e.Value) | ||
} | ||
} | ||
``` | ||
|
||
??? example "Output" | ||
``` | ||
1 | ||
2 | ||
3 | ||
4 | ||
5 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters