Answer to Question #236509 in Programming & Computer Science for shivam

Question #236509

Write a program to multiply every element of the linked list with 10


1
Expert's answer
2021-09-13T14:32:56-0400
#include<iostream>
#include <stdlib.h>    
#include <time.h>      


using namespace std;
struct LinkedList{
	int number;
	LinkedList *next;  
};
void sortedInsert(struct LinkedList**LinkedListHead , int number)
{
	LinkedList*temp = *LinkedListHead ;
	LinkedList *t = new LinkedList;
	if(*LinkedListHead==NULL)
	{
		*LinkedListHead = new LinkedList;
		(*LinkedListHead)->number = number;
		(*LinkedListHead)->next = NULL;
	}


	else
	{


		if(number < temp->number) 
		{
			t = new LinkedList;
			t->number = number;
			t->next = *LinkedListHead;
			*LinkedListHead = t;
		}else
		{
			while(temp->next != NULL && !(number < temp->next->number && number >= temp->number))
				temp=temp->next;


			if(temp->next == NULL) 
			{
				temp->next = new LinkedList;
				temp = temp->next;
				temp->number = number;
				temp->next = NULL;
			}
			else 
			{
				t = new LinkedList;
				t->number = number;
				t->next = temp->next; 
				temp->next = t;
			}
		}
	}
}
void printLinkedList(struct LinkedList**LinkedListHead)
{
	struct LinkedList*temp=*LinkedListHead;
	while(temp!=NULL)
	{
		if(temp->next!=NULL)
			cout<<temp->number<<" ->";
		else
			cout<<temp->number;
		temp=temp->next; 
	}
	cout<<endl;
}


void multiplyEveryElementLinkedList(struct LinkedList**LinkedListHead)
{
	struct LinkedList*temp=*LinkedListHead;
	while(temp!=NULL)
	{
		temp->number*=10;
		temp=temp->next; 
	}
	cout<<endl;
}




int main()
{


	struct LinkedList *LinkedListHead = NULL; 
	srand (time(NULL));
	for(int i=0;i<10;i++){
		int randomNumber = rand() % 101;
		sortedInsert(&LinkedListHead,randomNumber);
	}
	cout<<"Current Linked List is:\n";
	printLinkedList(&LinkedListHead);
	
	cout<<"\nMultiply every element of the linked list with 10:\n";
	multiplyEveryElementLinkedList(&LinkedListHead);
	cout<<"Current Linked List is:\n";
	printLinkedList(&LinkedListHead);
	int k;
	cin>>k;


	return 0;
}





Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS