258 chapter 11 deques double ended data structures
play

258 Chapter 11 Deques { Double Ended Data Structures Chapter - PDF document

258 Chapter 11 Deques { Double Ended Data Structures Chapter Ov erview In this c hapter w e will not only in tro duce the deque data structure from the standard template library , but also use the data t yp e


  1. 258

  2. Chapter 11 Deques { Double Ended Data Structures Chapter Ov erview In this c hapter w e will not only in tro duce the deque data structure from the standard template library , but also use the data t yp e to illustrate t w o imp ortan t general searc h tec hniques, depth and breadth �rst searc h. Next, w e will once again revisit the topic of inheritance, in tro duced in Chapter 9, sho wing ho w inheritance can b e used to construct fr ameworks , whic h are sk eleton applications used as the basis for solving similar problems. Finally , w e conclude the c hapter b y presen ting a simpli�ed deque implemen tation, similar to the standard library data structure. � The deque abstraction � Depth and Breadth �rst searc hing � F ramew orks � A Simpli�ed Implemen tation 11.1 The Deque abstraction The deque data t yp e (pronounced either \dec k" or \DQ") is one of the most in teresting data structures in the standard template library . Of all the STL con tainers, the deque is the least con v en tional. It represen ts a data t yp e that is seldom considered to b e one of the \classic" data abstractions, as are v ectors, lists, sets or trees. Nev ertheless, the deque is a p o w erful and v ersatile abstraction. 259

  3. 260 CHAPTER 11. DEQUES { DOUBLE ENDED D A T A STR UCTURES The op erations pro vided b y the deque data t yp e, sho wn in Figure 11.1, are a com bination of those pro vided b y the classes vecto r and list . Lik e a v ector, the deque is a randomly accessible structure. This means that instances of the class deque can b e used in most situations in whic h a v ector migh t b e emplo y ed. Lik e a list , elemen ts can b e inserted in to the middle of a deque, although suc h insertions are not as e�cien t as they are with a list. The term deque is short for Double-Ende d QUEue , and describ es the structure w ell. The deque is a com bination of stac k and queue, allo wing elemen ts to b e inserted at either end. Whereas a vecto r only allo ws e�cien t insertion at one end, the deque can p erform insertion in constan t time at either the fron t or the end of the con tainer. Lik e a v ector, a deque is a v ery space e�cien t structure, using far less memory for a giv en size collection than will, for example, a list . Ho w ev er, again lik e a v ector, insertions in to the middle of the structure are p ermitted, but are not e�cien t. An insertion in to a deque ma y require the mo v emen t of ev ery elemen t in the collection, and is th us ( n ) w orst case. O � - � - One of the most common uses for a deque is as an underlying con tainer for either a stack or a queue . The deque is a preferable con tainer for suc h emplo ymen t if the size of the collection remains relativ ely stable during the course of execution, while if the size v aries widely a list or vecto r is preferable. In man y cases the decision concerning whic h structure is most appropriate can only b e made b y p erforming direct measuremen t of program size or execution time. Because the meaning of the op erations on a deque are similar to those of a v ector or a list w e will not describ e them in detail. Instead, w e will pro ceed to an example program that mak es use of the features pro vided b y a deque . 11.2 Application{Depth and Breadth First Searc h In this section w e will examine a program that will disco v er a path through a maze, suc h as the one sho wn b elo w. W e will assume that the starting p oin t for the searc h is alw a ys in the lo w er righ t corner of the maze, and the goal is the upp er left corner. F S

  4. 11.2. APPLICA TION{DEPTH AND BREADTH FIRST SEAR CH 261 Constructors and Assignmen t deque < T > d; default constructor deque < T > d (anInt); construct with initial size deque < T > d (anInt, a T value); construct with initial size and initial v alue deque < T > d (aDeque); cop y constructor d = aDeque; assignmen t of deque from another deque d.sw ap (aDeque); sw ap con ten ts with another deque Elemen t Access and Insertion subscript access, can b e assignmen t target d[i] �rst v alue in collection d.front () �nal v alue in collection d.back () insert v alue b efore iterator d.insert (iterator, value) insert v alue at fron t of con tainer d.push front (value) insert v alue at bac k of con tainer d.push back (value) Remo v al d.pop front () remo v e elemen t from fron t of v ector d.pop back () remo v e elemen t from bac k of v ector d.erase (iterator) remo v e single elemen t d.erase (iterator,iterat or ) remo v e range of elemen ts Size n um b er of elemen ts curren tly held d.size () true if v ector is empt y d.empty () Iterators deque < T > ::iterato r itr declare a new iterator d.b egin () starting iterator d.end () stopping iterator d.rb egin () starting iterator for rev erse access d.rend () stopping iterator for rev erse access T able 11.1: Summary of deque op erations

  5. 262 CHAPTER 11. DEQUES { DOUBLE ENDED D A T A STR UCTURES Our purp ose in presen ting this example is not only to con trast t w o di�eren t t yp es of searc h tec hniques, but also to demonstrate the op erations of the deque data t yp e, and �nally to sho w ho w a deque can b e used either in a stac k-lik e or queue-lik e fashion. These t w o broad approac hes to searc hing are kno wn as depth �rst se ar ch and br e adth �rst se ar ch . W e w an t the maze searc hing program to b e general, able to solv e an y t w o dimensional maze and not simply the example maze sho wn ab o v e. W e therefore design a sc heme so that the description of the maze can b e read from an input �le. Di�eren t �les can b e used to test the program on a v ariet y of di�eren t mazes. T o see ho w to do this, note that a maze can b e describ ed as a sequence of squares, or c el ls . The example maze sho wn ab o v e, for example, is a �v e b y �v e square of 25 cells. Eac h cell can b e c haracterized b y a n um b er, whic h describ es the surrounding w alls. Sixteen n um b ers are su�cien t. In this fashion w e ha v e the follo wing v o cabulary for describing cells: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 The pattern of the n umeric v alues b ecomes apparen t if one considers the n um b er not as a decimal v alue, but as a binary pattern. The 1's p osition indicates the presence or absence of a south w all, the 2's p osition the east w all, the 4's p osition the north w all, and the 8's p osition the w est w all. A v alue suc h as 13 is written in binary as 1101. This indicates there are w alls to the north, w est, and south, but not the east. Using this sc heme, the example maze could b e describ ed b y 25 in teger v alues. In the fol- lo wing w e ha v e sup erimp osed these v alues on the maze, to b etter illustrate their relationship to the original structure. 14 12 5 4 6 10 9 4 3 10 9 5 2 13 2 14 14 10 12 2 9 1 1 3 11 This represen tation of the maze m ust b e mapp ed on to an represen- external internal tation. The in ternal represen tation need not matc h the external represen tation, as long as there is a means of con v ersion b et w een the t w o. The in ternal represen tation will again b e a sequence of cells. Eac h cell is an instance of the class cell . Instances of class cell main tain three data �elds:

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend