Question: Implement a Queue class called DynQueue based on the dlist. Implement the enqueue( ), dequeue ( ), front( ), isEmpty ( ) methods, constructor and
Implement a Queue class called DynQueue based on the dlist. Implement the enqueue( ), dequeue ( ), front( ), isEmpty ( ) methods, constructor and destructor. First implement the int-based DynQueue, then implement the template DynQueue. Do not include other methods in the DynQueue. Test the template DynQueue with value of different types.
dlist
#includeusing namespace std; #include "PersonInfo.h" template class node { public: T value; //value stored in the node node *next; //pointer to next node node *prev; //pointer to previous node }; template class dlist { private: node *head; //pointer to front of list node *tail; //pointer to back of list public: dlist() { head=NULL; tail=NULL; } ~dlist() { node *tempPtr; while (head != NULL) { tempPtr = head; head = head->next; delete tempPtr; } tail = NULL; } void insertFront(T value); void insertBack(T value); void removeFront(); void removeBack(); void insertBefore(T value,node *nodeB); void insertAfter(T value,node *nodeA); void removeBefore(node *nodeB); void removeAfter(node *nodeA); void removeNode(node *newNode); void printDListFront(); void printDListBack(); }; //insert a node before nodeB template void dlist ::insertBefore(T value,node *nodeB) { node *newNode; newNode=new node (); newNode->prev=nodeB->prev; newNode->next =nodeB; newNode->value =value; if(nodeB->prev==NULL) { this->head=newNode; } else nodeB->prev->next = newNode; nodeB->prev=newNode; } //insert a node before the front node template void dlist ::insertFront (T value) { node *newNode; cout<<" insert at front " << value < head==NULL) { newNode=new node (); newNode->value=value; newNode->prev=NULL; newNode->next=NULL; this->head=newNode; this->tail =newNode; } else { insertBefore(value,this->head ); } } //insert a node after nodeB template void dlist ::insertAfter(T value, node *nodeB) { node *newNode; newNode=new node (); newNode->next= nodeB->next ; newNode->prev =nodeB; newNode->value =value; if(nodeB->next==NULL) { cout<<" "<< endl; this->tail =newNode; } nodeB->next=newNode; } //insert a node after the last node template void dlist ::insertBack (T value) { cout<<" insert at back " << value < tail==NULL) { insertFront(value); } else { insertAfter(value,this->tail ); } } //remove the front node template void dlist ::removeFront () { removeNode(this->head); } //remove a back node template void dlist ::removeBack () { removeNode(this->tail); } //remove before a node template void dlist ::removeBefore(node *nodeB) { if(nodeB->prev==this->head) { this->head=nodeB; this->head->prev=NULL; } else { removeNode(nodeB->prev); } } //remove after a node template void dlist ::removeAfter(node *nodeA) { if(nodeA->next==this->tail) { this->tail=nodeA; this->tail->next=NULL; } else { removeNode(nodeA->next); } } //remove a perticular node template void dlist ::removeNode(node *nodeToRemove) { node *temp = nodeToRemove; if(nodeToRemove==this->head) { this->head=this->head->next; this->head->prev=NULL; } else if (nodeToRemove==this->tail) { this->tail=this->tail->prev; this->tail->next=NULL ; } else { nodeToRemove->prev->next=nodeToRemove->next; nodeToRemove->next->prev=nodeToRemove->prev; } if (temp != NULL) delete temp; } //Print the list from front template void dlist ::printDListFront() { node * cur; cur= this->head; cout<<"----- "; cout<<"List displayed from front "; cout<<"----- "; while(cur!=NULL) { cout <<" "<< cur->value<<" "; cur=cur->next; } cout< void dlist ::printDListBack() { node * cur; cur= this->tail; cout<<"----- "; cout<<"List display from back "; cout<<"----- "; while(cur!=NULL) { cout<<" "< value<<" "; cur=cur->prev; } cout< *intlist ; intlist= new dlist (); intlist->insertBack(8); intlist->printDListFront (); intlist->insertBack(9); intlist->printDListFront (); intlist->insertBack(19); intlist->printDListFront (); intlist->insertFront(7) ; intlist->printDListFront (); intlist->insertFront(3) ; intlist->printDListFront (); intlist->insertBack(40); intlist->printDListFront (); cout << "remove first node" << endl; intlist->removeFront(); intlist->printDListFront (); intlist->removeBack(); cout << "remove last node" << endl; intlist->printDListFront (); dlist *personList = new dlist (); personList->insertBack(PersonInfo("Mike", 18)); personList->insertBack(PersonInfo("Kent", 19)); personList->insertFront(PersonInfo("Alice", 20)); personList->insertFront(PersonInfo("Bob", 21)); personList->removeBack(); personList->printDListFront(); personList->removeFront(); personList->printDListFront(); }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
