11.07.2015 Views

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

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.

128 Chap. 4 Lists, Stacks, <strong>and</strong> Queuesn elements in the queue if there are n array positions. This means th<strong>at</strong> there aren + 1 different st<strong>at</strong>es for the queue (0 through n elements are possible).If the value of front is fixed, then n + 1 different values for rear are neededto distinguish among the n+1 st<strong>at</strong>es. However, there are only n possible values forrear unless we invent a special case for, say, empty queues. This is an example ofthe Pigeonhole Principle defined in Exercise 2.30. The Pigeonhole Principle st<strong>at</strong>esth<strong>at</strong>, given n pigeonholes <strong>and</strong> n + 1 pigeons, when all of the pigeons go into theholes we can be sure th<strong>at</strong> <strong>at</strong> least one hole contains more than one pigeon. In similarmanner, we can be sure th<strong>at</strong> two of the n + 1 st<strong>at</strong>es are indistinguishable by the nrel<strong>at</strong>ive values of front <strong>and</strong> rear. We must seek some other way to distinguishfull from empty queues.One obvious solution is to keep an explicit count of the number of elements inthe queue, or <strong>at</strong> least a Boolean variable th<strong>at</strong> indic<strong>at</strong>es whether the queue is emptyor not. Another solution is to make the array be of size n + 1, <strong>and</strong> only allown elements to be stored. Which of these solutions to adopt is purely a m<strong>at</strong>ter of theimplementor’s taste in such affairs. My choice is to use an array of size n + 1.Figure 4.27 shows an array-based queue implement<strong>at</strong>ion. listArray holdsthe queue elements, <strong>and</strong> as usual, the queue constructor allows an optional parameterto set the maximum size of the queue. The array as cre<strong>at</strong>ed is actually largeenough to hold one element more than the queue will allow, so th<strong>at</strong> empty queuescan be distinguished from full queues. Member maxSize is used to control thecircular motion of the queue (it is the base for the modulus oper<strong>at</strong>or). Memberrear is set to the position of the current rear element, while front is the positionof the current front element.In this implement<strong>at</strong>ion, the front of the queue is defined to be toward thelower numbered positions in the array (in the counter-clockwise direction in Figure4.26), <strong>and</strong> the rear is defined to be toward the higher-numbered positions. Thus,enqueue increments the rear pointer (modulus size), <strong>and</strong> dequeue incrementsthe front pointer. Implement<strong>at</strong>ion of all member functions is straightforward.4.3.2 Linked QueuesThe linked queue implement<strong>at</strong>ion is a straightforward adapt<strong>at</strong>ion of the linked list.Figure 4.28 shows the linked queue class declar<strong>at</strong>ion. Methods front <strong>and</strong> rearare pointers to the front <strong>and</strong> rear queue elements, respectively. We will use a headerlink node, which allows for a simpler implement<strong>at</strong>ion of the enqueue oper<strong>at</strong>ion byavoiding any special cases when the queue is empty. On initializ<strong>at</strong>ion, the front<strong>and</strong> rear pointers will point to the header node, <strong>and</strong> front will always point tothe header node while rear points to the true last link node in the queue. Methodenqueue places the new element in a link node <strong>at</strong> the end of the linked list (i.e.,the node th<strong>at</strong> rear points to) <strong>and</strong> then advances rear to point to the new linknode. Method dequeue removes <strong>and</strong> returns the first element of the list.

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

Saved successfully!

Ooh no, something went wrong!