123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- package main
- import (
- "bufio"
- "fmt"
- "os"
- "strconv"
- "strings"
- )
- /*
- del
- add 12
- del
- add 10
- add 9
- del
- del
- del
- add 10
- del
- add 1
- add 8
- add 20
- add 1
- add 11
- add 2
- del
- del
- del
- del
- */
- func main() {
- var cQueue = Constructor()
- input := bufio.NewScanner(os.Stdin)
- for input.Scan() {
- line := input.Text()
- if !strings.HasPrefix(line, "add ") && !strings.HasPrefix(line, "del") {
- continue
- }
- if strings.HasPrefix(line, "add ") {
- var digitStr = line[4:]
- var digit, _ = strconv.Atoi(digitStr)
- cQueue.AppendTail(digit)
- println("null")
- continue
- }
- if strings.HasPrefix(line, "del") {
- var digit = cQueue.DeleteHead()
- println(digit)
- }
- fmt.Println(cQueue.Stack1, "---", cQueue.Stack2)
- }
- }
- type CQueue struct {
- Stack1 []int
- Stack2 []int
- }
- func Constructor() CQueue {
- return CQueue{}
- }
- func (this *CQueue) AppendTail(value int) {
- this.Stack1 = append(this.Stack1, value)
- }
- func (this *CQueue) DeleteHead() int {
- if len(this.Stack1) == 0 {
- return -1
- }
- var value int
- this.Stack2 = append(this.Stack2, this.Stack1[0])
- value = this.Stack2[0]
- this.Stack1 = this.Stack1[1:]
- this.Stack2 = this.Stack2[1:]
- return value
- }
|