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