Last active
July 6, 2016 10:04
-
-
Save matishsiao/24050743170157072a4d219950237aab to your computer and use it in GitHub Desktop.
Golang FIFO Queue Example
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" | |
) | |
var queue_front int64 = 0 | |
var queue_rear int64 = 0 | |
var queue_init bool = false | |
var queue_size int64 = 0 | |
const max_queue_size int64 = 5 | |
var queue [max_queue_size]string | |
func main() { | |
queue_test() | |
} | |
type QueueTestObject struct { | |
Type string | |
Value string | |
} | |
func queue_test() { | |
var testing []QueueTestObject | |
testing = append(testing, QueueTestObject{"push", "Test"}) | |
testing = append(testing, QueueTestObject{"push", "Test1"}) | |
testing = append(testing, QueueTestObject{"push", "Test2"}) | |
testing = append(testing, QueueTestObject{"push", "Test3"}) | |
testing = append(testing, QueueTestObject{"push", "Test4"}) | |
testing = append(testing, QueueTestObject{"push", "Test5"}) | |
testing = append(testing, QueueTestObject{"pop", ""}) | |
testing = append(testing, QueueTestObject{"pop", ""}) | |
testing = append(testing, QueueTestObject{"pop", ""}) | |
testing = append(testing, QueueTestObject{"push", "Test6"}) | |
testing = append(testing, QueueTestObject{"push", "Test7"}) | |
testing = append(testing, QueueTestObject{"pop", ""}) | |
testing = append(testing, QueueTestObject{"pop", ""}) | |
testing = append(testing, QueueTestObject{"pop", ""}) | |
testing = append(testing, QueueTestObject{"pop", ""}) | |
testing = append(testing, QueueTestObject{"pop", ""}) | |
testing = append(testing, QueueTestObject{"pop", ""}) | |
testing = append(testing, QueueTestObject{"push", "Test8"}) | |
testing = append(testing, QueueTestObject{"push", "Test9"}) | |
testing = append(testing, QueueTestObject{"push", "Test10"}) | |
testing = append(testing, QueueTestObject{"push", "Test11"}) | |
testing = append(testing, QueueTestObject{"pop", ""}) | |
testing = append(testing, QueueTestObject{"pop", ""}) | |
testing = append(testing, QueueTestObject{"pop", ""}) | |
for _, v := range testing { | |
switch v.Type { | |
case "push": | |
err := push(v.Value) | |
if err != nil { | |
fmt.Printf("push data error:%v data:%v\n", err, v.Value) | |
} | |
case "pop": | |
data, err := pop() | |
if err != nil { | |
fmt.Printf("pop data error:%v\n", err) | |
} else { | |
fmt.Printf("pop data:%v\n", data) | |
} | |
} | |
dump() | |
} | |
dump() | |
} | |
func dump() { | |
fmt.Printf("[Queue][%v][%v] size:%d\n", queue_front, queue_rear, queue_size) | |
for k, v := range queue { | |
fmt.Printf("[%v]:%v ", k, v) | |
} | |
fmt.Println("") | |
} | |
func push(data string) error { | |
if full() { | |
return fmt.Errorf("Queue has full.") | |
} | |
queue_rear++ | |
if queue_rear >= max_queue_size { | |
queue_rear = 0 | |
} | |
queue[queue_rear] = data | |
queue_size++ | |
if !queue_init { | |
queue_init = true | |
} | |
return nil | |
} | |
func pop() (string, error) { | |
queue_size-- | |
if queue_size < 0 { | |
queue_size = 0 | |
return "", fmt.Errorf("empty") | |
} | |
queue_front++ | |
if queue_front >= max_queue_size { | |
queue_front = 0 | |
} | |
data := queue[queue_front] | |
queue[queue_front] = "" | |
return data, nil | |
} | |
func full() bool { | |
if queue_size >= max_queue_size && queue_init { | |
return true | |
} | |
return false | |
} | |
/* | |
==================================================== | |
Queue Result | |
==================================================== | |
[Queue][0][1] size:1 | |
[0]: [1]:Test [2]: [3]: [4]: | |
[Queue][0][2] size:2 | |
[0]: [1]:Test [2]:Test1 [3]: [4]: | |
[Queue][0][3] size:3 | |
[0]: [1]:Test [2]:Test1 [3]:Test2 [4]: | |
[Queue][0][4] size:4 | |
[0]: [1]:Test [2]:Test1 [3]:Test2 [4]:Test3 | |
[Queue][0][0] size:5 | |
[0]:Test4 [1]:Test [2]:Test1 [3]:Test2 [4]:Test3 | |
push data error:Queue has full. data:Test5 | |
[Queue][0][0] size:5 | |
[0]:Test4 [1]:Test [2]:Test1 [3]:Test2 [4]:Test3 | |
pop data:Test | |
[Queue][1][0] size:4 | |
[0]:Test4 [1]: [2]:Test1 [3]:Test2 [4]:Test3 | |
pop data:Test1 | |
[Queue][2][0] size:3 | |
[0]:Test4 [1]: [2]: [3]:Test2 [4]:Test3 | |
pop data:Test2 | |
[Queue][3][0] size:2 | |
[0]:Test4 [1]: [2]: [3]: [4]:Test3 | |
[Queue][3][1] size:3 | |
[0]:Test4 [1]:Test6 [2]: [3]: [4]:Test3 | |
[Queue][3][2] size:4 | |
[0]:Test4 [1]:Test6 [2]:Test7 [3]: [4]:Test3 | |
pop data:Test3 | |
[Queue][4][2] size:3 | |
[0]:Test4 [1]:Test6 [2]:Test7 [3]: [4]: | |
pop data:Test4 | |
[Queue][0][2] size:2 | |
[0]: [1]:Test6 [2]:Test7 [3]: [4]: | |
pop data:Test6 | |
[Queue][1][2] size:1 | |
[0]: [1]: [2]:Test7 [3]: [4]: | |
pop data:Test7 | |
[Queue][2][2] size:0 | |
[0]: [1]: [2]: [3]: [4]: | |
pop data error:empty | |
[Queue][2][2] size:0 | |
[0]: [1]: [2]: [3]: [4]: | |
pop data error:empty | |
[Queue][2][2] size:0 | |
[0]: [1]: [2]: [3]: [4]: | |
[Queue][2][3] size:1 | |
[0]: [1]: [2]: [3]:Test8 [4]: | |
[Queue][2][4] size:2 | |
[0]: [1]: [2]: [3]:Test8 [4]:Test9 | |
[Queue][2][0] size:3 | |
[0]:Test10 [1]: [2]: [3]:Test8 [4]:Test9 | |
[Queue][2][1] size:4 | |
[0]:Test10 [1]:Test11 [2]: [3]:Test8 [4]:Test9 | |
pop data:Test8 | |
[Queue][3][1] size:3 | |
[0]:Test10 [1]:Test11 [2]: [3]: [4]:Test9 | |
pop data:Test9 | |
[Queue][4][1] size:2 | |
[0]:Test10 [1]:Test11 [2]: [3]: [4]: | |
pop data:Test10 | |
[Queue][0][1] size:1 | |
[0]: [1]:Test11 [2]: [3]: [4]: | |
[Queue][0][1] size:1 | |
[0]: [1]:Test11 [2]: [3]: [4]: | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment