this simple example, the sizes of the arrays are easily known, but this same approach
can be applied to a wide range of more challenging situations.
Try This 52
A Queue Class
As you may know, a data structure is a means of organizing data. The simplest data
structure is the array, which is a linear list that supports random access to its elements.
Arrays are often used as the underpinning for more sophisticated data structures, such
as stacks and queues. A
stack is a list in which elements can be accessed in firstin, last
out (FILO) order only. A
queue is a list in which elements can be accessed in firstin,
firstout (FIFO) order only. Thus, a stack is like a stack of plates on a table—the first
down is the last to be used. A queue is like a line at a bank—the first in line is the first
served.
What makes data structures such as stacks and queues interesting is that they combine
storage for information with the methods that access that information. Thus, stacks
and queues are
data engines in which storage and retrieval are provided by the data
structure itself, not manually by your program. Such a combination is, obviously, an
excellent choice for a class, and in this project you will create a simple queue class.
In general, queues support two basic operations: put and get. Each put operation
places a new element on the end of the queue. Each get operation retrieves the next
element from the front of the queue. Queue operations are
consumptive: once an
element has been retrieved, it cannot be retrieved again. The queue can also become
full, if there is no space available to store an item, and it can become empty, if all of the
elements have been removed.
One last point: There are two basic types of queues—circular and noncircular. A
Dostları ilə paylaş: