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!
//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:

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