Question
Implement the codes using the implementation of ArrayQueue, ListQueue and LinkedQueue as the case may be for the solution to the problem. Place the output
Implement the codes using the implementation of ArrayQueue, ListQueue and LinkedQueue as the case may be for the solution to the problem. Place the output of your programs below the code Description of the problem: 1. (Carrano) Use the structure implementation of the ArrayQueue capability MAX_QUEUE = 5 , with to verify the solution of the following problem. a. Build an execution diagram for each instruction (See Fig.14.10 p.407) where you indicate the position of Front and Back also include within its diagram the operations carried out for its calculation. just like him movement of the variable "count" that indicates the number of elements that contains the tail. I. Add to ArrayQueue the following values of ii. remove two items iii. Add to ArrayQueue the following values of 2. (Carrano Implement Pseudocode 13.2.1 getInteger p. 377 Cap 13. Use the ListQueue structure. Use the example used in the section, to validate the operation of your program. 3. (Carrano) Implement Pseudocode 13.2.2 isPalindrome p. 378 Chapter 13. Use the LinkedQueue structure. Test your algorithm with Q
code given to do the excersice
// Created by Frank M. Carrano and Tim Henry. // Copyright (c) 2013 __Pearson Education__. All rights reserved. /** ADT queue: Link-based implementation. Listing 14-3. @file LinkedQueue.h */ #ifndef _LINKED_QUEUE #define _LINKED_QUEUE #include #include using namespace::std; #include "QueueInterface.h" #include "Node.h" #include "PrecondViolatedExcep.h" template class LinkedQueue : public QueueInterface { private: // The queue is implemented as a chain of linked nodes that has // two external pointers, a head pointer for front of the queue and // a tail pointer for the back of the queue. Node* backPtr; Node* frontPtr; public: LinkedQueue(); LinkedQueue (const LinkedQueue& aQueue); ~LinkedQueue(); bool isEmpty() const; bool enqueue(const ItemType& newEntry); bool dequeue(); void display() const; /** @throw PrecondViolatedExcep if the queue is empty */ ItemType peekFront() const throw(PrecondViolatedExcep); }; // end LinkedQueue //#include "LinkedQueue.cpp" // Created by Frank M. Carrano and Tim Henry. // Copyright (c) 2013 __Pearson Education__. All rights reserved. // PARITALLY COMPLETE. /** @file LinkedQueue.cpp */ //#include "LinkedQueue.h" template LinkedQueue::LinkedQueue() :backPtr(nullptr), frontPtr(nullptr){} template LinkedQueue::LinkedQueue(const LinkedQueue& aQueue) { Node* origChainPtr = aQueue.frontPtr; if (origChainPtr == nullptr) { frontPtr = nullptr; backPtr = nullptr; } else { // Copy first node // Original queue is empty frontPtr = new Node(); frontPtr->setItem(origChainPtr->getItem()); // Advance original-chain pointer origChainPtr = origChainPtr->getNext(); // Copy remaining nodes Node* newChainPtr = frontPtr; // Points to last node in new chain while (origChainPtr != nullptr) { // Get next item from original chain ItemType nextItem = origChainPtr->getItem(); // Create a new node containing the next item Node* newNodePtr = new Node(nextItem); // Link new node to end of new chain newChainPtr->setNext(newNodePtr); // Advance pointers newChainPtr = newChainPtr->getNext(); origChainPtr = origChainPtr->getNext(); } // end while newChainPtr->setNext(nullptr); // Flag end of chain backPtr = newChainPtr; } // end if } // end copy constructor template LinkedQueue::~LinkedQueue() { if (frontPtr != nullptr) { Node* curPtr = frontPtr; // Start with front end of queue while (curPtr != backPtr) { Node* tempPtr = curPtr->getNext(); delete curPtr; curPtr = tempPtr; tempPtr = nullptr; } // end while delete curPtr; // Delete last node curPtr = nullptr; frontPtr = nullptr; backPtr = nullptr; } // end if } // end destructor template bool LinkedQueue::isEmpty() const{ if (backPtr == nullptr && frontPtr == nullptr) return true; else return false; } template /** @throw PrecondViolatedExcep if the queue is empty */ ItemType LinkedQueue::peekFront() const throw(PrecondViolatedExcep){ if (!isEmpty()){ return frontPtr->getItem(); } else { string message = "peekFront() called with an empty list or "; message = message + "invalid position."; throw(PrecondViolatedExcep(message)); } // end if } template bool LinkedQueue::enqueue(const ItemType& newEntry) { Node* newNodePtr = new Node(newEntry); // Insert the new node if (isEmpty()) frontPtr = newNodePtr; // Insertion into empty queue else backPtr->setNext(newNodePtr); // Insertion into nonempty queue backPtr = newNodePtr; // New node is at back return true; } // end enqueue template bool LinkedQueue::dequeue() { bool result = false; if (!isEmpty()) { // Queue is not empty; delete front Node* nodeToDeletePtr = frontPtr; if (frontPtr == backPtr) { // Special case: one node in queue frontPtr = nullptr; backPtr = nullptr; } else frontPtr = frontPtr->getNext(); // Return deleted node to system nodeToDeletePtr->setNext(nullptr); delete nodeToDeletePtr; nodeToDeletePtr = nullptr; result = true; } // end if return result; } // end dequeue template void LinkedQueue::display() const { Node* curPtr = frontPtr; while (curPtr != backPtr) { cout getItem() getNext(); } // end while cout getItem() using namespace::std; #include "LinkedQueue.h" int main(){ LinkedQueue myQueue1; myQueue1.enqueue(5); myQueue1.enqueue(15); myQueue1.enqueue(20); myQueue1.enqueue(25); myQueue1.display(); cout myQueue2(myQueue1); myQueue2.display(); system("pause"); }//end main /* 5 15 20 25 5 15 20 25 20 25 20 25 Press any key to continue . . .*/
13.2.1 Reading a String of Characters When you enter characters at a keyboard, the system must retain them in the order in which you typed them. It could use a queue for this purpose, as the following pseudocode indicates: // Read a string of characters from a single line of input into a queue aQueue = a new empty queue while (not end of line) {ReadanewcharacterintochaQueue.enqueue(ch) \} Once the characters are in a queue, the system can process them as necessary. For example, if you had typed the integer 247-without any mistakes, but possibly preceded or followed by blanks - the queue would contain digits and possibly blanks. The system could convert the digits 2,4 , and 7 into the decimal value 247 by computing 10(102+4)+7. palindrome. You can see that the first character in the string is at the front of the queue and the last character in the string is at the top of the stack. Thus, characters removed from the queue will occur in the order in which they appear in the string, and characters removed from the stack will occur in the opposite order. Knowing this, you can compare the characters at the front of the queue and the top of the stack. If the characters are the same, you can remove them. You repeat this process until the stack and the queue become empty, in which case the original string is a palindrome, or the two characters are not the same, in which case the string is not a palindrome. The following is a pseudocode version of a nonrecursive recognition algorithm for the language of palindromes: // Tests whether a given string is a palindrome. isPalindrome(someString: string): boolean // Create an empty queue and an empty stack aQueue = a new empty queue aStack = a new empty stack // Add each character of the string to both the queue and the stack 1ength = length of someString for ( i=1 through 1ength) \{ nextChar =ith character of someString aQueue. enqueue (nextChar) aStack.push (nextChar) \} // Compare the queue characters with the stack characters charactersAreEqua 1 = true while (aQueue is notempty and charactersAreEqua1) \{ queueFront = aQueue peekFront( ) stackTop = aStack peek () if (queueFront equals stackTop) \{ aQueue. dequeue() aStack.pop() \} e7se charactersAreEqua 1= fal se \} return charactersAreEqua 1Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started