Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m trying to create a .cpp file to this header file. #ifndef LIST_H #define LI

ID: 3571433 • Letter: I

Question

I'm trying to create a .cpp file to this header file.

#ifndef LIST_H

#define LIST_H

#include <iostream>

using namespace std;

const bool NODE = true;

class List;

class Node

{

private:

Node(int, Node*);

int data;

Node* next;

friend class List;

friend ostream& operator<<(ostream&, const List&);

};

class List

{

public:

List(int = 0);

List(const List&);

~List();

bool goToBeginning();

bool goToEnd();

bool goToNext();

bool goToPrior();

bool insertBefore(int);

bool insertAfter(int);

bool remove();

bool isEmpty() const;

bool isFull() const; // Return False by default

void clear();

List& operator=(const List&);

friend ostream& operator<<(ostream&, const List&);

private:

Node* head;

Node* cursor;

};

#endif

Explanation / Answer

listA.cpp

#include "listA.h"

List::List(int s)
{
   size = s;
   data = new char[size];
   cursor = 0;
   actual = 0;
}

List::List(const List& l)
{
   size = l.size;
   data = new char[size];
   actual = l.actual;
   cursor = l.cursor;

   //copy all elements
   for(int i = 0; i < actual; i++)
   {
      data[i] = l.data[i];
   }
}

List::~List()
{
   delete[] data;
   cursor = 0;
   actual = 0;
}

bool List::gotoBeginning()
{
   cursor = 0;
   return true;
}

bool List::gotoEnd()
{
   if(!empty())
   {
      cursor = actual - 1;
      return true;
   }
   return false;
}

bool List::gotoNext()
{
   //can't go to next if at maxsize or empty
   if(cursor == actual-1 || empty())
      return false;
   cursor++;
   return true;
}

bool List::gotoPrior()
{
   //can't go backwards if at beginning or empty
   if(cursor == 0 || empty())
      return false;
   cursor--;
   return true;
}

bool List::insertAfter(char c)
{
   //don't implement the cursor if starting from empty list
   if(empty())
   {
      data[cursor] = c;
      actual++;
      return true;
   }
   else if(!full())
   {
      if(cursor != size -1)
      {
         //shift all succeeding elements down by 1
         for( int i = actual; i > cursor+1; i--)
         {
            data[i] = data[i-1];
         }
         cursor++;
         data[cursor] = c;
         actual++;
         return true;
      }
   }
   return false;
}

bool List::insertBefore(char c)
{
   if(empty())
   {
      data[cursor] = c;
      actual++;
      return true;
   }
   else if(!full())
   {
      //shuffle all elements including cursor down
      for( int i = actual; i > cursor; i--)
      {
         data[i] = data[i-1];
      }
      data[cursor] = c;
      actual++;
      return true;
    
   }
   return false;
}

bool List::remove(char& c)
{
   if(empty())
      return false;
   c = data[cursor];
   //shift elements up, overwriting old data at the cursor
   for(int i = cursor; i < actual-1; i++)
   {
      data[i] = data[i+1];
   }
   actual--;
   return true;
}

bool List::replace(char c)
{
   if(!empty())
   {
      data[cursor] = c;
      return true;
   }
   return false;
}

bool List::getCursor(char& c) const
{
   if(!empty())
   {
      c = data[cursor];
      return true;
   }
   return false;
}

bool List::empty() const
{
   if(actual == 0)
      return true;
   return false;
}

bool List::full() const
{
   if(actual == size)
      return true;
   else
      return false;
}

bool List::clear()
{
   if(empty())
      return false;
   cursor = 0;
   actual = 0;
   return true;
}

List& List::operator=(const List& l)
{
   if(this != &l)
   {
      //remove old array
      delete[] data;
      size = l.size;
      data = new char[size];
      actual = l.actual;
      cursor = l.cursor;

      //copy all elements
      for(int i = 0; i < actual; i++)
      {
         data[i] = l.data[i];
      }
   }
   return *this;
}

ostream& operator<<(ostream& os, const List& l)
{
   if(l.empty())
   {
      os << "EMPTY";
      return os;
   }
   //print data up to cursor
   for(int i = 0; i <= l.cursor -1; i++)
   {
      os << l.data[i] << ' ';
   }

   //print cursor
   os << '[' << l.data[l.cursor] << "] ";

   //print data after cursor
   for(int i = l.cursor+1; i < l.actual; i++)
   {
      os << l.data[i] << ' ';
   }
   return os;
}


bool List::operator==(const List& l) const
{
   //check simple conditions that give immediate solution
   if(empty() ^ l.empty())
      return false;
   if(empty() && l.empty())
      return true;
   if(actual != l.actual ||
      size != l.size     ||
      cursor != l.cursor   )
      return false;

   //check individual data elements for equivalence
   for(int i = 0; i < actual; i++)
   {
      if(data[i] != l.data[i])
      {
         return false;
      }
   }

   //if got this far, they are the same
   return true;
}


listA.h

#ifndef __LIST_H_
#define __LIST_H_
#include <iostream>

using namespace std;

class List{
        public:
                List(int = 10);
                List(const List&);
                ~List();
       bool gotoBeginning();
       bool gotoEnd();
       bool gotoNext();
                bool gotoPrior();
       bool insertAfter(char);
       bool insertBefore(char);
                bool remove(char&);
       bool replace(char);
       bool getCursor(char&) const;
                bool empty() const;
                bool full() const;
                bool clear();
       List& operator=(const List&);
                friend ostream& operator<<(ostream&, const List&);
                bool operator==(const List&) const;
        private:
                int size;
       int actual;
                int cursor;
                char *data;
};

#endif