Question

In: Computer Science

Write an algorithm that doubles ALL the prime values of a linked lists, and then output...

Write an algorithm that doubles ALL the prime values of a linked lists, and then output the whole new list.

Solutions

Expert Solution

Algorithm:-

I).Run a loop until next part of linked list node becomes NULL. in each pass of this loop check if the node in inspection is prime or not . If it is then just double it's value and replace the that Node's data with this doubled value.

Ii).After that prints the whole new list obtained in step I).

C-style pseudoCode:-

Let the algorithm is called doubledPrime( ) which takes address of linked list as input.

Linked list's node has two fields one for data and one for pointer which holds address of next node called next.

doubledPrime ( struct node *head)

{

if(head == NULL)// means linked list is empty

return

struct node *p //make another pointer just to store head so that it remains undisturbed.

p = head

While( p-> next ! = NULL)

{

if(prime(p->data )== 1)// means Node's data is prime

p-> data = 2*(p-> data)

p = p-> next

}

//Now , new list is obtained so we just need to print it.

p = head

while (p->next != NULL)

{

printf( p->data )

if(p->next != NULL)

Printf (" ->")

p = p->next

}

}

prime(n)

{

for (I = 2 to n/2)

{

if( n% I == 0)

return 1

}

return 0

}

Time complexity of above algorithm:-

The while loop runs for 'n' number of times where n is number of elements in linked list.in each pass of this loop it runs prime() algorithm which takes O( Node's data) in worst case.

So, in worst case all the node's in Linked list are prime then for each node in linked list we have to run prime algorithm fully which will takes O(node's data) time.

Suppose linked list contain d1 ,d2 , d3 ,....dn as data.

So, Time complexity = O(d1 ) + O(d2) + O(d3) + O(d4) +....O(dn) = O(d1 + d2 + d3 +....+dn)


Related Solutions

Write a program that uses an array of doubles initialized to whatever values you wish. Write...
Write a program that uses an array of doubles initialized to whatever values you wish. Write methods to calculate and return the minimum and a method to calculate and return the maximum value in the array. You must write YOUR ORIGINAL methods for minimum and maximum. You MAY NOT use Math class methods or other library methods. Write an additional method that accepts the array as a parameter and then creates and returns a new array with all the same...
Suppose you are given two circularly linked lists, L and M. Create an algorithm for telling...
Suppose you are given two circularly linked lists, L and M. Create an algorithm for telling if L and M store the same sequence of elements (but perhaps with different starting points). Please provide a main() function with it in Java to test it.
Write an algorithm to check equality of two link lists.(java)
Write an algorithm to check equality of two link lists.(java)
How to write Prim's Algorithm with min-Heap and adjacency Lists?
How to write Prim's Algorithm with min-Heap and adjacency Lists?
C++ Linked Lists Practice your understanding of linked lists in C++ by creating a list of...
C++ Linked Lists Practice your understanding of linked lists in C++ by creating a list of songs/artist pairs. Allow your user to add song / artist pairs to the list, remove songs (and associated artist) from the list and be sure to also write a function to print the list! Have fun! Make sure you show your implementation of the use of vectors in this lab (You can use them too ) You MUST modularize your code ( meaning, there...
2.1 Linked Lists Linked lists are an example of an unbound data structure. Whereas the array...
2.1 Linked Lists Linked lists are an example of an unbound data structure. Whereas the array is based around having a fixed size per array creation, there is no logical limit on the ability of a linked list to grow. Physical limitations aside, linked lists are capable of growing largely without any limitations. To achieve this, they trade easier access to their individual elements. This is because linked lists have to be traversed from their root node to any node...
Write a recursive function in C++ that creates a copy of an array of linked lists....
Write a recursive function in C++ that creates a copy of an array of linked lists. Assuming: struct node { int data; node * next; }; class arrayList { public: arrayList(); ~arrayList(); private: node ** head; int size; //(this can equal 10) }
The Sieve of Eratosthenes is “a simple, ancient algorithm for finding all prime numbers up to...
The Sieve of Eratosthenes is “a simple, ancient algorithm for finding all prime numbers up to any given limit,” Write a method called sieve that takes an integer parameter, n, and returns a boolean array that indicates, for each number from 0 to n - 1, whether the number is prime. In Java
c# code working but output not right, I need to output all numbers like : Prime...
c# code working but output not right, I need to output all numbers like : Prime factors of 4 are: 2 x 2 here is just 2 Prime factors of 7 are: 7 Prime factors of 30 are: 2 x 3 x 5 Prime factors of 40 are: 2 x 2 x 2 x 5 here is just 2,5 Prime factors of 50 are: 2 x 5 x 5 here is just 2,5 1) How I can fix it 2)I...
Write an algorithm for combining two skip lists in O(a + b) time, where a is...
Write an algorithm for combining two skip lists in O(a + b) time, where a is the number of keys in the first list, and b is the number of keys in the second list.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT