Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

c++ programming IntList.h ************************************************************************************************* #ifndef INTLIST_H #define INTLIST_H #include IntNode.h #include using std::ostream; class IntList { public: IntList( ) = default; ~IntList( void ); bool

c++ programming

image text in transcribedimage text in transcribed

IntList.h

*************************************************************************************************

#ifndef INTLIST_H

#define INTLIST_H

#include "IntNode.h"

#include

using std::ostream;

class IntList {

public:

IntList( ) = default;

~IntList( void );

bool is_empty( void ) const;

bool add_front( int newData );

int remove_front(int& oldData );

void write( ostream& outfile ) const;

private:

IntNode* head = nullptr;

// disallow copies by restricting the following to `private`:

IntList( const IntList& );

IntList& operator=( const IntList& );

};

ostream& operator

#endif

Intlist.cpp

*******************************************************************************************************

#include "IntList.h"

#include "IntNode.h"

#include

#include

using std::ostream;

IntList::~IntList()

{

int oldData;

while(!remove_front(oldData))

{

remove_front(oldData);

}

}

bool IntList::is_empty () const

{

bool check = true;

if(head)

{

check = false;

}

return check;

}

bool IntList::add_front( int newData )

{

IntNode* newNode = new(std::nothrow) IntNode(newData);

bool status=true;

if (newNode == 0)

{

status = false;

}

else

{

newNode -> set_next(head);

head = newNode;

}

return status;

}

void IntList::write (ostream& outfile) const

{

IntNode* current = head;

while (current)

{

if (current -> get_next()) //current is last two element

{

outfile get_data() " get_next() -> get_data()

current = current -> get_next();

}

else

{

outfile get_data() / ) ";

current = current -> get_next();

}

}

}

ostream& operator

{

list.write(outfile);

return outfile;

}

int IntList::remove_front (int& oldData)

{

IntNode* current = new IntNode(oldData);

bool status=true;

if (is_empty())

{

status = false;

}

else

{

current = head;

oldData = current -> get_data();

head = head -> get_next();

delete current;

}

return status;

}

//intnode.h*******************************************************************************************

#ifndef INTNODE_H

#define INTNODE_H

#include

using std::ostream;

class IntNode {

public:

IntNode( int new_value );

int get_data( void ) const;

void set_next( IntNode* new_next );

IntNode* get_next( void ) const;

void debug_write( ostream& outfile ) const;

private:

int payload;

IntNode* next = nullptr;

};

ostream& operator

#endif

//intnode.cpp********************************************************************************************

#include "IntNode.h"

#include

using std::ostream;

IntNode::IntNode(int new_value)

{

payload = new_value;

}

int IntNode:: get_data (void) const

{

return payload;

}

void IntNode:: set_next (IntNode* new_next)

{

next = new_next;

}

IntNode* IntNode::get_next (void) const

{

return next;

}

void IntNode::debug_write (ostream& outfile) const

{

if (next)

outfile " payload

else

outfile / )";

}

ostream& operator

{

node.debug_write(outfile);

return outfile;

}

Assignment: The Queue and the Class Template A class template allows a class to be defined around "varlable types; these variable types will be specified in source code elsewhere so that the compiler can instantlate instances of the templates for the specific types desired. In this assignment, the original classes will be modified to work with types other than just integers. For example, one program may use these class templates to implement a queue of integers, while another program could implement a queue of double s or Customer s with the same class templates. From a pragmatic perspective, it is often easier to develop class templates by first writing the class with a fixed data type, then modifying it for generality once all debugging and testing for the well known type is completed. To illustrate this, you will be developing a Queue class for this assignment, based on the code you created in lab. But first, we will create a queue that works for the fixed type int. After you get that working, you will generalize it to a class template that can support (nearly) any type of payload. The Queue The queue data structure is a First-In, First-Out collection. Items are added to the queue (usually we call this action enqueue) and they line up just as you would in a line for an amusement park ride. Then, when itens are removed, the one that is at the front of the queue (i.e. the one that has been waiting the longest) is removed first. We call this action dequeue. Create a new headerimplementation file pair named "IntQueue.h and IntQueue.cpp. Use your work on the Intlist in lab and your experience in specializing it to a stack as a guide. The primary difference between the general-purpose ist and the queue is the way items are added and removed. The queue should only support one method of addition (enqueue)) and one method of removal (dequeu). These operations will map to adding at the end of the list and removing at the front, respectively. Since queue-like operations require you to add at the end of the list, you will need to add some functionality to your IntList implementation. Add a tail pointer to the IntList (as a private attribute that is initialized appropriately), and then create an add back) method in Intlist to allow items to be efficiently added at the end of the list. Don't forget to update your existing IntList methods to keep the tail1 pointer consistent with the state of the list! Once you have completed these changes, your list will now support all the operations necessary to operate as the underlying container for your queue. Your IntQueue class should implement the following interface: class IntQueue public: bool is empty consti void enqueue( int operand) int dequeue void debug write ostream& outfile) const; private: IntList list; ti ostream& operator

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 Application Development And Administration

Authors: Michael V. Mannino

4th Edition

0615231047, 978-0615231044

More Books

Students also viewed these Databases questions

Question

recognise typical interviewer errors and explain how to avoid them

Answered: 1 week ago

Question

identify and evaluate a range of recruitment and selection methods

Answered: 1 week ago

Question

understand the role of competencies and a competency framework

Answered: 1 week ago