Question

MODIFY THIS LINK LIST USING C++: Write a function which determines if the values in the...

MODIFY THIS LINK LIST USING C++:

Write a function which determines if the values in the queue is divisible by 5 or 10 if true the value is divided by 5

void NumberList::listDivisibleBy5or10()

ListNode* currentNode = head;   //Pointer for current value in the link list
  
   if(!head) // checks for an empty link list
   {
       std::cout<<"The list is empty \n"; // tells user that the link list is empty
   }
   else
   {
       while(currentNode)
       {
           if(currentNode->value % 5 == 0)
           {
               currentNode->value %= 5;
           }
          
           else
           currentNode->value = currentNode;
           currentNode = currentNode->next; // shows that the node after the current node is next
       }      
   }  
}

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

Answer: here is the answer for your question:you have provided the code:

void NumberList::listDivisibleBy5or10()

ListNode* currentNode = head;   //Pointer for current value in the link list
  
   if(!head) // checks for an empty link list
   {
       std::cout<<"The list is empty \n"; // tells user that the link list is empty
   }
   else
   {
       while(currentNode)
       {
           if(currentNode->value % 5 == 0)
           {
               currentNode->value %= 5;
           }
          
           else
           currentNode->value = currentNode;
           currentNode = currentNode->next; // shows that the node after the current node is next
       }      
   }  
}

In your question , you mentioned that the condition to check if a node in queue is divisible by 5 or 10 , then divide it by 5 .

So you need to make 2 changes to your code:

1. check the condition of divisibity for both 5 and 10

2. in your code , you are doing currentNode->value %=5 . here % is modulo operator which finds the remainder. This statement must be replaced by division operator . ie currentNode / = 5

3 in while loop , use currentNode!=NULL to check if the node has become null and list is traversed

hence , your final code would be :

void NumberList::listDivisibleBy5or10()

ListNode* currentNode = head;   //Pointer for current value in the link list
  
   if(!head) // checks for an empty link list
   {
       std::cout<<"The list is empty \n"; // tells user that the link list is empty
   }
   else
   {
       while(currentNode!=NULL)
       {
           if(currentNode->value % 5 == 0 || currentNode->value % 10 == 0)
           {
               currentNode->value /= 5;
           }
          
           else
           currentNode->value = currentNode;
           currentNode = currentNode->next; // shows that the node after the current node is next
       }      
   }  
}

I hope this would help you out

If you have any doubt, you can provide comment /feedback below the answer

Thanks

Add a comment
Know the answer?
Add Answer to:
MODIFY THIS LINK LIST USING C++: Write a function which determines if the values in the...
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
  • In C++, for the provided template linked list class create a derived class of it which...

    In C++, for the provided template linked list class create a derived class of it which adds the functionality to it to find the high and low value of any given data stored in the list. The derived class must be a template. LinkedList.h #pragma once #include <iostream> using namespace std; template <class T> class ListNode {    public:        T data;        ListNode<T>* next;        ListNode(T data)        {            this->data = data;...

  • #include <iostream> using namespace std; struct ListNode { float value; ListNode *next; }; ...

    #include <iostream> using namespace std; struct ListNode { float value; ListNode *next; }; ListNode *head; class LinkedList { public: int insertNode(float num); void deleteNode(float num); void destroyList(); void displayList(); LinkedList(void) {head = NULL;} ~LinkedList(void) {destroyList();} }; int LinkedList::insertNode(float num) { struct ListNode *newNode, *nodePtr = head, *prevNodePtr = NULL; newNode = new ListNode; if(newNode == NULL) { cout << "Error allocating memory for new list member!\n"; return 1; } newNode->value = num; newNode->next = NULL; if(head==NULL) { cout << "List...

  • c++ modify the attached unsorted linked list class into a sorted linked list class #include <iostream>...

    c++ modify the attached unsorted linked list class into a sorted linked list class #include <iostream> using namespace std; template<class T> struct Node {     T data;//data field     Node * next;//link field     Node(T data) {       this->data = data;     } }; template<class T> class linked_list{ private:       Node<T> *head,*current; public:       linked_list(){//constructor, empty linked list         head = NULL;         current = NULL;       }       ~linked_list(){         current = head;         while(current != NULL) {          ...

  • ***CODE MUST BE IN C++*** Using the linked list in "basic linked list" which has a...

    ***CODE MUST BE IN C++*** Using the linked list in "basic linked list" which has a STRUCTURE for each node, write FUNCTION which starts at the head and outputs the value for each node until the last node is reached. Note: your function should work with the structure that is in this program. Please do not use the example which is for a class, but this example canbe helkpful.  Also note that the function must work no matter how many nodes...

  • C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you...

    C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you to extend the interface of a list to have these member functions as well. struct ListNode { int element; ListNode *next; } Write a function to concatenate two linked lists. Given lists l1 = (2, 3, 1)and l2 = (4, 5), after return from l1.concatenate(l2)the list l1should be changed to be l1 = (2, 3, 1, 4, 5). Your function should not change l2and...

  • C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you to extend the interface of...

    C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you to extend the interface of a list to have these member functions as well. struct ListNode { int element; ListNode *next; } Write a function to concatenate two linked lists. Given lists l1 = (2, 3, 1)and l2 = (4, 5), after return from l1.concatenate(l2)the list l1should be changed to be l1 = (2, 3, 1, 4, 5). Your function should not change l2and...

  • Programming in C: I am trying to modify this linked list to be doubly linked list....

    Programming in C: I am trying to modify this linked list to be doubly linked list. I’m also trying to add a print in reverse function. I’m really struggling with how to change the insert function to doubly link the nodes without effecting the alphabetical sorting mechanism. Example of desired output: Enter your choice: 1 to insert an element into the list. 2 to delete an element from the list. 3 to end. ? 1 Enter a character: a The...

  • Q) Modify the class Linked List below to make it a Doubly Linked List. Name your...

    Q) Modify the class Linked List below to make it a Doubly Linked List. Name your class DoublyLinkedList. Add a method addEnd to add an integer at the end of the list and a method displayInReverse to print the list backwards. void addEnd(int x): create this method to add x to the end of the list. void displayInReverse(): create this method to display the list elements from the last item to the first one. Create a main() function to test...

  • Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should h...

    Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should have member function for inserting an item in the list, deleting an item from the list, and searching the list for an item. Note: the search function should return the position of the item in the list (first item at position 0) and -1 if not found. In addition, it should member functions to display the list, check if...

  • Working in C++, Complete the code. // Lab 9 code /* Insert leading comments with your...

    Working in C++, Complete the code. // Lab 9 code /* Insert leading comments with your name and the date. Describe the purpose of the code. Also, list each pointer and describe how it is used to enable the selection sort for the linked list structure. */ /* Insert code as described by the comments. */ /* Add comments to each line of code that uses a pointer, describing how it is being used. */ #include <iostream> using namespace std;...

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
ADVERTISEMENT