-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchallenge1.go
90 lines (78 loc) · 1.85 KB
/
challenge1.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 Queue struct{
Flights []Flight
}
type Flight struct {
Origin string
Destination string
Price int
}
func (q *Queue) Pop() (Flight,error) {
if q.IsEmpty() {
return Flight{}, errors.New("Queue is Empty")
} else {
firstFlight := q.Flights[0]
q.Flights = q.Flights[1:]
return firstFlight, nil
}
}
func (q *Queue) Push(f Flight) {
q.Flights = append(q.Flights,f)
}
func (q *Queue) Peek() (Flight,error) {
if q.IsEmpty() {
return Flight{}, errors.New("Queue is Empty")
} else {
return q.Flights[0], nil
}
}
func (q *Queue) IsEmpty() bool {
return len(q.Flights) == 0
}
func main() {
flight1 := Flight{Origin: "Pune", Destination: "Nashik", Price: 120,}
flight2 := Flight{Origin: "Goa", Destination: "Mumbai", Price: 150,}
queue := Queue{}
// To check all methods when queue is empty
fmt.Println("queue",queue)
fmt.Println()
fmt.Println("queue.IsEmpty()",queue.IsEmpty())
fmt.Println()
fmt.Printf("queue.Peek() ")
fmt.Println(queue.Peek())
fmt.Println()
fmt.Printf("queue.Pop() ")
fmt.Println(queue.Pop())
fmt.Println()
// To check all methods when we add flight in the queue
queue.Push(flight1)
fmt.Println("queue",queue)
fmt.Println()
fmt.Println("queue.IsEmpty()",queue.IsEmpty())
fmt.Println()
fmt.Printf("queue.Peek() ")
fmt.Println(queue.Peek())
fmt.Println()
fmt.Printf("queue.Pop() ")
fmt.Println(queue.Pop())
fmt.Println()
fmt.Println("queue",queue)
fmt.Println()
fmt.Printf("queue.Pop() ")
fmt.Println(queue.Pop())
fmt.Println()
// To check all methods when we add multiple flight in the queue
queue.Push(flight1)
queue.Push(flight2)
fmt.Println("queue",queue)
fmt.Println()
fmt.Println("queue.IsEmpty()",queue.IsEmpty())
fmt.Println()
fmt.Printf("queue.Peek() ")
fmt.Println(queue.Peek())
fmt.Println()
}