Ejemplo n.º 1
0
func main() {
	help()

	queue := [10]string{}
	var queueId int
	for {
		in := common.ReadDateFromInput()

		input := strings.Split(in, " ")
		switch input[0] {
		case "push":
			if queueId == 10 {
				fmt.Println("队上溢")
			} else {
				queue[queueId] = input[1]
				queueId++
			}
		case "pop":
			if queueId == 0 {
				fmt.Println("队下溢")
			} else {
				queueId--
				fmt.Println(queue[0])
				for i := 0; i < queueId; i++ {
					queue[i] = queue[i+1]
				}
				queue[queueId] = ""
			}
		case "show":
			fmt.Println(queue)
		case "quit":
			goto C
		default:
			help()

		}
	}
C:
}

func help() {
	fmt.Println("--队列操作\n命令:push ..	入队\n命令:pop	出队\n命令:show	展示队内元素\n命令:quit	退出")
}
Ejemplo n.º 2
0
func main() {
	help()

	stack := [10]string{}
	var stackId int
	for {
		in := common.ReadDateFromInput()

		input := strings.Split(in, " ")
		switch input[0] {
		case "push":
			if stackId == 10 {
				fmt.Println("栈上溢")
			} else {
				stack[stackId] = input[1]
				stackId++
			}
		case "pop":
			if stackId == 0 {
				fmt.Println(stack)
			} else {
				stackId--
				fmt.Println(stack[stackId])
				stack[stackId] = ""
			}
		case "show":
			fmt.Println(stack)
		case "quit":
			goto C
		default:
			help()

		}
	}
C:
}

func help() {
	fmt.Println("--栈操作\n命令:push ..	入栈\n命令:pop	出栈\n命令:show	展示栈内元素\n命令:quit	退出")
}
Ejemplo n.º 3
0
func main() {
	help()
	ListAll := make([]List, 100)

	for {
		in := common.ReadDateFromInput()
		input := strings.Split(in, " ")
		switch input[0] {
		case "push":
			if Length == 0 {
				ListAll[0].Key = input[1]
				ListAll[0].Next = "nil"
				ListAll[0].Prev = "nil"
				Length++
			} else {
				ListAll[Length].Key = input[1]
				ListAll[Length].Prev = ListAll[Length-1].Key
				ListAll[Length].Next = "nil"
				ListAll[Length-1].Next = ListAll[Length].Key
				Length++
			}
		case "delete":
			if Length == 0 {
				fmt.Println("链表为空")
			} else {
				for id, one := range ListAll {
					if one.Key == input[1] {
						if one.Next == "nil" && one.Prev == "nil" {
							Length--
							ListAll[0] = List{"", "", ""}
						} else if one.Prev == "nil" {
							for i := 0; i < Length; i++ {
								ListAll[i] = ListAll[i+1]
							}
							Length--
							ListAll[Length] = List{"", "", ""}
							ListAll[Length-1].Prev = "nil"
						} else if one.Next == "nil" {
							Length--
							ListAll[Length] = List{"", "", ""}
							ListAll[Length-1].Next = "nil"
						} else {
							for i := id; i <= Length; i++ {
								ListAll[id] = ListAll[id+1]
							}
							Length--
						}
						break
					}
				}
			}
		case "selete":
			var bo bool
			if Length == 0 {
				fmt.Println("链表为空")
			}
			for _, one := range ListAll {
				if one.Key == input[1] {
					fmt.Println(one)
					bo = true
				}
			}
			if !bo {
				fmt.Println("无此数据")
			}
		case "show":
			if Length == 0 {
				fmt.Println("链表为空")
			} else {
				fmt.Println()
				ListAllShow := ""
				for i := 0; i < Length; i++ {
					ListAllShow = ListAllShow + "prev:" + ListAll[i].Prev + ",key:" + ListAll[i].Key + ",next:" + ListAll[i].Next + "\n"
				}
				ListAllShow = strings.TrimSpace(ListAllShow)
				fmt.Println(ListAllShow + "\n")
			}
		case "quit":
			goto C
		default:
			help()
		}
	}
C:
}

func help() {
	fmt.Println("--链表操作\n命令:push ..	链表插入\n命令:delete ..	链表删除\n命令:selete ..	搜素链表\n命令:show	展示所有链表元素\n命令:quit	退出")
}