Question
HELP WITH C++ CODE.. Starlist must have a Starlist.cpp and Starlist.h.. You will be using your List, respectively, as the internal data structure to hold
HELP WITH C++ CODE..
Starlist must have a Starlist.cpp and Starlist.h.. You will be using your List, respectively, as the internal data structure to hold Planets.
Starlist
Your Starlist must have the following:
- Starlist()
- Initialize memory
- ~Starlist()
- deallocate all memory when the Star is deleted.
- long addPlanet()
- return the ID of the newly created Planet
- bool removePlanet(int)
- Takes a Planets ID as a parameter, and removes the Planet from the Star.
- You must return true upon successful deletion and false on failure if the ID isn't found.
- Planet * getPlanet(int)
- Takes a Planets ID and returns a pointer to the Planet. If the Planet is not found, it returns NULL.
- void orbit()
- Iterate through your planets and alter their orbit position by +1
- void printStarInfo()
- Prints the Star information.
- unsigned int getCurrentNumPlanets()
- returns the current number of planets store
HERE IS LIST.CPP
#include "List.h" #include
Node::Node(Planet * p){ this->planet = p; this->next = NULL; this->prev = NULL;
}
List::List(){ //A pointer to a head and tail node, both initialized to NULL this->head = NULL;//Set head to null this->tail = NULL;//set tail to null
} List::~List(){ //A destructor to clean up memory Node *current = this->head; Node *next = NULL; while (current){ next = current->next; delete current; current = next; } } void List::insert(int index, Planet * p){ //inserts an element at index, increasing the List size by 1 //if the insert index is out of bounds, you should append to the end of the list Node*n = new Node(p);
int len = size(); if(index < 0 || index >= len){ //out of bounds if(head == nullptr) head = tail = n; else { tail->next = n; n->prev = tail; tail = n; } } else{ Node* curr = head; for(int i = 0; i < index; i++){ curr = curr->next; } n->next = curr; if(curr == head){//inserting at head head = n; curr->prev = n; } else{ n->prev = curr->prev; curr->prev = n; } } }
bool List::remove(int index){ //remove the Planet object at index, decreasing the size of the Vector by 1. //return true on successful deletion or false if the index is out of bounds /* if list in NULL or invalid position is given */
if (head == NULL || index < 0) return false; Node* current = head; for (int i = 1; current != NULL && i < index; i++) current = current->next;
/* if 'n' is greater than the number of nodes
in the doubly linked list */
if (current == NULL) return false;
/* If node to be deleted is head node */
if (head == current) head = current->next;
/* Change next only if node to be deleted is NOT
the last node */ if (current->next != NULL) current->next->prev = current->prev;
/* Change prev only if node to be deleted is NOT
the first node */ if (current->prev != NULL) current->prev->next = current->next;
return true;
}
Planet* List::read(int index){ //returns a pointer to the Planet object at index //if the index is out of bounds, return NULL Node *temp = this->head; while(true){ if (temp == NULL) return NULL; else if (temp->planet->getID() == index) return temp->getPlanet(); else temp = temp->next; }
} unsigned List::size(){ //returns the current size of the List unsigned length = 0;//unsigned can never be negative Node *current = this->head;
for(current = head; current != NULL; current = current->next) { length++; } return length;
}
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