main.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. "strconv"
  7. "strings"
  8. )
  9. /*
  10. del
  11. add 12
  12. del
  13. add 10
  14. add 9
  15. del
  16. del
  17. del
  18. add 10
  19. del
  20. add 1
  21. add 8
  22. add 20
  23. add 1
  24. add 11
  25. add 2
  26. del
  27. del
  28. del
  29. del
  30. */
  31. func main() {
  32. var cQueue = Constructor()
  33. input := bufio.NewScanner(os.Stdin)
  34. for input.Scan() {
  35. line := input.Text()
  36. if !strings.HasPrefix(line, "add ") && !strings.HasPrefix(line, "del") {
  37. continue
  38. }
  39. if strings.HasPrefix(line, "add ") {
  40. var digitStr = line[4:]
  41. var digit, _ = strconv.Atoi(digitStr)
  42. cQueue.AppendTail(digit)
  43. println("null")
  44. continue
  45. }
  46. if strings.HasPrefix(line, "del") {
  47. var digit = cQueue.DeleteHead()
  48. println(digit)
  49. }
  50. fmt.Println(cQueue.Stack1, "---", cQueue.Stack2)
  51. }
  52. }
  53. type CQueue struct {
  54. Stack1 []int
  55. Stack2 []int
  56. }
  57. func Constructor() CQueue {
  58. return CQueue{}
  59. }
  60. func (this *CQueue) AppendTail(value int) {
  61. this.Stack1 = append(this.Stack1, value)
  62. }
  63. func (this *CQueue) DeleteHead() int {
  64. if len(this.Stack1) == 0 {
  65. return -1
  66. }
  67. var value int
  68. this.Stack2 = append(this.Stack2, this.Stack1[0])
  69. value = this.Stack2[0]
  70. this.Stack1 = this.Stack1[1:]
  71. this.Stack2 = this.Stack2[1:]
  72. return value
  73. }