Below is my List.h header file and implementation file .cpp file. how can I add
ID: 3545437 • Letter: B
Question
Below is my List.h header file
and implementation file .cpp file.
how can I add a value input by the user to the front of the node.
this is the call provided by the professor to access the function insertFront
myList.insertFront(value);
PLEASE HELP I AM SO LOST. I may also have errors in the code provided or may be making something more complicated than it should it be. Thanks in advance for the help. I will RATE!!!
#ifndef LIST_H
#define LIST_H
//************************************************************//
// //
// Define the ListItemType before compilation //
// //
//************************************************************//
typedef int ListItemType;
//************************************************************//
// //
// Define the ADT class: List //
// //
//************************************************************//
class List
{
public:
//************************************************//
// //
// Constructor and Deconstructor //
// //
//************************************************//
List(); //default constructor
List( const List& aList); // copy constructor and deconstructor
//************************************************//
// //
// List Operatorstions //
// //
//************************************************//
bool isEmpty() const; // determines if the list is empty
int getLength() const; // determines the length of the list
void insertFront(ListItemType newItem); // inserts a new node at
// the beginning of the list
void insertBack(ListItemType newItem); // insert a new node at
// the end of the list
void removeFront(ListItemType dataItem); // remove node from the
// beginning of the list
void removeBack(ListItemType dataItem); // remove node from the end of
// of the list
void printList(); // print linked list ( list -> 10 -> 5)
List operator= (const List& aList); // overloading for list1 = list2
private:
//************************************************//
// //
// Define the struct ListNode //
// //
//************************************************//
struct ListNode // a node on the list
{
ListItemType item; //Data item on the list
ListNode *next; //Pointer to next node
};
int size; // number of items in list
ListNode *head; // pointer of link list of items
};
#endif
//**********************************************************//
// //
// List the preprocessor directives needed for execution //
// //
//**********************************************************//
#include "List.h"
#include <iostream>
using namespace std;
//**********************************************************//
// //
// Begin the implementations //
// //
//**********************************************************//
//**********************************************************//
// //
// Default constructor //
// //
//**********************************************************//
List :: List()
{
size = 0;
head = NULL;
}
//**********************************************************//
// //
// Copy constructor //
// //
//**********************************************************//
List :: List(const List& aList): size(aList.size)
{
if (aList.head == NULL)
head = NULL;
else
{
head = new ListNode;
head -> item = aList.head -> item;
ListNode* newPointer = head;
for (ListNode* firstPtr = aList.head -> next ; firstPtr != NULL ; firstPtr = firstPtr -> next)
{
newPointer -> next = new ListNode;
newPointer = newPointer -> next;
newPointer -> item = firstPtr -> item;
}
newPointer -> next = NULL;
}
}
//**********************************************************//
// //
// Determine if the list is empty //
// //
//**********************************************************//
bool List :: isEmpty() const
{
return size == 0;
}
//**********************************************************//
// //
// Determine the length of the list //
// //
//**********************************************************//
int List :: getLength() const
{
return size;
}
//**********************************************************//
// //
// Add a new node at the beginning of the list //
// //
//**********************************************************//
void :: insertFront()
{
Explanation / Answer
#ifndef LIST_H
#define LIST_H
#include <iostream>
using namespace std;
//************************************************************//
// //
// Define the ListItemType before compilation //
// //
//************************************************************//
typedef int ListItemType;
//************************************************************//
// //
// Define the ADT class: List //
// //
//************************************************************//
class List
{
public:
//************************************************//
// //
// Constructor and Deconstructor //
// //
//************************************************//
List(); //default constructor
List( const List& aList); // copy constructor and deconstructor
~List(); //destructor
//************************************************//
// //
// List Operatorstions //
// //
//************************************************//
bool isEmpty() const; // determines if the list is empty
int getLength() const; // determines the length of the list
void insertFront(ListItemType newItem); // inserts a new node at
// the beginning of the list
void insertBack(ListItemType newItem); // insert a new node at
// the end of the list
void removeFront(); // remove node from the beginning of the list
void removeBack(); // remove node from the end of the list
void printList(); // print linked list ( list -> 10 -> 5)
List& operator=(const List& aList); // overloading for list1 = list2
private:
//************************************************//
// //
// Define the struct ListNode //
// //
//************************************************//
struct ListNode // a node on the list
{
ListItemType item; //Data item on the list
ListNode* next; //Pointer to next node
ListNode(ListItemType item=0, ListNode* next=NULL)
: item(item), next(next) { }
};
int size; // number of items in list
ListNode *head; // pointer of link list of items
};
#endif
//**********************************************************//
// //
// List the preprocessor directives needed for execution //
// //
//**********************************************************//
#include "List.h"
//**********************************************************//
// //
// Begin the implementations //
// //
//**********************************************************//
//**********************************************************//
// //
// Default constructor //
// //
//**********************************************************//
List::List(): size(0), head(NULL)
{
}
//**********************************************************//
// //
// Copy constructor //
// //
//**********************************************************//
List::List(const List& aList): size(0), head(NULL)
{
if (aList.isEmpty()) return;
const ListNode* srcPtr = aList.head; // source pointer
head = new ListNode(srcPtr->item);
++size;
ListNode* dstPtr = head; // destination pointer
srcPtr = srcPtr->next;
while (srcPtr) // while there are items to copy...
{
dstPtr->next = new ListNode(srcPtr->item); // copy new item to dstPtr next
++size;
dstPtr = dstPtr->next; // update dstPtr
srcPtr = srcPtr->next; // go to next new item
}
}
//Dtor
List::~List()
{
while (head)
{
ListNode* toDelete = head;
head = head->next;
delete toDelete;
}
}
//**********************************************************//
// //
// Determine if the list is empty //
// //
//**********************************************************//
bool List::isEmpty() const
{
return size == 0;
}
//**********************************************************//
// //
// Determine the length of the list //
// //
//**********************************************************//
int List::getLength() const
{
return size;
}
//**********************************************************//
// //
// Add a new node at the beginning of the list //
// //
//**********************************************************//
void List::insertFront(ListItemType newItem)
{
head = new ListNode(newItem, head);
++size;
}
void List::insertBack(ListItemType newItem)
{
if (!head) head = new ListNode(newItem);
else
{
ListNode* tail = head;
while (tail->next) // while tail is not the last pointer...
tail = tail->next;
tail->next = new ListNode(newItem);
}
++size;
}
void List::removeFront()
{
if (!head) return; // prevent remove empty list
ListNode* toDelete = head;
head = head->next;
delete toDelete;
--size;
}
void List::removeBack()
{
if (!head) return; // prevent remove empty list
if (!head->next) // head is the last pointer
{
delete head;
head = NULL;
}
else
{
ListNode* nextToDelete = head;
while (nextToDelete->next->next) // while nextToDelete is not
//the second to last pointer...
nextToDelete = nextToDelete->next;
delete nextToDelete->next;
nextToDelete->next = NULL;
}
--size;
}
void List::printList()
{
ListNode* ptr = head;
while (ptr)
{
cout << ptr->item << " ";
ptr = ptr->next;
}
cout << endl;
}
List& List::operator=(const List& aList)
{
if (this == &aList) return *this; // prevent self-assignment
// delete old items
while (head)
{
ListNode* toDelete = head;
head = head->next;
delete toDelete;
}
head = NULL;
size = 0;
if (aList.isEmpty()) return *this;
const ListNode* srcPtr = aList.head; // source pointer
head = new ListNode(srcPtr->item);
++size;
ListNode* dstPtr = head; // destination pointer
srcPtr = srcPtr->next;
while (srcPtr) // while there are items to copy...
{
dstPtr->next = new ListNode(srcPtr->item); // copy new item to dstPtr next
++size;
dstPtr = dstPtr->next; // update dstPtr
srcPtr = srcPtr->next; // go to next new item
}
return *this;
}
//------------------------------------------------DONE---------------------------------------------------
below is my test driver:
#include <iostream>
#include "List.h"
using namespace std;
void insertConsecutiveFront(List& p, int count, int initVal=1);
void insertConsecutiveBack(List& p, int count, int initVal=1);
void removeFronts(List& p, int count);
void removeBacks(List& p, int count);
int main()
{
List listA;
insertConsecutiveFront(listA, 5);
removeFronts(listA, 5);
cout << endl;
insertConsecutiveBack(listA, 5);
removeBacks(listA, 5);
cout << endl;
insertConsecutiveFront(listA, 5);
removeBacks(listA, 5);
cout << endl;
insertConsecutiveBack(listA, 5);
removeFronts(listA, 5);
cout << endl;
insertConsecutiveBack(listA, 2);
insertConsecutiveFront(listA, 2, 3);
cout << "Copy ctor... ";
List listB = listA;
cout << listB.getLength() << " items, ";
listB.printList();
cout << endl;
removeBacks(listA, 2);
removeFronts(listA, 2);
cout << "List B contains " << listB.getLength() << " items, ";
listB.printList();
cout << endl;
cout << "Assignment operator... ";
listA = listB;
cout << "List A contains " << listA.getLength() << " items, ";
listA.printList();
cout << "Emptying list B... ";
removeFronts(listB, listB.getLength());
cout << "Done ";
cout << "List A contains " << listA.getLength() << " items, ";
listA.printList();
}
void insertConsecutiveFront(List& p, int count, int initVal)
{
cout << "Insert " << count << " items at the front of list: ";
for (int i = 0, val = initVal; i < count; ++i, ++val)
{
cout << "Insert front item " << val << "... ";
p.insertFront(val);
cout << "Done! List contains " << p.getLength() << " items, ";
p.printList();
}
}
void insertConsecutiveBack(List& p, int count, int initVal)
{
cout << "Insert " << count << " items at the back of list: ";
for (int i = 0, val = initVal; i < count; ++i, ++val)
{
cout << "Insert front item " << val << "... ";
p.insertBack(val);
cout << "Done! List contains " << p.getLength() << " items, ";
p.printList();
}
}
void removeFronts(List& p, int count)
{
cout << "Remove front " << count << " items: ";
for (int i = 0; i < count; ++i)
{
cout << "Remove front... ";
p.removeFront();
cout << "Done! List contains " << p.getLength() << " items, ";
p.printList();
}
}
void removeBacks(List& p, int count)
{
cout << "Remove back " << count << " items: ";
for (int i = 0; i < count; ++i)
{
cout << "Remove back... ";
p.removeBack();
cout << "Done! List contains " << p.getLength() << " items, ";
p.printList();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.