Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

a. Create a function for the AUList structure AUList copyReverse() which creates a new AUList with identical elements, but in reverse order. Ex: if myList

a. Create a function for the AUList structure AUList copyReverse() which creates a new AUList with identical elements, but in reverse order. Ex: if myList is an AUList with elements in the order 1, 4, 2, 8, 6, myList.copyReverse() will return an AUList with elements in the order 6, 8, 2, 4, 1.

C++ ONLY PLEASE

<--------------------------Beginning Header File--------------------->

#define MAXSIZE 10

//Remember that header files should contain DECLARATIONS, but not DEFINITIONS for the associated class functions.

class AUList

{

public:

AUList(); // Constructor

void MakeEmpty(); // Returns the list to the empty state.

bool IsFull() const; //Determines whether list is full. (Precondition -- list has been initialized). Return value of "True" indicates class is full.

int GetLength() const; //Determines the number of elements in list.

int GetItem(int); //Retrieves position of list element matching input item (if present). If not present, -1 is returned.

void PutItem(int); //Adds the item to the list.

void DeleteItem(int); //Deletes the element whose key matches item's key.

void ResetList(); //Initializes iterator's current position for an iteration through the list.

void PrintList(); //Print all elements of the list in a readable format.

int GetNextItem(); //Gets the next element in the list.

private:

int length;

int ListItems[MAXSIZE];

int currentPos;

};

<------------End Header File------------------------>

<-------------Beginning AUList.cpp---------------->

// Implementation file for AUList

#include

#include "AUList.h"

using namespace std;

AUList::AUList() {

length = 0; //a newly constructed list has 0 elements; we don't care about the contents of the array.

}

bool AUList::IsFull() const {

return (length == MAXSIZE); //Remember that preprocessor commands like #define carry over into files to which they are included (like this one!)

}

int AUList::GetLength() const {

return length;

}

int AUList::GetItem(int gitem)

{

int searchiter;

for (searchiter = length-1; searchiter>=0; searchiter--)

{ //main loop decrements from the final index in the list down to -1

if (ListItems[searchiter] == gitem) //if this condition is met, we have a match

break; //break terminates the innermost-loop in progress (so in this case, the for-loop. Has no impact on other conditionals or scope.

}

return searchiter;

}

void AUList::MakeEmpty() {

length = 0; //as with the constructor, we need do nothing to the actual array, as it now "junk" data

}

void AUList::PutItem(int item) { //This function assumes the "IsFull" condition hasn't been met.

ListItems[length] = item; //Remember that C++ uses 0-indexing.

length++;

}

void AUList::DeleteItem(int item) { //This is the less efficient version of what we discussed ("Move-Up") that maintains list order

//Note: assumes item is actually in list

bool indexfound=false;

for (int loc=0; loc

if (ListItems[loc]==item)

indexfound=true;

if (indexfound)

ListItems[loc]=ListItems[loc+1];

}

length--;

}

void AUList::ResetList() {

currentPos = -1; //We want the position BEFORE the first element, since incrementation in GetNextItems happens first

}

int AUList::GetNextItem() {

currentPos++; //Remember that currentPos is a class member variable

return ListItems[currentPos];

}

void AUList::PrintList() { //simple function to print a list's items in stored order

std::cout<<"(";

for (int loc=0; loc

std::cout<

if (loc

std::cout<<", ";

}

std::cout<<")"<

}

int main(int argc, char** argv)

{

AUList TestList;

AUList* NewList;

AUList copyReverse;

NewList* copyReverse;

std::cout<<"Newly Created List: ";

TestList.PrintList();

//copyReverse->PrintList();

for (int i=0; i<10; i++)

TestList.PutItem(100-i*10);

std::cout<<"List after 'PutItem' calls: ";

TestList.PrintList();

std::cout<<"Length after 'PutItem' calls: " <

std::cout<<"IsFull after 'PutItem' calls? " <

TestList.DeleteItem(50);

std::cout<<"List after 'DeleteItem' call: ";

TestList.PrintList();

std::cout<<"IsFull after 'DeleteItem' call? " <

std::cout<<"Index of value 80: "<

std::cout<<"Index of value 25: "<

TestList.MakeEmpty();

std::cout<<"List after 'MakeEmpty': ";

TestList.PrintList();

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

Students also viewed these Databases questions