-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchallenge.go
90 lines (78 loc) · 1.89 KB
/
challenge.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"fmt"
"errors"
)
type Stack struct{
Flights []Flight
}
type Flight struct {
Origin string
Destination string
Price int
}
func (s *Stack) Pop() (Flight,error) {
if s.IsEmpty() {
return Flight{}, errors.New("Stack is Empty")
} else {
lastFlight := s.Flights[len(s.Flights)-1]
s.Flights = s.Flights[:len(s.Flights)-1]
return lastFlight, nil
}
}
func (s *Stack) Push(f Flight) {
s.Flights = append(s.Flights,f)
}
func (s *Stack) Peek() (Flight,error) {
if s.IsEmpty() {
return Flight{}, errors.New("Stack is Empty")
} else {
return s.Flights[len(s.Flights)-1], nil
}
}
func (s *Stack) IsEmpty() bool {
return len(s.Flights) == 0
}
func main() {
flight1 := Flight{Origin: "Pune", Destination: "Nashik", Price: 120,}
flight2 := Flight{Origin: "Goa", Destination: "Mumbai", Price: 150,}
stack := Stack{}
// To check all methods when stack is empty
fmt.Println("stack",stack)
fmt.Println()
fmt.Println("stack.IsEmpty()",stack.IsEmpty())
fmt.Println()
fmt.Printf("stack.Peek() ")
fmt.Println(stack.Peek())
fmt.Println()
fmt.Printf("stack.Pop() ")
fmt.Println(stack.Pop())
fmt.Println()
// To check all methods when we add flight in the stack
stack.Push(flight1)
fmt.Println("stack",stack)
fmt.Println()
fmt.Println("stack.IsEmpty()",stack.IsEmpty())
fmt.Println()
fmt.Printf("stack.Peek() ")
fmt.Println(stack.Peek())
fmt.Println()
fmt.Printf("stack.Pop() ")
fmt.Println(stack.Pop())
fmt.Println()
fmt.Println("stack",stack)
fmt.Println()
fmt.Printf("stack.Pop() ")
fmt.Println(stack.Pop())
fmt.Println()
// To check all methods when we add multiple flight in the stack
stack.Push(flight1)
stack.Push(flight2)
fmt.Println("stack",stack)
fmt.Println()
fmt.Println("stack.IsEmpty()",stack.IsEmpty())
fmt.Println()
fmt.Printf("stack.Peek() ")
fmt.Println(stack.Peek())
fmt.Println()
}