Last active
December 11, 2022 15:36
-
-
Save derrickturk/a4178d4c291403f0c763f580f3edc173 to your computer and use it in GitHub Desktop.
SICKOS: hahahahah, yes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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