
Data Structure

This illustrates a data structure, a means of storing data within a program.

You may see other such structures in the Data Structures category.
 Illustration of FIFO behavior
Task

Implement a FIFO queue.

Elements are added at one side and popped from the other in the order of insertion.



Operations:

   push   (aka enqueue)    - add element
   pop     (aka dequeue)    - pop first element
   empty                             - return truth value when empty


Errors:

   handle the error of trying to pop from an empty queue (behavior depends on the language and platform)


See

   Queue/Usage   for the built-in FIFO or queue of your language or standard library.


See also

 Array
 Associative array: Creation, Iteration
 Collections
 Compound data type
 Doubly-linked list: Definition, Element definition, Element insertion, List Traversal, Element Removal
 Linked list
 Queue: Definition, Usage
 Set
 Singly-linked list: Element definition, Element insertion, List Traversal, Element Removal
 Stack


class FIFO
	declare contents
 
	// define constructors for FIFO objects
	def FIFO()
		this.contents = {}
	end
	def FIFO(contents)
		this.contents = contents
	end
 
	// define methods for this class
	def push(value)
		contents.append(value)
	end
	def pop()
		if !this.empty()
			value = contents[len(contents) - 1]
			contents.remove(len(contents) - 1)
			return value
		else
			// we could throw our own exception here but
			// we'll return null instead
			return null
		end
	end
	def length()
		return len(contents)
	end
	def extend(itemlist)
		contents += itemlist
	end
	def empty()
		return len(contents) = 0
	end
 
	// define operators for this class
	def toString()
		return str(contents)
	end
	def operator+(other)
		return this.contents + other.contents
	end
	def operator*(n)
		return this.contents * n
	end
	def operator=(other)
		return this.contents = other.contents
	end
end