I already have the answer to this question, but I don\'t understand the logic be
ID: 3799039 • Letter: I
Question
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.
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.
Explanation / Answer
#include <iostream>
#include "IntList.h"
using namespace std;
int main() {
//In this chunk we tests constructor, destructor, push_front, pop_front, display
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;
cout << "list1 destructor called" << endl;
return 0;
}
//Now we come to 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) {
/*This creates a temporary pointer IntNode, assigning a memory address to temp_ptr of type IntNode and then initialze it with a constructor. */
IntNode* temp_ptr = new IntNode(value);
/*After this we assign the current address of head to the dereferenced temporary pointer's next memory address */
(*temp_ptr).next = this->head;
/*Now assign the memory address of temporary pointer to head. */
this->head = temp_ptr;
return;
}
void IntList::display() const {
/* If empty list, exit out of display function */
if(empty()) {
return;
}
//As our 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 Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.