Thursday, December 6, 2007

Stack

The stack implementation is a lot simpler than the queue, as only the pointer to the last element is needed. In this case, we use the entry: 'next', which points to the location where the next 'push' to the stack should place the element to. The following diagrams illustrate the operations performed on a stack. The example shows two 'push' operations to the stack, and followed by a 'pop' at the end.

Stack empty and initialized:
Statement: stack_init (my_stack);

3
2
1
0 <- next +---+ Stack with 1 element 'push'ed Statement: stack_push (my_stack, "a"); 3 2 1 <- next 0 a +---+ Stack with 1 more element 'push'ed Statement: stack_push (my_stack, "b"); 3 2 <- next 1 b 0 a +---+ Stack after a 'pop' function call: Statement: stack_pop (my_stack, myval); # myval will contain "b" 3 2 1 <- next 0 a +---+


Stack

stack_init
Initializes a stack. The stack will be cleared of all its existing entries.
stack_length
Returns the length of the stack.
stack_push
Adds an entry to the end of the stack.
stack_pop
Remove an entry from the end of the stack.
stack_isempty
Whether the stack is empty.

In programming, a special type of data structure in which items are removed in the reverse order from that in which they are added, so the most recently added item is the first one removed. This is also called last-in, first-out (LIFO). Adding an item to a stack is called pushing. Removing an item from a stack is called popping.
A set of network protocol layers that work together. The OSI Reference Model that defines seven protocol layers is often called a stack, as is the set of TCP/IP protocols that define communication over the internet. The term stack also refers to the actual software that processes the protocols. So, for example, programmers sometimes talk about loading a stack, which means to load the software required to use a specific set of protocols. Another common phrase is binding a stack, which refers to linking a set of network protocols to a network interface card (NIC). Every NIC must have at least one stack bound to it. In Windows, the TCP/IP stack is implemented by the Winsock DLL.


Queue

The queue implementation will create 2 entries in the associative array to maintain pointers to the start and end of the queue ( 'curr' and 'next'). The 'curr' pointer points to the current element in the queue (the one that will be returned when the 'dequeue' function is called); while the 'next' pointer points to the position where the next 'enqueue' should insert the item to. The following diagrams illustrates the operation performed on the array as 2 elements are added to the queue, and then a function call to 'dequeue' is made at the end. The numbers to the left of the queue is the associative index of the array (by defining 'curr' and 'next', the array becomes an associative array).

Queue empty and initialized:
Statement: queue_init (my_queue);

3
2
1
0 <- curr, next Queue with 1 element enqueued: Statement: enqueue (my_queue, "a"); 3 2 1 <- next 0 a <- curr Queue with 1 more element enqueued: Statement: enqueue (my_queue, "b"); 3 2 <- next 1 b 0 a <- curr Queue after a 'dequeue' function call (the function returns "a"): Statement: dequeue (my_queue, myval); # myval will contain "a" 3 2 <- next 1 b <- curr 0

Queue

queue_init
Initializes a queue. The queue will be cleared of all its existing entries.
queue_length
Returns the length of the queue.
enqueue or queue_push
Adds an entry to the end of the queue.
queue_unpop
Adds an entry to the beginning of the queue. (New in 2.1)
dequeue or queue_pop
Remove an entry from the beginning of the queue.
queue_isempty
Whether the queue is empty.

In programming, a queue is a data structure in which elements are removed in the same order they were entered. This is often referred to as FIFO (first in, first out). In contrast, a stack is a data structure in which elements are removed in the reverse order from which they were entered. This is referred to as LIFO (last in, first out).

Differences between version 1 and 2

Other than the addition of several functions ('enqueue', 'dequeue', 'queue_isempty', 'stack_isempty'), the implementation of the queue and stack library has also changed. Previously, all of the functions accepts a string representing the name of the queue or stack; however, in version 2 the functions need to accept the actual array variable directly. I.e.:

Version 1: queue_init ("my_queue");
queue_push ("my_queue", "value1");

Version 2: queue_init (my_queue);
queue_push (my_queue, "value1");
where my_queue is an array. If you need to update from version 1 to version 2 of the library, the quickest way would be to remove the double quotes from the first parameter of all the func_queue_stack function calls.

Can't wait to get your hands on it? Get it here: