go queue구조 연습

chacha·2020년 7월 24일
0

go알고리즘

목록 보기
1/8

source

package main

import (
	"fmt"
)

func main() {
	stack := newQueue()
	testValue := []int{1, 5, 3, 6, 7, 6, 5}

	for _, value := range testValue {
		stack.push(value)
	}

	for {
		value := stack.pop()
		if value == -1{
			break
		}
		fmt.Println(value)
	}
}

type Queue struct {
	s	[]int
}

func newQueue() *Queue{
	return &Queue{}
}

func(q *Queue) push(v int) {
	q.s = append(q.s, v)
}

func(q *Queue) pop() int {
	l := len(q.s)
	if l==0 {
		return -1
	}
	item, items := q.s[0], q.s[1:]
	q.s = items
	return item
}

func(q *Queue) first() int {
	return q.s[0]
}

func(q *Queue) size() int {
	return len(q.s)
}

output

1
5
3
6
7
6
5
profile
안녕하세요~ :)

0개의 댓글