-->
Previous Table of Contents Next


Stacks

The Stacks class implements the standard version of a last-in-first-out (LIFO) stack. Three different implementations of stacks are offered by the GNU C++ class library: the VStack, the XPStack, and the SLStack. The VStack is a fixed-size stack, meaning that you must specify an upper bound on the size of the stack when you first create it. The XPStack and the SLStack are both dynamically sized stacks that are implemented in a slightly different way.

Table 27.8 lists the operations that can be performed on the Stacks classes.

Table 27.8. Stack class operators.

Operator Description

Stack st Declares st to be a stack
Stack st(sz) Declares st to be a stack of size sz
st.empty() Returns TRUE if stack is empty
st.full() Returns TRUE if stack is full
st.length() Returns the number of elements in stack
st.push(x) Puts element x onto the top of the stack
x= st.pop() Removes and returns the top element from the stack
st.top() Returns a pointer to the top element in the stack
st.del_top() Deletes the top element from the stack without returning it
st.clear() Deletes all elements from stack

Queues

The Queue class implements a standard version of a first-in-first-out (FIFO) queue. Three different kinds of queue are provided by the GNU C++ class library: the VQueue, the XPQueue, and the SLQueue. The VQueue is a fixed-size queue, so you must specify an upper bound on the size of this kind of queue when you first create it. The XPQueue and the SLQueue are both dynamically sized queues, so no upper bound is required. The operations supported by the Queue classes are listed in Table 27.9.

Table 27.9. Queue class operators.

Operator Description

Queue q Declares q to be a queue
Queue q(sz) Declares q to be a queue of size sz
q.empty() Returns TRUE if q is empty
q.full() Returns TRUE if q is full
q.length() Returns the number of elements in q
q.enq(x) Adds the x element to q
x= q.deq() Removes and returns an element from q
q.front() Returns a pointer to the front of q
q.del_front() Removes an element from q and does not return the result
q.clear Removes all elements from the queue

In addition to the normal kind of queue that is discussed in this section, the GNU C++ class library also supports double-ended queues and priority queues. Both of these types of queues have similar behavior to the regular queue. The double-ended queue adds operators for returning a pointer to the rear of the queue and deleting elements from the rear of the queue. The priority queues are arranged so that a user has fast access to the least element in the queue. They support additional operators that allow for searching for elements in the queue.


Previous Table of Contents Next