Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I already have the answer to this question, but I don't understand the logic behind what is happening inside these functions. Can someone explain the

I already have the answer to this question, but I don't understand the logic behind what is happening inside these functions. Can someone explain the logic behind these functions and what is happening inside them? The finished code is below but I just need help understanding the logic and code used inside the functions in IntList.cpp file. I want to know how to write these functions.

Thanks.

Question:

home / study / engineering / computer science / questions and answers / intlist lab specifications you are required to ...

Question: IntList Lab Specifications You are required to com...

Bookmark

IntList Lab Specifications

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) {} }; 

ntList 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 NOT output 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.

The remaining functions may only be developed in your private workspace. Copy the 3 files from lab 7 into your private workspace before beginning on these functions:

IntList(const IntList &cpy): the copy constructor. Make sure this performs deep copy.

IntList & operator=(const IntList &rhs): the overloaded assignment operator. Make sure this performs deep copy.

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

void clear(): Removes (deallocates) all IntNodes in the IntList. Don't forget to set both head and tail to appropriate values for an empty list.

void selection_sort(): Sorts the integers in the list into ascending order. Do NOT move the nodes, just the integers.

void insert_ordered(int value): Inserts a data value (within a new node) in an ordered list. Assumes the list is already sorted in ascending order (i.e., Do not sort the list first, assume the list is already is sorted.) This function loops through the list until if finds the correct position for the new data value and then inserts the new IntNode in that location. This function may NOT ever sort the list.

void remove_duplicates(): Removes all duplicate data values (actually removes the nodes that contain the values) within the list. Always remove just the later value within the list when a duplicate is found. This function may NOT ever sort the list.

friend ostream & operator<<(ostream &out, const IntList &rhs): A global friend function that outputs to the stream all of the integer values within the list on a single line, each separated by a space. This function does NOT send to the stream a newline or space at the end.

main.cpp test harness for PROGRAM

Use this main.cpp file for testing your IntList.

#include "IntList.h" #include  using namespace std; int main() { cout << "Enter a test number(1-5): "; int test; cin >> test; cout << endl; //tests constructor, destructor, push_front, pop_front, display if (test == 1) { cout << " list1 constructor called"; IntList list1; cout << " pushfront 10"; list1.push_front( 10 ); cout << " pushfront 20"; list1.push_front( 20 ); cout << " pushfront 30"; list1.push_front( 30 ); cout << " list1: "; list1.display(); cout << " pop"; list1.pop_front(); cout << " list1: "; list1.display(); cout << " pop"; list1.pop_front(); cout << " list1: "; list1.display(); cout << " pop"; list1.pop_front(); cout << " list1: "; list1.display(); cout << endl; } if (test == 1) { cout << "list1 destructor called" << endl; } //tests push_back if (test == 2) { cout << " list2 constructor called"; IntList list2; cout << " pushback 10"; list2.push_back( 10 ); cout << " pushback 20"; list2.push_back( 20 ); cout << " pushback 30"; list2.push_back( 30 ); cout << " list2: "; list2.display(); cout << " pop"; list2.pop_front(); cout << " list2: "; list2.display(); cout << " pop"; list2.pop_front(); cout << " list2: "; list2.display(); cout << " pop"; list2.pop_front(); cout << " list2: "; list2.display(); cout << endl; } if (test == 2) { cout << "list2 destructor called" << endl; } //tests selection_sort if (test == 3) { cout << " list3 constructor called"; IntList list3; cout << " pushfront 10"; list3.push_front( 10 ); cout << " pushfront 20"; list3.push_front( 20 ); cout << " pushfront 30"; list3.push_front( 30 ); cout << " list3: "; list3.display(); cout << " selection_sort()"; list3.selection_sort(); cout << " list3: "; list3.display(); cout << " pop"; list3.pop_front(); cout << " pop"; list3.pop_front(); cout << " pop"; list3.pop_front(); cout << " list3: "; list3.display(); cout << " selection_sort()"; list3.selection_sort(); cout << " list3: "; list3.display(); cout << " pushfront 10"; list3.push_front( 10 ); cout << " selection_sort()"; list3.selection_sort(); cout << " list3: "; list3.display(); cout << " pushfront 20"; list3.push_front( 20 ); cout << " list3: "; list3.display(); cout << " selection_sort()"; list3.selection_sort(); cout << " list3: "; list3.display(); cout << endl; } if (test == 3) { cout << "list3 destructor called" << endl; } //tests insert_sorted if (test == 4) { cout << " list4 constructor called"; IntList list4; cout << " insert 20"; list4.insert_ordered( 20 ); cout << " insert 10"; list4.insert_ordered( 10 ); cout << " insert 30"; list4.insert_ordered( 30 ); cout << " list4: "; list4.display(); cout << " insert 50"; list4.insert_ordered( 50 ); cout << " list4: "; list4.display(); cout << " insert 40"; list4.insert_ordered( 40 ); cout << " list4: "; list4.display(); cout << " insert 11"; list4.insert_ordered( 11 ); cout << " list4: "; list4.display(); cout << " insert 10"; list4.insert_ordered( 10 ); cout << " list4: "; list4.display(); cout << " insert 11"; list4.insert_ordered( 11 ); cout << " list4: "; list4.display(); cout << " insert 9"; list4.insert_ordered( 9 ); cout << " list4: "; list4.display(); cout << " insert 50"; list4.insert_ordered( 50 ); cout << " list4: "; list4.display(); cout << " insert 51"; list4.insert_ordered( 51 ); cout << " list4: "; list4.display(); cout << endl; } if (test == 4) { cout << "list4 destructor called" << endl; } //tests remove_duplicates if (test == 5) { cout << " list5 constructor called"; IntList list5; cout << " pushfront 10"; list5.push_front( 10 ); cout << " pushfront 20"; list5.push_front( 20 ); cout << " pushfront 10"; list5.push_front( 10 ); cout << " pushfront 30"; list5.push_front( 30 ); cout << " list5: "; list5.display(); cout << " remove_duplicates()"; list5.remove_duplicates(); cout << " list5: "; list5.display(); cout << " pushfront 10"; list5.push_front( 10 ); cout << " list5: "; list5.display(); cout << " remove_duplicates()"; list5.remove_duplicates(); cout << " list5: "; list5.display(); cout << " pushfront 20"; list5.push_front( 20 ); cout << " list5: "; list5.display(); cout << " remove_duplicates()"; list5.remove_duplicates(); cout << " list5: "; list5.display(); cout << " pushfront 20"; list5.push_front( 20 ); cout << " list5: "; list5.display(); cout << " remove_duplicates()"; list5.remove_duplicates(); cout << " list5: "; list5.display(); cout << " pushfront 20"; list5.push_front( 20 ); cout << " pushfront 20"; list5.push_front( 20 ); cout << " list5: "; list5.display(); cout << " remove_duplicates()"; list5.remove_duplicates(); cout << " list5: "; list5.display(); cout << " remove_duplicates()"; list5.remove_duplicates(); cout << " list5: "; list5.display(); cout << " pop"; list5.pop_front(); cout << " pop"; list5.pop_front(); cout << " push_front(30)"; list5.push_front(30); cout << " list5: "; list5.display(); cout << " remove_duplicates()" << flush; list5.remove_duplicates(); cout << " list5: " << flush; list5.display(); cout << " push_front(30)"; list5.push_front(30); cout << " push_front(30)"; list5.push_front(30); cout << " list5: "; list5.display(); cout << " remove_duplicates()" << flush; list5.remove_duplicates(); cout << " list5: " << flush; list5.display(); cout << " remove_duplicates()" << flush; list5.remove_duplicates(); cout << " list5: " << flush; list5.display(); cout << " pop"; list5.pop_front(); cout << " list5: "; list5.display(); cout << " remove_duplicates()" << flush; list5.remove_duplicates(); cout << " list5: " << flush; list5.display(); cout << endl; } if (test == 5) { cout << "list5 destructor called" << endl; } /* * Destructor will be tested by looking at code. There is no run-time * test for it. Make sure your destructor actually deletes ALL nodes, not * just the head and/or tail. */ return 0; } 

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

More Books

Students also viewed these Databases questions

Question

True or False: SOP prevents XSS attacks. Please explain why

Answered: 1 week ago

Question

5. Identify and describe nine social and cultural identities.

Answered: 1 week ago

Question

2. Define identity.

Answered: 1 week ago

Question

4. Describe phases of majority identity development.

Answered: 1 week ago