Discover the answers to your questions at Westonci.ca, where experts share their knowledge and insights with you. Get immediate and reliable solutions to your questions from a community of experienced experts on our Q&A platform. Connect with a community of professionals ready to provide precise solutions to your questions quickly and accurately.

Read these instructions carefully to ensure your implementation is correct.
Write the function odds which takes as input a linked list and modifies the input linked list to contain only the odd elements from the list. You must also return this list. The modified list must maintain the original order. Note: the input parameter is not pass-by-reference due to APT limitations, however, you are still expected to modify the incoming list, not make copies of nodes. Your linked list is represented by a pointer to a node; the node class is defined like so: class node { public: int data; node *next; }; For example, if odds is called with the list { 4,7,8, 2, 99, 34, -7} then input list must be modified to contain only { 7.99, -7). If the list would be empty, set it to nullptr. Your code must work for any valid list. Assume all libraries needed for your implementation and namespace std have already been included. Constraints For full credit, your answer must:
not create any new nodes
have no memory leaks
not use any auxiliary data structures (only nodes)
not change any data fields (only next fields)
modify the incoming list as well as return the resulting list
work for any valid list
run in O(N) time where N is the original number of nodes in the list


Sagot :

Answer:

hope this helps and do consider giving brainliest

Explanation:

#include <stdio.h>

#include <stdlib.h>

/* a node of the singly linked list */

struct Node

{

   int data;

   struct Node *next;

};

// Function to segregate odd nodes.

void segregateEvenOdd(struct Node **head_ref)

{

   // Starting node of list having

   // even values.

   Node *evenStart = NULL;

  

   // Ending node of even values list.

   Node *evenEnd = NULL;

  

   // Starting node of odd values list.

   Node *oddStart = NULL;

  

   // Ending node of odd values list.

   Node *oddEnd = NULL;

  

   // Node to traverse the list.

   Node *currNode = *head_ref;

  

   while(currNode != NULL){

       int val = currNode -> data;

      

       // If current value is even, add

       // it to even values list.

       if(val % 2 == 0) {

           if(evenStart == NULL){

               evenStart = currNode;

               evenEnd = evenStart;

           }

          

           else{

               evenEnd -> next = currNode;

               evenEnd = evenEnd -> next;

           }

       }

      

       // If current value is odd, add

       // it to odd values list.

       else{

           if(oddStart == NULL){

               oddStart = currNode;

               oddEnd = oddStart;

           }

           else{

               oddEnd -> next = currNode;

               oddEnd = oddEnd -> next;

           }

       }

                  

       // Move head pointer one step in

       // forward direction

       currNode = currNode -> next;  

   }

  

   // If either odd list or even list is empty,

   // no change is required as all elements

   // are either even or odd.

   if(oddStart == NULL || evenStart == NULL){

       return;

   }

  

  

   // Modify head pointer to

   // starting of even list.

   *head_ref = oddStart;

}

/* UTILITY FUNCTIONS */

/* Function to insert a node at the beginning */

void push(struct Node** head_ref, int new_data)

{

   /* allocate node */

   struct Node* new_node =

       (struct Node*) malloc(sizeof(struct Node));

   /* put in the data */

   new_node->data = new_data;

   /* link the old list off the new node */

   new_node->next = (*head_ref);

   /* move the head to point to the new node */

   (*head_ref) = new_node;

}

/* Function to print nodes in a given linked list */

void printList(struct Node *node)

{

   while (node!=NULL)

   {

       printf("%d ", node->data);

       node = node->next;

   }

}

/* Driver program to test above functions*/

int main()

{

   /* Start with the empty list */

   struct Node* head = NULL;

   /* Let us create a sample linked list as following

   0->1->4->6->9->10->11 */

   push(&head, 11);

   push(&head, 10);

   push(&head, 9);

   push(&head, 6);

   push(&head, 4);

   push(&head, 1);

   push(&head, 0);

   printf("\nOriginal Linked list \n");

   printList(head);

   segregateEvenOdd(&head);

   printf("\nModified Linked list \n");

   printList(head);

   return 0;

}