Question

In: Computer Science

You are required to come up with a single header file (IntList.h) that declares and implements...

You are required to come up with a single header file (IntList.h) that declares and implements the IntNode class (just copy it exactly as it is below) as well as declares the IntList Class interface only. You are also required to come up with a separate implementation file (IntList.cpp) that implements the member functions of the IntList class. While developing your IntList class you must write your own test harness (within a file named main.cpp). Never implement more than 1 or 2 member functions without fulling testing them with your own test harness.

IntNode struct

I am providing the IntNode class you are required to use. Place this class definition within the IntList.h file exactly as is. Make sure you place it above the definition of your IntList class. Notice that you will not code an implementation file for the IntNode class. The IntNode constructor has been defined inline (within the class declaration). Do not write any other functions for the IntNode class. Use as is.

struct IntNode {
    int data;
    IntNode *next;
    IntNode(int data) : data(data), next(0) {}
};

IntList class

Encapsulated (Private) Data Fields

head: IntNode *

tail: IntNode *

Public Interface (Public Member Functions)

IntList(): Initializes an empty list.

~IntList(): Deallocates all remaining dynamically allocated memory (all remaining IntNodes).

void display() const: Displays to a single line all of the int values stored in the list, each separated by a space. This function does NOToutput a newline or space at the end.

void push_front(int value): Inserts a data value (within a new node) at the front end of the list.

void pop_front(): Removes the value (actually removes the node that contains the value) at the front end of the list. Does nothing if the list is already empty.

bool empty() const: Returns true if the list does not store any data values (does not have any nodes), otherwise returns false.

main.cpp test harness for lab

Use this main.cpp file for testing your IntList.

#include <iostream>

using namespace std;

#include "IntList.h"


int main() {

  //tests constructor, destructor, push_front, pop_front, display

   {
      cout << "\nlist1 constructor called";
      IntList list1;
      cout << "\npushfront 10";
      list1.push_front(10);
      cout << "\npushfront 20";
      list1.push_front(20);
      cout << "\npushfront 30";
      list1.push_front(30);
      cout << "\nlist1: ";
      list1.display();
      cout << "\npop";
      list1.pop_front();
      cout << "\nlist1: ";
      list1.display();
      cout << "\npop";
      list1.pop_front();
      cout << "\nlist1: ";
      list1.display();
      cout << "\npop";
      list1.pop_front();
      cout << "\nlist1: ";
      list1.display();
      cout << endl;
   }
   cout << "list1 destructor called" << endl;

   return 0;
}

Solutions

Expert Solution

main.cpp


#include <iostream>

using namespace std;

#include "IntList.h"


int main() {

//tests constructor, destructor, push_front, pop_front, display
cout << "\nlist1 constructor called";
IntList list1;
cout << "\npushfront 10";    
list1.push_front(10);
cout << "\npushfront 20";
list1.push_front(20);
cout << "\npushfront 30";
list1.push_front(30);
cout << "\nlist1: ";
list1.display();
cout << "\npop";
list1.pop_front();
cout << "\nlist1: ";
list1.display();
cout << "\npop";
list1.pop_front();
cout << "\nlist1: ";
list1.display();
cout << "\npop";
list1.pop_front();
cout << "\nlist1: ";
list1.display();
cout << endl;
cout << "list1 destructor called" << endl;

return 0;
}

IntList.cpp

#include <iostream>
#include "IntList.h"

using namespace std;

IntList::IntList() : head(NULL), tail(NULL) { }

IntList::~IntList() {
while(!empty()){
pop_front();
}
}

void IntList::push_front(int value) {
/*Creates a temporary pointer of type IntNode, assigning a memory address (block of memory)
to temp_ptr of type IntNode and then initializing with a constructor. */
IntNode* temp_ptr = new IntNode(value);
/*Then we assign the current address of head (if just empty list, this is NULL) to the
   dereferenced temporary pointer's next memory address */
(*temp_ptr).next = this->head;
/*Assign the memory address of temporary pointer (which points to the newly created block of memory) to head. */
this->head = temp_ptr;
return;
}

void IntList::display() const {
/* If empty list, exit out of display function */
if(empty()) {
    return;
}
//Since the list is not empty, print the data which the dereferenced head points to
cout << (*head).data;
//Initialize a new pointer, which is assigned to the next pointer in the linked list
IntNode* plc_ptr = (*head).next;
// Create a while loop that checks whether the pointer points to anything.
// This checks to see whether it is the end of the linked list
while(plc_ptr != NULL) {
// Prints the dereferenced pointers data
    cout << " " << (*plc_ptr).data;
/*Updates the memory address which the temporary pointer points to,
going through the list each time it goes through the while loop */
    plc_ptr = (*plc_ptr).next;
}
return;
}

bool IntList::empty() const {
//The linked list is empty if the head doesn't point to any memory addres/is NULL
if(head == NULL) {
    return true;
}
return false;
}

void IntList::pop_front() {
//First check to see if the linked list is empty/if there is a first item that needs to be deleted
if(empty()) {
    return;
}
//Set a temporary pointer to the next of the first item, which is the memory address of the second item
IntNode* temp_ptr = (*head).next;
//Delete the memory address for head
delete head;
//Reassign to head the memory address of what was the memory address of the second item
head = temp_ptr;
return;
}

IntList.h

#ifndef _INTLIST_H
#define _INTLIST_H

struct IntNode {
int data;
IntNode* next;
IntNode(int data) : data(data), next(NULL) {}
};

class IntList {
public:
    IntList();
    ~IntList();
    void display() const;
    void push_front(int);
    void pop_front();
    bool empty() const;
private:
    IntNode* head;
    IntNode* tail;
};

#endif


Related Solutions

You are required to come up with a business that is fit to apply JOB Costing...
You are required to come up with a business that is fit to apply JOB Costing on. That is each product/service/project is unique than the other in its using resources, labor, and other indirect manufacturing costs. 1. You have to give a name for your business. 2. You have to explain the products or services or projects that you provide for your customers. 3. Write the Mission Statement and Vision of your firm . 4. Come up with at least...
You are required to come up with a business that is fit to apply JOB Costing...
You are required to come up with a business that is fit to apply JOB Costing on. That is each product/service/project is unique than the other in its using resources, labor, and other indirect manufacturing costs. 1. You have to give a name for your business. 2. You have to explain the products or services or projects that you provide for your customers. 3. Write the Mission Statement and Vision of your firm . 4. Come up with at least...
You are required to come up with a business that is fit to apply JOB Costing...
You are required to come up with a business that is fit to apply JOB Costing on. That is each product/service/project is unique than the other in its using resources, labor, and other indirect manufacturing costs. 1. You have to give a name for your business. 2. You have to explain the products or services or projects that you provide for your customers. 3. Write the Mission Statement and Vision of your firm . 4. Come up with at least...
Write an entire program (with a main function and necessary header files included) that declares an...
Write an entire program (with a main function and necessary header files included) that declares an 80 byte character buffer, prompts a user to enter a single word (no spaces) from the default input device (e.g. the keyboard), which is stored in the buffer, and then reverses the string while overwriting the buffer. Print the sting before and after the reversal. The program transcript should resemble the following output: $ ./program.exe enter string: nvidia before: nvidia after: aidivn in C
using the header: #include <pthread.h> // This is a header file for a Read/Right Lock Library....
using the header: #include <pthread.h> // This is a header file for a Read/Right Lock Library. Your C code //SHOULD access your routines using these exact function // prototypes typedef struct RW_lock_s { } RW_lock_t; void RW_lock_init(RW_lock_t *lock); /* This routine should be called on a pointer to a struct variable of RW_lock_t to initialize it and ready it for use. */ void RW_read_lock(RW_lock_t *lock); /* This routine should be called at the beginning of a READER critical section */...
Note: This is a single file C++ project - No documentation is required on this assignment....
Note: This is a single file C++ project - No documentation is required on this assignment. Write a Fraction class whose objects will represent Fractions. Note: You should not simplify (reduce) fractions, you should not use "const," and all of your code should be in a single file. In this single file, the class declaration will come first, followed by the definitions of the class member functions, followed by the client program. You must provide the following member functions: A...
Global Marketing In international marketing, the marketers are required to come up with a decision as...
Global Marketing In international marketing, the marketers are required to come up with a decision as to whether they are going to standardize the product or to modify the existing products which is one of the challenging decisions that they must make. Choose a company that is known internationally and analyze the standardization and adaptation strategies for the marketing mix (4Ps) of a product in both home and chosen country. Please provide the references with your answer. Thanks!
Study the file polygon.h. It contains the header file for a class of regular polygons. Implement...
Study the file polygon.h. It contains the header file for a class of regular polygons. Implement the methods, and provide a driver to test it. It should be in C++ polygon.h file- #ifndef POLY_RVC_H #define POLY_RVC_H #include <iostream> using namespace std; class Polygon { public:    Polygon();    Polygon(int n, double l);    //accessors - all accessors should be declared "const"    // usually, accessors are also inline functions    int getSides() const { return sides; }    double getLength()...
Study the file polygon.h. It contains the header file for a class of regular polygons. Implement...
Study the file polygon.h. It contains the header file for a class of regular polygons. Implement the methods, and provide a driver to test it. It should be in C++ Write a polygon.h file with given instructions for the polygon.cpp file #include <iostream> #include <math.h> using namespace std; #ifndef M_PI # define M_PI 3.14159265358979323846 #endif int main() {    float areaP, length, sides;    cout << "\n\n Polygon area program.\n";    cout << "---------------------------------\n";    cout << " Enter the...
For this discussion, come up with a business idea focusing on a single product. Briefly describe...
For this discussion, come up with a business idea focusing on a single product. Briefly describe your business scenario. In your business, discuss the following (make sure the numbers are realistic based on a real-life scenario (i.e. use 4.99 instead of 5) and not overly simple (i.e. all of the numbers end in '0' or '5', etc.) or overly complicated). 1. What is fixed cost (make sure to outline what's included in the fixed cost)? What is the marginal cost?...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT