ID:1695978
 
Keywords: queue, snippet, stack
Stacks & Queues
Hello, this is an intermediate level snippet for programmers. It assumes a certain level of background knowledge. If you'd like to learn more about stacks and queues then please watch this educational video below.

Educational Video: http://www.youtube.com/watch?v=6QS_Cup1YoI
..Not authored by me

How Stacks Work
mob
Login()

/*
Lets make a stack of dishes. You can't easily
take a plate off the bottom right? So it
always takes from the top of the stack.
*/


var/Stack/dishes = new/Stack()
dishes.push("Dirty Plate 1")
dishes.push("Dirty Plate 2")
dishes.push("Dirty Plate 3")

src << dishes.pop()
src << dishes.pop()
src << dishes.pop()

/*
The output will be:
Dirty Plate 3
Dirty Plate 2
Dirty Plate 1

Whatever is on top of the stack gets chosen
first just like a real stack of dishes.
*/

How Queues Work
mob
Login()

/*
Lets make a queue of people like you would
see at a bank or shop counter. The person
who got there first goes first right?
*/


var/Queue/people = new/Queue()
people.enqueue("Marge")
people.enqueue("Homer")
people.enqueue("El Barto")

src << people.dequeue()
src << people.dequeue()
src << people.dequeue()

/*
The output will be:
Marge
Homer
El Barto

Marge was there first so she gets served
first. Just like a real shop counter.
*/

Functionality

Stack:
push(element) Puts the chosen element on top of the stack.
pop() Returns the element on top of the stack. Unless stack is empty, it will return null.

Queue:
enqueue(element) Puts the chosen element into the queue.
dequeue() Returns the next element in the queue. If the queue is empty, returns null.

Both:
isEmpty() Returns 1 if the queue or stack is empty.
empty() Empties the queue or stack of all elements.

The Code
Stack
parent_type = /Abstract_List

proc
push(var/element)
next = element
contents += element

pop()
. = next
if(contents.len > 0)
contents.len --
if(contents.len == 0) next = null
else next = contents[contents.len]


Queue
parent_type = /Abstract_List

proc
enqueue(var/element)
if(!next) next = element
contents += element

dequeue()
. = next
if(contents.len > 0)
contents.Cut(1,2)
if(contents.len == 0) next = null
else next = contents[1]


Abstract_List
var/list/contents = list()
var/next

proc
clear()
next = null
contents = new/list()

isEmpty()
if(contents > 0) return 0
else return 1

peek() return next
Suggestion: change "empty()" to "clear()", to reduce the chance of accidentally using empty() where you mean to use isEmpty()
In response to Topkasa
Probably not a bad idea when I think about it. I personally like the ring of empty() isEmpty(). There's a continuity, a stream of consciousness with that which clear() doesn't offer but from a practical standpoint you're probably right.
I'm mostly speaking from experience; empty() and isempty() are very easily mixed up, and I've done it a few times.
You should probably give examples on situations during programming in DM where using stacks and/or queues would be beneficial compared to other methods.
In response to Spunky_Girl
I feel like if a person knows how stacks and queues work it should become apparent when to use them. Not always but there's just far to many uses for me to cover like that and it really should just be intuitive when to use them.
I'm not sure what you mean. BYOND doesn't have built in stacks and queues. Also I actually modeled this off the Java API.

If you ever want to treat a BYOND list like a stack or queue you're only going to end up writing what I just wrote in some shape or form.
What he's saying, is that BYOND's built-in list can be used as a stack/queue as-is. List.Add() and List.Remove() are your push/pop functions, both of which have added functionality already. Writing your own push/pop methods is the "added overhead" he's talking about.

As for your response to me...
If you know of so many examples, then put one for each on your post to help further demonstrate the use of stacks/queues. It's only natural people will want to see it in real action rather than just your mini-examples, or at the very least some neat tricks you can pull off using them.
Your code is gobbledygook. Even just by looking at those methods I knew they didn't make any sense but to prove my point absolutely none of them worked. It's gibberish.

Frankly, you don't know what you're talking about. Thank you for crashing my thread.


Didn't initialize the contents? What? Maybe write something that works and I'll try it.
No, you wrote it wrong and then edited it just now to cover up your mistake.
I copied everything on that post exactly.
Explain to me how someone copies something incorrectly.
Ctrl+C, Ctrl+V doesn't lie.
I didn't. You can choose to believe that or not. I'm not here to tell you to.
I'm just replying to your messages. From my point of view you're the one dragging it out.

Anyway. Here's the profile of our code.

The only gain you have is by using the dot character which I refuse to use for readability purposes.

Your argument is trash, all you're exploiting is .

i gave pretty good advice

Whatever man.
In response to Alex Peterson
No, really - I think you need to go back and re-learn the basics of programming. You're repeating a behavior the language already has and just re-naming the functions to your own personal conventions, and calling it 'an addition' to someone's usable programming snippet arsenal.
In response to Cloud Magic
You make me laugh.
So, how about them examples? I've never had a need for stacks or queues in game progtamming (mostly because I just use a list and know what I'm doing), although they seem pretty important to the fundamentals of how computers work.
Page: 1 2