154k views
5 votes
Write a function which multiplies the values in odd position values by 10. Odd positions in this case refers to the first value in the list, the third value, the fifth value etc

User Artod
by
8.8k points

1 Answer

5 votes

Answer:

Using linkedlist on C++, we have the program below.

Step-by-step explanation:

#include<iostream>

#include<cstdlib>

using namespace std;

//structure of linked list

struct linkedList

{

int data;

struct linkedList *next;

};

//print linked list

void printList(struct linkedList *head)

{

linkedList *t=head;

while(t!=NULL)

{

cout<<t->data;

if(t->next!=NULL)

cout<<" -> ";

t=t->next;

}

}

//insert newnode at head of linked List

struct linkedList* insert(struct linkedList *head,int data)

{

linkedList *newnode=new linkedList;

newnode->data=data;

newnode->next=NULL;

if(head==NULL)

head=newnode;

else

{

struct linkedList *temp=head;

while(temp->next!=NULL)

temp=temp->next;

temp->next=newnode;

}

return head;

}

void multiplyOddPosition(struct linkedList *head)

{

struct linkedList *temp=head;

while(temp!=NULL)

{

temp->data = temp->data*10; //multiply values at odd position by 10

temp = temp->next;

//skip odd position values

if(temp!= NULL)

temp = temp->next;

}

}

int main()

{

int n,data;

linkedList *head=NULL;

// create linked list

head=insert(head,20);

head=insert(head,5);

head=insert(head,11);

head=insert(head,17);

head=insert(head,23);

head=insert(head,12);

head=insert(head,4);

head=insert(head,21);

cout<<"\\Linked List : ";

printList(head); //print list

multiplyOddPosition(head);

cout<<"\\Linked List After Multiply by 10 at odd position : ";

printList(head); //print list

return 0;

}

User Artsiom Miksiuk
by
8.1k points