-
Notifications
You must be signed in to change notification settings - Fork 13
/
solution.go
95 lines (86 loc) · 3.75 KB
/
solution.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
91
92
93
94
95
// Define a structure for the circular deque
type MyCircularDeque struct {
deque []int // The deque implemented as a slice (array) of integers
front, rear, size int // front: index pointing to the front element, rear: index pointing to the rear element, size: total capacity of the deque
}
// Constructor initializes the deque with a size of k + 1 (one extra space to differentiate between full and empty states)
func Constructor(k int) MyCircularDeque {
return MyCircularDeque{
deque: make([]int, k + 1), // Create an array with size k + 1
front: 0, // Initialize front pointer to 0
rear: 0, // Initialize rear pointer to 0
size: k + 1, // Set the total size to k + 1
}
}
// InsertFront inserts an element at the front of the deque
func (this *MyCircularDeque) InsertFront(value int) bool {
// Check if the deque is full
if this.IsFull() {
return false // Return false if full, cannot insert
}
// Move front pointer backwards (circularly) by adjusting it within bounds
this.front = (this.front - 1 + this.size) % this.size
// Insert the value at the new front index
this.deque[this.front] = value
return true // Return true as insertion is successful
}
// InsertLast inserts an element at the rear of the deque
func (this *MyCircularDeque) InsertLast(value int) bool {
// Check if the deque is full
if this.IsFull() {
return false // Return false if full, cannot insert
}
// Insert the value at the current rear index
this.deque[this.rear] = value
// Move rear pointer forward (circularly) to the next available position
this.rear = (this.rear + 1) % this.size
return true // Return true as insertion is successful
}
// DeleteFront removes an element from the front of the deque
func (this *MyCircularDeque) DeleteFront() bool {
// Check if the deque is empty
if this.IsEmpty() {
return false // Return false if empty, nothing to delete
}
// Move front pointer forward (circularly) to remove the current front element
this.front = (this.front + 1) % this.size
return true // Return true as deletion is successful
}
// DeleteLast removes an element from the rear of the deque
func (this *MyCircularDeque) DeleteLast() bool {
// Check if the deque is empty
if this.IsEmpty() {
return false // Return false if empty, nothing to delete
}
// Move rear pointer backward (circularly) to remove the current rear element
this.rear = (this.rear - 1 + this.size) % this.size
return true // Return true as deletion is successful
}
// GetFront returns the front element of the deque without removing it
func (this *MyCircularDeque) GetFront() int {
// Check if the deque is empty
if this.IsEmpty() {
return -1 // Return -1 if empty, no front element exists
}
// Return the element at the front index
return this.deque[this.front]
}
// GetRear returns the rear element of the deque without removing it
func (this *MyCircularDeque) GetRear() int {
// Check if the deque is empty
if this.IsEmpty() {
return -1 // Return -1 if empty, no rear element exists
}
// Return the element at the index before the rear pointer (circularly adjusted)
return this.deque[(this.rear - 1 + this.size) % this.size]
}
// IsEmpty checks whether the deque is empty
func (this *MyCircularDeque) IsEmpty() bool {
// The deque is empty if the front and rear pointers are at the same position
return this.front == this.rear
}
// IsFull checks whether the deque is full
func (this *MyCircularDeque) IsFull() bool {
// The deque is full if the next position of the rear pointer equals the front pointer
return (this.rear + 1) % this.size == this.front
}