Question

Hi, can you teach me a way to multiply the data in a linked list by...

Hi, can you teach me a way to multiply the data in a linked list by a number? With C++

I will be very appreciated!

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

//header files
#include <iostream>
#include <cstdlib>
using namespace std;

//structure to contain the data and the pointer
struct node {
   int data;
   struct node *next;
};

//creating a head pointer of the list pointing to null
struct node* head = NULL;
//creating a new node and inserting the element
void insert(int data){
   //calculating the size of the node
   struct node* newNode = (struct node*) std::malloc(sizeof(struct node));
   //inserting the data to the node
   newNode->data = data;
   //assinging the pointer to point to the new node
   newNode->next = head;
   head = newNode;
}
//function to show the element in the list
void show(){
   //creating a pointer to point the head of the list
   struct node* pointer=head;
   //while the pointer value is not null, show all the element in the list
   while (pointer != NULL) {
       cout<< pointer->data <<" ";
       pointer = pointer->next;
   }
}
//function to multiply the element in the list
void multiply(){
   //variable to store the number
   int num;
   struct node* point=head;
   //asking user to input the number
   cout<<"\nEnter the number to multiply: ";
   cin>>num;
   //accessing all node untill the pointer is null
   while (point!= NULL) {
       //fetch the data field and multiply by the number
       point->data = point->data * num;
       //move the pointer to next node
       point = point->next;
   }
}

//main method
int main(){
   //inserting the element in the list
   insert(23);
   insert(21);
   insert(8);
   insert(4);
   insert(11);
   insert(29);
   insert(18);
   //showing the element in the list
   cout<<"The element in the linked list are: ";
   show();
   //calling the function to multiply the element in the list
   multiply();
   //showing the element in the list after the implementation
   cout<<"\nThe element in the linked list after multiplying are: ";
   show();
   return 0;
}

-----------------------------------------

Output:

Add a comment
Know the answer?
Add Answer to:
Hi, can you teach me a way to multiply the data in a linked list by...
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