Write a program to multiply every element of the linked list with 10
#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;
}
Comments
Leave a comment