Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Linked list even Write the function void ReadIntegers(Node **pOdd, Node **pEven)

ID: 3556881 • Letter: L

Question

Linked list even Write the function void ReadIntegers(Node **pOdd, Node **pEven) so that it reads in input containing zero or more positive numbers ending with 0. Read these numbers into two linked lists, one containing only even numbers and one containing only odd numbers, keeping the lists numerically sorted. The main provided will write out the contents of these lists. Note that you must keep each linked list sorted. Do not add the terminating 0 to either list. If a number occurs more than once, it should occur that many times in the linked lists as well. Reaching the best complexity will require using a double pointer that points to a single pointer that is within a struct..

int main() {

Node *odd, *even;

Node *temp; ReadIntegers(&odd, &even);

temp = odd; printf("Odd values:");

while (temp) { printf(" %d", temp->data); temp = temp->next; } temp = even; printf(" Even values:");

while (temp) {

printf(" %d", temp->data); temp = temp->next;

}

Test Cases: Test 1 Input Output 1 2 3 4 5 0 Odd values: 1 3 5 Even values: 2 4

Test 2 Input Output 1 1 6 5 2 3 7 4 4 0 Odd values: 1 1 3 5 7 Even values: 2 4 4 6

Test 3 Input Output 1 0 Odd values: 1 Even values:

Test 4 Input Output 4 4 0 Odd values: Even values: 4 4

t Test 5 Input Output 0 Odd values: Even values:sed 0 bytes.

Test 6 Input Output 8 7 8 4 6 13 3 5 8 16 7 5 10 5 1 2 7 9 2 6 8 9 7 8 0 Odd values: 1 3 5 5 5 7 7 7 7 9 9 13 Even values: 2 2 4 6 6 8 8 8 8 8 10 16

Explanation / Answer

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
   int data;
   struct Node* next;
}Node;


void ReadIntegers(Node **pOdd, Node **pEven)
{
   int value;
   Node* plist;
   Node* p;
   Node* pre;

   *pOdd = *pEven = NULL;

   scanf("%d", &value);
   while (value != 0)
   {
       if (value % 2 == 0)
           plist = *pEven;
       else
           plist = *pOdd;

       p = (Node*)malloc(sizeof(Node));
       p->data = value;
       p->next = NULL;

       if (plist == NULL)
       {
           plist = p;
       }
       else if (value <= plist->data)
       {
           p->next = plist;
           plist = p;
       }
       else
       {
           pre = plist;
           while (pre->next != NULL && value > pre->next->data)
               pre = pre->next;

           p->next = pre->next;
           pre->next = p;
       }

       if (value % 2 == 0)
           *pEven = plist;
       else
           *pOdd = plist;

       scanf("%d", &value);
   }

}

int main() {
   Node *odd, *even;
   Node *temp;

   ReadIntegers(&odd, &even);
   temp = odd;
   printf("Odd values:");
   while (temp) {
       printf(" %d", temp->data);
       temp = temp->next;
   }
  
   temp = even;
   printf(" Even values:");
   while (temp) {
       printf(" %d", temp->data);
       temp = temp->next;
   }

   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote