Skip to content

Instantly share code, notes, and snippets.

@derrickturk
Last active December 11, 2022 15:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save derrickturk/a4178d4c291403f0c763f580f3edc173 to your computer and use it in GitHub Desktop.
Save derrickturk/a4178d4c291403f0c763f580f3edc173 to your computer and use it in GitHub Desktop.
SICKOS: hahahahah, yes
function push(q, val) {
if (empty(q)) {
q["head"] = 0
q["tail"] = 0
q[0] = val
} else {
q[q["tail"] + 1] = val
q["tail"] = q["tail"] + 1
}
}
# local val, new_head
function pop(q, val, new_head) {
if (empty(q))
return ""
val = q[q["head"]]
new_head = q["head"] + 1
delete q[q["head"]]
if (new_head > q["tail"]) {
delete q["head"]
delete q["tail"]
} else {
q["head"] = new_head
}
return val
}
function peek(q) {
if (empty(q))
return ""
return q[q["head"]]
}
function empty(q) {
return q["head"] == ""
}
BEGIN {
delete q
push(q, 1)
push(q, 2)
push(q, 3)
print peek(q)
pop(q)
push(q, 4)
while (v = pop(q))
print v
push(q, 4)
push(q, 5)
push(q, 6)
while (!empty(q))
print pop(q)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment