-
Notifications
You must be signed in to change notification settings - Fork 215
/
Copy pathstack_linked_list.go
74 lines (64 loc) · 1.36 KB
/
stack_linked_list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import "fmt"
type Stack struct {
top *ListNode
size int
}
type ListNode struct {
data interface{}
next *ListNode
}
// Size: Returns the size of Stack
func (s *Stack) Length() int {
return s.size
}
// Push: Pushes new [data] into Stack
// Time complexity O(1)
// Space complexity for n push operations O(n)
func (s *Stack) Push(data interface{}) {
s.top = &ListNode{data, s.top}
s.size++
fmt.Printf("\n%v Pushed to stack", data)
}
// IsEmpty: Returns true if Stack is empty or else false
// Time complexity O(1)
func (s *Stack) IsEmpty() bool {
return s.size == 0
}
// IsFull: Returns false since its LL based implementation
// Time complexity O(1)
func (s *Stack) IsFull() bool {
return false
}
// Pop: Pops top most data from Stack
// Time complexity O(1)
func (s *Stack) Pop() (data interface{}) {
if s.size > 0 {
data, s.top = s.top.data, s.top.next
s.size--
fmt.Printf("\n%v Popped from stack", data)
return data
}
return nil
}
// Peek: Returns top most element from Stack
// Time complexity O(1)
func (s *Stack) Peek() (data interface{}) {
if s.size > 0 {
data = s.top.data
fmt.Printf("\n%v is the topmost element in stack", data)
return data
}
return nil
}
func main() {
stack := new(Stack)
stack.Push("Hello")
stack.Push("World!")
stack.Push("Yo")
stack.Peek()
fmt.Println(stack.Length())
stack.Pop()
stack.Pop()
stack.Pop()
}