Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello I need help with below assignment . Modify queue4.h and queue4.template to add three new methods to the queue ADT - enqueue: Just a

Hello I need help with below assignment

. Modify queue4.h and queue4.template to add three new methods to the queue ADT - enqueue: Just a wrapper for push. - dequeue: A wrapper for pop, but it returns the first item in the queue, as well as removes it from the queue. - isInQ: Returns true if entry is in the queue; false otherwise. If queue is empty, returns false. Use the forward iterator provided to move through the queue. Dont directly use pointers in the linked list that implements the queue. queue4.template

// FILE: queue4.template // TEMPLATE CLASS IMPLEMENTED: Queue (see queue4.h for documentation) // This file is included in the header file, and not compiled separately. // INVARIANT for the Queue class: // 1. The number of items in the queue is stored in the member variable // count. // 2. The items in the queue are stored in a linked list, with the front of // the queue stored at the head node, and the rear of the queue stored at // the final node. // 3. The member variable front_ptr is the head pointer of the linked list of // items. For a non-empty queue, the member variable rear_ptr is the // tail pointer of the linked list; for an empty list, we don't care // what's stored in rear_ptr.

#include // Provides assert #include "node2.h" // Node template class #include "queue4.h"

namespace main_savitch_8C { template queue::queue( ) { count = 0; front_ptr = NULL; }

template queue::queue(const queue& source) // Library facilities used: node2.h { count = source.count; list_copy(source.front_ptr, front_ptr, rear_ptr); }

template queue::~queue( ) { list_clear(front_ptr); }

template void queue::operator =(const queue& source) // Library facilities used: node2.h { if (this == &source) // Handle self-assignment return; list_clear(front_ptr); list_copy(source.front_ptr, front_ptr, rear_ptr); count = source.count; }

template Item& queue::front( ) // Library facilities used: cassert { assert(!empty( )); return front_ptr->data( ); }

template const Item& queue::front( ) const // Library facilities used: cassert { assert(!empty( )); return front_ptr->data( ); } template void queue::pop( ) // Library facilities used: cassert, node2.h { assert(!empty( )); list_head_remove(front_ptr); --count; } template void queue::push(const Item& entry) // Library facilities used: node2.h { if (empty( )) { // Insert first entry. list_head_insert(front_ptr, entry); rear_ptr = front_ptr; } else { // Insert an entry that is not the first. list_insert(rear_ptr, entry); rear_ptr = rear_ptr->link( ); } ++count; } // C243 students: Remove this line and next line nd add 3 methods here. Use my // function header comment style to document each method. }

Queue.h

// FILE: queue4.h (part of the namespace // TEMPLATE CLASS PROVIDED: queue (a queue of items) // // TEMPLATE PARAMETER, TYPEDEFS and MEMBER CONSTANTS for the stack class: // The template parameter, Item, is the data type of the items in the queue, // also defined as queue::value_type. It may be any of the C++ built-in // types (int, char, etc.), or a class with a default constructor, a copy // constructor, and an assignment operator. The definition // queue::size_type is the data type of any variable that keeps track of // how many items are in a queue. // NOTE: // Many compilers require the use of the new keyword typename before using // the expressions queue::value_type and queue::size_type. // Otherwise the compiler doesn't have enough information to realize that it // is the name of a data type. // // CONSTRUCTOR for the queue template class: // queue( ) // Postcondition: The queue has been initialized as an empty queue. // // MODIFICATION MEMBER FUNCTIONS for the queue template class: // Item& front( ) // Precondition: size( ) > 0 // Postcondition: The return value is a reference to the item at // the front of the queue (and the queue is unaltered). // // void pop( ) // Precondition: size( ) > 0. // Postcondition: The front item of the queue has been removed. // // void push(const Item& entry) // Postcondition: A new copy of entry has been inserted at the rear of the // queue. // // To be added by C243 student. // Item& dequeue() // Precondition: size() > 0. // Postcondition: The front item of the queue has been removed and returned. // // To be added by C243 student. // void enqueue(const Item& entry) // Postcondition: A new copy of entry has been inserted at the rear of the // queue. // // To be added by C243 student. Use an iterator here! // bool isInQ(const Item& entry) // Postcondition: The return value is true if the queue contains entry. // // CONSTANT MEMBER FUNCTIONS for the queue template class: // bool empty( ) const // Postcondition: The return value is true if the queue is empty. // // const Item& front( ) const // Precondition: size( ) > 0 // Postcondition: The return value is a const reference to the item at // the front of the queue (and the queue is unaltered). // // size_type size( ) const // Postcondition: The return value is the total number of items in the queue. // // VALUE SEMANTICS for the queue template class: // Assignments and the copy constructor may be used with queue objects.

#ifndef MAIN_SAVITCH_QUEUE4_H // Prevent duplicate definition #define MAIN_SAVITCH_QUEUE4_H #include // Provides std::size_t #include "node2.h" // Node template class

namespace main_savitch_8C { template class queue { public: // TYPEDEFS typedef std::size_t size_type; typedef Item value_type;

// Iterator code here taken from Main & Savitch, Data // Structures and Other Objects Using C++, 4th ed., p. 338. typedef main_savitch_6B::node_iterator iterator; typedef main_savitch_6B::const_node_iterator const_iterator;

// Methods to provide iterators. iterator begin() { return iterator(front_ptr); } const_iterator begin() const { return const_iterator(front_ptr); } iterator end() // Using the iterator's default constructor { return iterator(); } const_iterator end() const // Using the default constructor { return const_iterator(); }

// CONSTRUCTORS and DESTRUCTOR queue( ); queue(const queue& source); ~queue( ); // MODIFICATION MEMBER FUNCTIONS Item& front( ); void pop( ); void push(const Item& entry); Item& dequeue(); // To be added by C243 student. void enqueue(const Item& entry); // To be added by C243 student. bool isInQ(const Item& entry); // To be added by C243 student. void operator =(const queue& source); // CONSTANT MEMBER FUNCTIONS bool empty( ) const { return (count == 0); } const Item& front( ) const; size_type size( ) const { return count; } private: main_savitch_6B::node *front_ptr; main_savitch_6B::node *rear_ptr; size_type count; // Total number of items in the queue }; } #include "queue4.template" // Include the implementation #endif

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design For Mere Mortals

Authors: Michael J Hernandez

4th Edition

978-0136788041

More Books

Students also viewed these Databases questions