数据结构与算法分析 第8章 答案 Larry Nyhoff 清华大学.pdf
《数据结构与算法分析 第8章 答案 Larry Nyhoff 清华大学.pdf》由会员分享,可在线阅读,更多相关《数据结构与算法分析 第8章 答案 Larry Nyhoff 清华大学.pdf(25页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、Chapter 8 71 Chapter 8:QueuesExercises 8.21.2.myFrontmyArraymyBack0 1 2 3 4qA B C A?14myFrontmyArraymyBack0 1 2 3 4qX Y Z?33 Queue is now empty3.4.myFrontmyArraymyBack0 1 2 3 4qs r q r r31myFrontmyArraymyBack0 1 2 3 4qC A A B B43Error occurs when i=4.After ch=A isinserted in location 2,myBack is 3 a
2、ndmyFront is 4,which means the queue isfull,so the next enqueue()operationfails.5./*-DQueue.h-This header file defines a Queue data type.Basic operations:constructor:Constructs an empty queue copy constructor:Constructs a copy of a queue =:Assignment operator destructor:Destroys a queue empty:Checks
3、 if a queue is empty enqueue:Modifies a queue by adding a value at the back front:Accesses the top stack value;leaves queue unchanged dequeue:Modifies queue by removing the value at the front display:Displays all the queue elements-*/#include#ifndef DQUEUE#define DQUEUEtypedef int QueueElement;Chapt
4、er 8 72 class Queue public:/*Function members*/*Constructors*/Queue(int numElements=128);/*-Construct a Queue object.Precondition:None.Postcondition:An empty Queue object has been constructed (myFront and myBack are initialized to 0 and myArray is an array with numElements(default 128)elements of ty
5、pe QueueElement).-*/Queue(const Queue&original);/*-Copy Constructor Precondition:original is the queue to be copied and is received as a const reference parameter.Postcondition:A copy of original has been constructed.-*/*Destructor*/Queue();/*-Class destructor Precondition:None Postcondition:The dyn
6、amic array in the queue has been deallocated.-*/*Assignment*/const Queue&operator=(const Queue&rightHandSide);/*-Assignment Operator Precondition:original is the queue to be assigned and is received as a const reference parameter.Postcondition:The current queue becomes a copy of original and a const
7、 reference to it is returned.-*/bool empty()const;/*-Check if queue is empty.Precondition:None Postcondition:Returns true if queue is empty and false otherwise.-*/Chapter 8 73 void enqueue(const QueueElement&value);/*-Add a value to a queue.Precondition:value is to be added to this queue Postconditi
8、on:value is added at back of queue provided there is space;otherwise,a queue-full message is displayed and execution is terminated.-*/void display(ostream&out)const;/*-Display values stored in the queue.Precondition:ostream out is open.Postcondition:Queues contents,from front to back,have been outpu
9、t to out.-*/QueueElement front()const;/*-Retrieve value at front of queue(if any).Precondition:Queue is nonempty Postcondition:Value at front of queue is returned,unless the queue is empty;in that case,an error message is displayed and a garbage value is returned.-*/void dequeue();/*-Remove value at
10、 front of queue(if any).Precondition:Queue is nonempty.Postcondition:Value at front of queue has been removed,unless the queue is empty;in that case,an error message is displayed and execution allowed to proceed.-*/private:/*Data members*/int myFront,/front myBack;/and back of queue int myCapacity;/
11、capacity of queue QueueElement*myArray;/dynamic array to store elements;/empty slot used to distinguish /between empty and full;/end of class declaration#endif/*-DQueue.cpp-This file implements Stack member functions.Empty slot used to distinguish between empty and full-*/Chapter 8 74#include#includ
12、e#include using namespace std;#include DQueue.h/-Definition of Queue constructorQueue:Queue(int numElements)assert(numElements 0);/check precondition myCapacity=numElements;/set queue capacity /allocate array of this capacity myArray=new(nothrow)QueueElementmyCapacity;if(myArray!=0)/memory available
13、 myFront=myBack=0;else cerr Inadequate memory to allocate queue n -terminating executionn;exit(1);/or assert(myArray!=0);/-Definition of Queue copy constructorQueue:Queue(const Queue&original):myCapacity(original.myCapacity),myFront(original.myFront),myBack(original.myBack)/-Get new array for copy m
14、yArray=new(nothrow)QueueElementmyCapacity;if(myArray!=0)/check if memory available /copy originals array member into this new array for(int i=myFront;i!=myBack;i=(i+1)%myCapacity)myArrayi=original.myArrayi;else cerr *Inadequate memory to allocate queue*n;exit(1);/-Definition of Queue destructorQueue
15、:Queue()delete myArray;/-Definition of assignment operatorconst Queue&Queue:operator=(const Queue&rightHandSide)if(this!=&rightHandSide)/check that not st=st /-Allocate a new array if necessary if(myCapacity!=rightHandSide.myCapacity)delete myArray;/destroy previous array myCapacity=rightHandSide.my
16、Capacity;/copy myCapacityChapter 8 75 myArray=new QueueElementmyCapacity;if(myArray=0)/check if memory available cerr *Inadequate memory*n;exit(1);myFront=rightHandSide.myFront;/copy myFront member myBack=rightHandSide.myBack;/copy myBack member /copy queue elements for(int i=myFront;i!=myBack;i=(i+
17、1)%myCapacity)myArrayi=rightHandSide.myArrayi;return*this;/-Definition of empty()inline bool Queue:empty()const return myFront=myBack;/-Definition of enqueue()void Queue:enqueue(const QueueElement&item)if(myBack+1)%myCapacity=myFront)cerr Queue is full:cannot add to queue.Error!endl;else myArraymyBa
18、ck=item;myBack=(myBack+1)%myCapacity;/-Definition of front()QueueElement Queue:front()const if(myFront=myBack)cerr Queue is empty:error!Returning garbage valuen;QueueElement garbage;return garbage;else return myArraymyFront;/-Definition of dequeue()void Queue:dequeue()if(myFront=myBack)cerr Queue is
19、 empty:cannot remove from queue:error!n;else myFront=(myFront+1)%myCapacity;Chapter 8 76 void Queue:display(ostream&out)const for(int i=myFront;i!=myBack;i=(i+1)%myCapacity)cout myArrayi ;cout myBack)return myBack-myFront+QUEUE_CAPACITY;else return myBack-myFront;8./Prototypeint size(Queue q);/*-Fin
20、d number of elements in a queue received as a value parameter.Precondition:None Postcondition:Number of queue elements is returned.-*Chapter 8 77/Definitionint size(Queue q)int count=0;while(!q.empty()q.removeQ();count+;return count;/*Here is a version that preserves the parameter q.*/int size(Queue
21、 q)Queue temp;int count=0;while(!q.Empty()temp.addQ(q.front();q.removeQ();count+;while(!temp.empty()q.addQ(temp.front();temp.removeQ();return count;9./Prototype:QueueElement back()const;/*-Retrieve the back element of this queue.Precondition:None Postcondition:Back element of the queue is returned,u
22、nless there was none,in which case a queue-empty message is displayed.-*/Definition:QueueElement Queue:back()const if(myFront=myBack)cerr Error:queue is empty-returning garbage valuen;QueueElement garbage;return garbage;/else if(myBack=0)return myArrayQUEUE_CAPACITY-1;/else return myArraymyBack-1;Ch
23、apter 8 78 10./Prototype:QueueElement back();/*-Retrieve the back element of a queue received as a value parameter.Precondition:None Postcondition:Back element of the queue is returned,unless there was none,in which case a queue-empty message is displayed.-*/Definition:QueueElement back(Queue q)if(q
24、.empty()cerr Error:queue is empty-returning garbage valuen;QueueElement garbage;return garbage;/else QueueElement last;while(!q.empty()last=q.front();q.dequeue();return last;/-Non-destructive version(preserves parameter q)QueueElement back(Queue q)const if(q.empty()cerr Error:queue is empty-returnin
25、g garbage valuen;QueueElement garbage;return garbage;/else Queue temp;QueueElement last;while(!q.empty()last=q.front();temp.addQ(last);q.removeQ();while(!temp.empty()q.addQ(temp.front();temp.removeQ();return last;Chapter 8 79 11./Prototype:QueueElement nthElement(int n);/*-Retrieve the n-th element
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据结构与算法分析 第8章 答案 Larry Nyhoff 清华大学 数据结构 算法 分析
限制150内