From 130d615e88b1d6d951263570828860fc1b88da32 Mon Sep 17 00:00:00 2001 From: Orest Ivasiv Date: Mon, 16 Oct 2023 12:56:50 +0200 Subject: [PATCH] added container.md --- docs/wiki/Lang-Go/Cookbook/container.md | 48 +++++++++++++++++++++++++ docs/wiki/misc/time-complexities.md | 36 ------------------- 2 files changed, 48 insertions(+), 36 deletions(-) create mode 100644 docs/wiki/Lang-Go/Cookbook/container.md diff --git a/docs/wiki/Lang-Go/Cookbook/container.md b/docs/wiki/Lang-Go/Cookbook/container.md new file mode 100644 index 0000000..0aef5f4 --- /dev/null +++ b/docs/wiki/Lang-Go/Cookbook/container.md @@ -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 + ``` diff --git a/docs/wiki/misc/time-complexities.md b/docs/wiki/misc/time-complexities.md index b06bd9d..986e88a 100644 --- a/docs/wiki/misc/time-complexities.md +++ b/docs/wiki/misc/time-complexities.md @@ -251,39 +251,3 @@ func main() { fmt.Println("N = 100, Number of instructions O(n) ::", fun10(100)) } ``` - -## 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 - ``` \ No newline at end of file