18.04.2013 Views

The.Algorithm.Design.Manual.Springer-Verlag.1998

The.Algorithm.Design.Manual.Springer-Verlag.1998

The.Algorithm.Design.Manual.Springer-Verlag.1998

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Containers<br />

Next: Dictionaries Up: Fundamental Data Types Previous: Fundamental Data Types<br />

Containers<br />

Containers are abstract data types that hold stuff. <strong>The</strong>y don't do much more than hold it so that it can be<br />

retrieved later. Still, they are critical to the functioning of society. We will use the term container to<br />

denote a data structure that permits storage and retrieval of data items independently of content. <strong>The</strong> two<br />

fundamental operations of any container are:<br />

● Put(C,x): Insert a new data item x into the container C.<br />

● Get(C): Retrieve the next item from the container C. Different types of containers support<br />

different retrieval orders, based on insertion order or position.<br />

Containers are typically most useful when they will contain only a limited number of items and when the<br />

retrieval order is predefined or irrelevant. <strong>The</strong> most popular type of containers are:<br />

● Stacks: Supports retrieval in last in, first out order (LIFO). Stacks are simple to implement, and<br />

very efficient. Indeed, stacks are probably the right container to use when the retrieval order<br />

doesn't matter at all, as when processing batch jobs. <strong>The</strong> put and get operations for stacks are<br />

usually called push and pop.<br />

● Queues: Supports retrieval in first in, first out order (FIFO). FIFO may seem the fairest way to<br />

control waiting times. However, for many applications, data items have infinite patience. Queues<br />

are trickier to implement than stacks and are appropriate only for applications (like certain<br />

simulations) where the order is important. <strong>The</strong> put and get operations for queues are usually called<br />

enqueue and dequeue.<br />

● Tables: Supports retrieval by position, so that put and get each accept an index as an argument.<br />

Tables are naturally implemented using arrays.<br />

Each of these containers can be implemented using either arrays or linked lists. With the exception of<br />

tables, the choice of lists versus tables probably doesn't matter very much. <strong>The</strong> key issue is whether an<br />

upper bound on the size of the container is known in advance, thus permitting a statically allocated array.<br />

Using arrays, put and get can be implemented in constant time per operation for each of the containers.<br />

file:///E|/BOOK/BOOK/NODE24.HTM (1 of 2) [19/1/2003 1:28:22]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!