all tasks
This commit is contained in:
parent
b83f433714
commit
68f8f3e56b
14735 changed files with 178959 additions and 0 deletions
1
Task/Stack/Go/stack-1.go
Normal file
1
Task/Stack/Go/stack-1.go
Normal file
|
|
@ -0,0 +1 @@
|
|||
var intStack []int
|
||||
1
Task/Stack/Go/stack-2.go
Normal file
1
Task/Stack/Go/stack-2.go
Normal file
|
|
@ -0,0 +1 @@
|
|||
intStack = append(intStack, 7)
|
||||
1
Task/Stack/Go/stack-3.go
Normal file
1
Task/Stack/Go/stack-3.go
Normal file
|
|
@ -0,0 +1 @@
|
|||
popped, intStack = intStack[len(intStack)-1], intStack[:len(intStack)-1]
|
||||
1
Task/Stack/Go/stack-4.go
Normal file
1
Task/Stack/Go/stack-4.go
Normal file
|
|
@ -0,0 +1 @@
|
|||
len(intStack) == 0
|
||||
1
Task/Stack/Go/stack-5.go
Normal file
1
Task/Stack/Go/stack-5.go
Normal file
|
|
@ -0,0 +1 @@
|
|||
intStack[len(intStack)-1]
|
||||
53
Task/Stack/Go/stack-6.go
Normal file
53
Task/Stack/Go/stack-6.go
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type stack []interface{}
|
||||
|
||||
func (k *stack) push(s interface{}) {
|
||||
*k = append(*k, s)
|
||||
}
|
||||
|
||||
func (k *stack) pop() (s interface{}, ok bool) {
|
||||
if k.empty() {
|
||||
return
|
||||
}
|
||||
last := len(*k) - 1
|
||||
s = (*k)[last]
|
||||
*k = (*k)[:last]
|
||||
return s, true
|
||||
}
|
||||
|
||||
func (k *stack) peek() (s interface{}, ok bool) {
|
||||
if k.empty() {
|
||||
return
|
||||
}
|
||||
last := len(*k) - 1
|
||||
s = (*k)[last]
|
||||
return s, true
|
||||
}
|
||||
|
||||
func (k *stack) empty() bool {
|
||||
return len(*k) == 0
|
||||
}
|
||||
|
||||
func main() {
|
||||
var s stack
|
||||
fmt.Println("new stack:", s)
|
||||
fmt.Println("empty?", s.empty())
|
||||
s.push(3)
|
||||
fmt.Println("push 3. stack:", s)
|
||||
fmt.Println("empty?", s.empty())
|
||||
s.push("four")
|
||||
fmt.Println(`push "four" stack:`, s)
|
||||
if top, ok := s.peek(); ok {
|
||||
fmt.Println("top value:", top)
|
||||
} else {
|
||||
fmt.Println("nothing on stack")
|
||||
}
|
||||
if popped, ok := s.pop(); ok {
|
||||
fmt.Println(popped, "popped. stack:", s)
|
||||
} else {
|
||||
fmt.Println("nothing to pop")
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue