Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- //file to implement expanding/shrinking intlist with a map
- import "fmt"
- type IntList struct {
- list map[int]int
- //for keeping track of placement indices and insertion spots
- record map[string]int
- }
- //creates an IntList and returns it
- func createIntList() IntList {
- lst := IntList{list:make(map[int]int), record:make(map[string]int)}
- lst.record["insert"] = 0
- lst.record["last"] = -1
- return lst
- }
- //appends a new element to the end of the list, and updates the insertion and last element positions
- func (il IntList) push(elem int) {
- il.list[il.record["insert"]] = elem
- il.record["insert"] += 1
- il.record["last"] += 1
- }
- func (il IntList) pop() int {
- popped, has := il.list[il.record["last"]]
- if has {
- delete(il.list, il.record["last"])
- il.record["last"] -= 1
- il.record["insert"] -= 1
- return popped
- }
- return -1
- }
- func main() {
- tester := createIntList()
- tester.push(5)
- tester.push(8)
- fmt.Println(tester)
- fmt.Println(tester.pop())
- fmt.Println(tester)
- }
- /*{map[0:5 1:8] map[insert:2 last:1]}
- 8
- {map[0:5] map[last:0 insert:1]}
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement