STACKS AND QUEUES

Stacks and queues are specialized data structures that are useful in many programming applications that need to add and remove items in a particular order. The Visual Basic Stack and Queue classes implement stacks and queues.

The difference between a stack and a queue is the order in which they return the items stored in them. The following two sections describe stacks and queues and explain the ways in which they return items.

Stack

A stack returns items in last-in-first-out (LIFO, pronounced life-o) order. Because of the LIFO behavior, a stack is sometimes called a LIFO list or simply a LIFO.

Adding an item to the stack is called pushing the item onto the stack and removing an item is called popping the item off of the stack. These operations have the names push and pop because a stack is like a spring-loaded stack of plates in a cafeteria or buffet. You push new plates down onto the top of the stack and the plates sink into the counter. You pop the top plate off and the stack rises to give you the next plate.

Figure 25-3 illustrates this kind of stack. If you haven’t seen this sort of thing before, don’t worry about it. Just remember that push adds an item and pop removes the top item.

FIGURE 25-3: A Stack lets you remove items in last-in-first-out (LIFO) order.

image

Normally, you use a Stack object’s Push and Pop methods to add and remove items, but the Stack ...

Get Visual Basic 2012 Programmer's Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.