Question

HELP with c++ code.. Having trouble with this method. bool List::remove(int index){    //remove the Planet...

HELP with c++ code..

Having trouble with this method.

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

}

Here is the header file..

#ifndef LIST_H
#define LIST_H
#include "Planet.h"
#include <iostream>
class Node
{
   friend class List; //allow list class to access private fields
   //https://www.geeksforgeeks.org/friend-class-function-cpp/
   private:
       Planet *planet;
       Node *next;
       Node *prev;
   public:
       Planet* getPlanet(){return this->planet;}
       Node(Planet * p);
      
};

class List{
   private:
       Node *head;
       Node *tail;
   public:
       List();
       ~List();
       void insert(int index, Planet * p);
       Planet * read(int index);
       bool remove(int index);
       unsigned size();
};

#endif

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE

bool List::remove(int index)

{

/* if list in NULL or invalid position is given */

if (*head == NULL || index < 0)

return false;

struct Node* current = *head;

int i;

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;

}

Add a comment
Know the answer?
Add Answer to:
HELP with c++ code.. Having trouble with this method. bool List::remove(int index){    //remove the Planet...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT