Created
October 24, 2021 08:04
-
-
Save codephilosopher/1a8667d2ae31999c259020b4ed6a26be to your computer and use it in GitHub Desktop.
a small gist of double eneded queue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
type Queue struct { | |
front int | |
rear int | |
size int | |
QArray []int | |
} | |
func (q *Queue) initQueue(size int) { | |
q.size = size | |
q.front = -1 | |
q.rear = -1 | |
q.QArray = make([]int, q.size) | |
} | |
func (q *Queue) enqueueFromFront(value int) { | |
if (q.front == 0 && q.rear == q.size-1) || q.front == q.rear+1 { | |
fmt.Println("Queue is Full!") | |
} else if q.front == -1 && q.rear == -1 { | |
q.front = 0 | |
q.rear = 0 | |
q.QArray[q.front] = value | |
} else if q.front == 0 { | |
q.front = q.size - 1 | |
q.QArray[q.front] = value | |
} else { | |
q.front = q.front - 1 | |
q.QArray[q.front] = value | |
} | |
} | |
func (q *Queue) dqueueFromFront() int { | |
x := -1 | |
if q.front == -1 && q.rear == -1 { | |
fmt.Println("Queue is Empty!") | |
} else if q.front == 0 { | |
x = q.QArray[q.front] | |
q.front = q.front + 1 | |
} else if q.front == q.rear { | |
x = q.QArray[q.front] | |
q.front = 0 | |
} else { | |
x = q.QArray[q.front] | |
q.front = q.front + 1 | |
} | |
return x | |
} | |
func (q Queue) Display() { | |
if q.front == -1 && q.rear == -1 { | |
fmt.Println("Queue is empty!") | |
} else { | |
for i := q.front; i != q.rear; i = (i + 1) % q.size { | |
fmt.Printf("%d ", q.QArray[i]) | |
} | |
} | |
print("\n") | |
} | |
func main() { | |
q := &Queue{} | |
q.initQueue(8) | |
q.enqueueFromFront(1) | |
q.enqueueFromFront(2) | |
q.enqueueFromFront(3) | |
q.enqueueFromFront(4) | |
q.enqueueFromFront(5) | |
q.enqueueFromFront(6) | |
q.enqueueFromFront(7) | |
q.enqueueFromFront(8) | |
q.Display() | |
fmt.Println("Dequeeing..", q.dqueueFromFront()) | |
q.Display() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment