write asingle linked list which read from two list & the do
the following

1 sort the prime & nonprime num (prime should be less tn
nonprime)

2 each node has a prime num followd by nonprime

3 add a new node into its sutable plce

4 erase the most three duplicated non prime num

5 find the least duplicated prime num



write asingle linked list which read from two list & the do the following 1 sort the prime &..

Answer / Sachin Yadav

To implement a single linked list with the mentioned requirements, follow these steps:

1. Define a struct Node that contains two integers (prime and non_prime) and a pointer to the next node.
2. Create an empty linked list by initializing a variable called head to NULL.
3. Read numbers from both lists and check if they are prime or not using the following function:

```cpp
bool isPrime(int num) {
if (num <= 1) return false; // 0 and 1 are not prime numbers
for (int i = 2; i * i <= num; ++i) {
if (num % i == 0) return false; // If the number is divisible by any number between 2 and its square root, it's not a prime number.
}
return true; // If the number is not divisible by any number between 2 and its square root, it's a prime number.
}
```
4. Implement a function to insert a new node into the sorted linked list:

```cpp
void addNode(int prime, int non_prime, Node*& head) {
Node* newNode = new Node();
newNode->prime = prime;
newNode->non_prime = non_prime;
Node* current = head;

if (head == NULL || (current->prime > prime && current->non_prime >= non_prime)) { // If the list is empty or the new node should be inserted before the current node.
newNode->next = current;
head = newNode;
} else {
while (current->next != NULL && (current->next->prime <= prime || current->next->non_prime < non_prime)) {
current = current->next;
}
newNode->next = current->next;
current->next = newNode;
}
}
```
5. To erase the most three duplicated non-prime numbers, first count the occurrences of each number using an unordered_map or a hash table:

```cpp
unordered_map<int, int> counts; // A map to store the count of each non-prime number.
Node* current = head;
while (current != NULL) {
if (!isPrime(current->non_prime)) {
++counts[current->non_prime];
}
current = current->next;
}
```
6. Find the least duplicated prime number by iterating through the linked list:

```cpp
Node* leastDuplicatePrime(Node* head) {
Node* current = head;
int leastCount = INT_MAX; // Initialize with a large value.
int leastPrime = -1; // Initialize with an invalid value.

while (current != NULL) {
if (isPrime(current->prime)) {
if (counts[current->prime] < leastCount) {
leastCount = counts[current->prime];
leastPrime = current->prime;
}
}
current = current->next;
}
return new Node(leastPrime, -1); // Return a node with the least duplicated prime number and no non-prime number.
}
```

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More C++ General Interview Questions

What relational operators if statements in c++?

1 Answers  


What is the difference between static global and global ?

2 Answers   CA,


What is the Standard Template Library?

1 Answers  


What are c++ tokens?

1 Answers  


Describe protected access specifiers?

1 Answers  


Differentiate between declaration and definition in C++?

1 Answers  


What is ctime c++?

1 Answers  


Why was c++ created?

1 Answers  


What are the classes in c++?

1 Answers  


why and when we can declar member fuction as a private in the class?

1 Answers  


Write a Program for dynamically intialize a 2 dimentional array. Eg:5x20, accept strings and check for vowels and display the no.finally free the space allocated .

1 Answers  


What is the operator in c++?

1 Answers  


Categories