Question
Please implement the following operations in the linkedList.cpp : moveToBeginning, insertBefore, and slideshow Here is linkedList.cpp if you need any more information please let me
Please implement the following operations in the linkedList.cpp : moveToBeginning, insertBefore, and slideshow
Here is linkedList.cpp if you need any more information please let me know.
/////////////////////////
#ifndef LISTLINKED_CPP
#define LISTLINKED_CPP
using namespace std;
#include
#include "ListLinked.h"
//--------------------------------------------------------------------
// NOTE: because ListNode is an inner class, references to ListNode do not take
// the
// the
// We switched from an external class in the 2nd edition to an inner class in
// order to demonstrate better OO design, as well as to get away from the ugly use of
// a private constructor in ListNode and "friend" to allow the List class to access
// the ListNode's private data.
//--------------------------------------------------------------------
template
List
ListNode* nextPtr)
// Creates a list node containing item elem and next pointer
// nextPtr.
: dataItem(nodeData), next(nextPtr)
{
}
//--------------------------------------------------------------------
template
List
// Creates an empty list. The argument is included for compatibility
// with the array implementation (maxSize specifier) and is ignored.
: head(0), cursor(0)
{
}
//--------------------------------------------------------------------
template
List
: head(0), cursor(0)
// Copy constructor. Creates a list which is equivalent in content
// to the "other" list.
{
operator=(other);
}
//--------------------------------------------------------------------
template
List
// Overloaded assignment operator. Reinitializes the list to be
// equivalent in content to the "other" list.
// Note: we include self-assignment protection by checking whether
// "this" object is identical to the "other" object.
{
if( this != &other ) {
clear();
ListNode *otherPtr = other.head;
ListNode *holdCursor = 0;
while( otherPtr != 0 ) {
insert(otherPtr->dataItem);
if(otherPtr == other.cursor) {
holdCursor = cursor;
}
otherPtr = otherPtr->next;
}
cursor = holdCursor;
}
return *this;
}
//--------------------------------------------------------------------
template
List
// Destructor. Frees the memory used by a list.
{
clear();
}
//--------------------------------------------------------------------
template
void List
// Inserts newDataItem after the cursor. If the list is empty, then
// newDataItem is inserted as the first (and only) item in the list.
// In either case, moves the cursor to newDataItem.
{
if ( isEmpty() ) { // List is empty: could use "if ( cursor == 0 )"
cursor = head = new ListNode(newDataItem, 0);
} else {
cursor = cursor->next = new ListNode(newDataItem, cursor->next);
}
}
//--------------------------------------------------------------------
template
void List
// Removes the item marked by the cursor from a list. Moves the
// cursor to the next item in the list. Assumes that the first list
// item "follows" the last list item.
{
ListNode *p, // Pointer to removed node
*q; // Pointer to prior node
// Requires that the list is not empty
if ( head == 0 )
throw logic_error ("Trying to Remove from empty list.");
if ( cursor == head ) // Remove first item. Adjust head
{
p = head;
head = head->next;
cursor = head;
}
else if ( cursor->next != 0 ) // Remove middle ite
{
p = cursor->next;
cursor->dataItem = p->dataItem;
cursor->next = p->next;
}
else // Remove last item. Reset cursor to head
{
p = cursor;
for ( q = head ; q->next != cursor ; q = q->next )
;
q->next = 0;
cursor = head;
}
delete p;
}
//--------------------------------------------------------------------
template
void List
// Replaces the item marked by the cursor with newDataItem and
// leaves the cursor at newDataItem.
{
// Requires that the list is not empty
if ( isEmpty() )
throw logic_error("Trying to replace item in empty list ");
cursor->dataItem = newDataItem;
}
//--------------------------------------------------------------------
template
void List
// Removes all the items from a list. Sets head and cursor to 0.
{
cursor = head;
while ( cursor != 0 )
{
head = head->next;
delete cursor;
cursor = head;
}
// Invariant: whether or not the while loop was entered,
// both head and cursor == 0 at this point.
// Could explicitly set head = cursor = 0, but not needed.
}
//--------------------------------------------------------------------
template
bool List
// Returns true if a list is empty. Otherwise, returns false.
{
return head == 0;
}
//--------------------------------------------------------------------
template
bool List
// Returns true if a list is full. Otherwise, returns false.
//
// Note: determining whether the free store/heap would be able to
// satisfy the request to allocate another ListNode is either ugly,
// non-portable, or both. We explain the issue to our students and
// tell them that it is acceptible to simply return false. The main
// reason we include isFull() is for compatibility with the array-based
// implementation of the list. You may choose to instruct your students
// to use a different implementation.
{
return false; // See note above and alternative below.
/*
// One alternative implementation: Not really recommended, but
// might have some pedagogic value.
// Below is a somewhat hacked way to test if the list is full.
// If a node can be successfully allocated than the list is not
// full. If the allocation fails it is implied that there is no
// more free memory therefore the list is full.
DataType testDataItem;
ListNode *p;
try
{
p = new ListNode(testDataItem, 0);
}
catch ( bad_alloc &e )
{
return true;
}
delete p;
return false;
// End of alternative implementation.
*/
}
//--------------------------------------------------------------------
template
void List
// If a list is not empty, then moves the cursor to the beginning of
// the list. If list is empty, throws logic error.
{
if (isEmpty()) {
throw logic_error("Cannot move to beginning of empty list");
}
cursor = head;
}
//--------------------------------------------------------------------
template
void List
// If a list is not empty, then moves the cursor to the end of the
// list. Otherwise, throws logic_error.
{
if (isEmpty()) {
throw logic_error("Cannot move to end of empty list");
}
while (cursor->next) { // or could use "(cursor->next != 0)"
cursor = cursor->next;
}
}
//--------------------------------------------------------------------
template
bool List
// If the cursor is not at the end of a list, then moves the
// cursor to the next item in the list and returns true. Otherwise,
// leaves cursor unmoved and returns false.
{
if (isEmpty()) {
throw logic_error("Cannot move to next item in an empty list");
}
if( cursor->next != 0 ) {
cursor = cursor->next;
return true;
} else {
return false;
}
}
//--------------------------------------------------------------------
template
bool List
// If the cursor is not at the beginning of a list, then moves the
// cursor to the preceeding item in the list and returns true.
// Otherwise, returns false.
{
if (isEmpty()) {
throw logic_error("Cannot move to prior item in an empty list");
}
if (cursor == head) {
return false;
}
ListNode* temp = cursor;
for (cursor = head; cursor->next != temp; cursor = cursor->next)
/* Do nothing. All necessary work done in increment step above. */ ;
return true;
}
//--------------------------------------------------------------------
template
DataType List
// Returns the item marked by the cursor. Requires that list is not empty.
{
if (isEmpty()) {
throw logic_error("Cannot get data at cursor in an empty list.");
}
return cursor->dataItem;
}
template
void List
{
}
template
void List
{
}
#include "show5.cpp"
#endif
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started