Skip to content

Commit

Permalink
added container.md
Browse files Browse the repository at this point in the history
  • Loading branch information
halyph committed Oct 16, 2023
1 parent d5063f0 commit 130d615
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 36 deletions.
48 changes: 48 additions & 0 deletions docs/wiki/Lang-Go/Cookbook/container.md
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
```
36 changes: 0 additions & 36 deletions docs/wiki/misc/time-complexities.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

0 comments on commit 130d615

Please sign in to comment.