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
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 |
What relational operators if statements in c++?
What is the difference between static global and global ?
What is the Standard Template Library?
What are c++ tokens?
Describe protected access specifiers?
Differentiate between declaration and definition in C++?
What is ctime c++?
Why was c++ created?
What are the classes in c++?
why and when we can declar member fuction as a private in the class?
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 .
What is the operator in c++?