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

Here is the code I solved for section 1 but I\'m having trouble to solve section

ID: 3811830 • Letter: H

Question

Here is the code

I solved for section 1 but I'm having trouble to solve section 2 a and b. Can someone help me, please?

in C language. Thank You

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

typedef struct list_tag {
   int data;
   struct list_tag * next;
   struct list_tag * prev;
} ListNode;

typedef struct {
   ListNode * first;
   ListNode * last;
} queue;

void queueInsert (queue * qp, int t);
void queuePrint (queue q);
void queuePrintReverse(queue q);
void queueInit(queue * qp);


void main(){

   queue odd_queue;
queue even_queue;
int data;
queueInit(&odd_queue); //Initialize the queue as empty
queueInit(&even_queue); //Initialize the queue as empty

   printf("Give me numbers. 0 = exit ");
   scanf("%d",&data);


   while (data != 0){
if(data%2==0)
{
       queueInsert(&even_queue, data);
}
else
{
queueInsert(&odd_queue, data);

}
       scanf("%d",&data);
   }
   printf("Even numbers:");
queuePrint(even_queue);
queuePrintReverse(even_queue);
printf("Odd numbers:");
queuePrint(odd_queue);
queuePrintReverse(odd_queue);
   printf("Bye ");
   // system("pause"); Uncomment this to see the output in window
}

void queueInit(queue * qp){
   qp->first = NULL;
   qp->last = NULL;
}

void queueInsert (queue * qp, int t){

   ListNode * n = (ListNode *) malloc(sizeof(ListNode));
   if (n == NULL) {
       printf("Out of memory ");
       exit(1);
   }
   n->data = t;
   n->next = NULL;
   n->prev = NULL;
   if (qp->last == NULL)
       qp->first = qp->last = n;
   else {
       qp->last->next = n;
       qp->last->prev = qp->last;
       qp->last = n;
   }
}


void queuePrint (queue q){


   ListNode * n;
   for (n = q.first; n != NULL; n = n->next) {
       printf(" %d ", n->data);
   }
}

Section 1 Compile and run the code that it is provided to you (queue.c. What does it do? Make all the necessary changes so that the program creates two queues. One queue for odd numbers and another for even numbers. Each queue stores the number that the user enters (e.g. if the user enters 2 it stores this data to the even queue, if the user types 3 it stores this number to the odd queue etc.). Print the two queues separately by using the queuePrint function. Section 2 (a) Modify the provided code (queue.c in order to print the queue in the reverse order. (b) Modify the provided code (queue.c) so as to use a cyclist list instead of a single linked list. Section 3

Explanation / Answer

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

typedef struct list_tag {
    int data;
    struct list_tag * next;
    struct list_tag * prev;
} ListNode;

typedef struct {
    ListNode * first;
    ListNode * last;
} queue;

void queueInsert (queue * qp, int t);
void queuePrint (queue q);
void queuePrintReverse(queue q);
void queueInit(queue * qp);


void main(){

    queue odd_queue;
    queue even_queue;
    int data;
    queueInit(&odd_queue); //Initialize the queue as empty
    queueInit(&even_queue); //Initialize the queue as empty

    printf("Give me numbers. 0 = exit ");
    scanf("%d",&data);


    while (data != 0){
            if(data%2==0)
            {
        queueInsert(&even_queue, data);
            }
            else
            {
                queueInsert(&odd_queue, data);

            }
        scanf("%d",&data);
    }
    printf("Even numbers:");
    queuePrint(even_queue);
    printf("Reverse ");
    queuePrintReverse(even_queue);
    printf("Odd numbers:");
    queuePrint(odd_queue);
    printf("Reverse ");
    queuePrintReverse(odd_queue);
    printf("Bye ");
    // system("pause"); Uncomment this to see the output in window
}
//function to print queue in reverse order...
void queuePrintReverse(queue q)
{
   //printing reverse of the queue
   ListNode * n;
    for (n = q.last; n != q.first; n = n->prev) {
        printf(" %d ", n->data);
    }
    printf(" %d ", q.first->data);
}

void queueInit(queue * qp){
    qp->first = NULL;
    qp->last = NULL;
}

void queueInsert (queue * qp, int t){

    ListNode * n = (ListNode *) malloc(sizeof(ListNode));
    if (n == NULL) {
        printf("Out of memory ");
        exit(1);
    }
    n->data = t;
    n->next = NULL;
    n->prev = NULL;
    if (qp->last == NULL)
        qp->first = qp->last = n;
    else {
        qp->last->next = n;
        //qp->last->prev = qp->last;
        n->prev = qp->last;
        qp->last = n;
        //making circular lisk..
        n->next=qp->first;
        qp->first->prev = n;
    }
}


void queuePrint (queue q){


    ListNode * n;
    for (n = q.first; n != q.last; n = n->next) {
        printf(" %d ", n->data);
    }
     printf(" %d ", q.last->data);
}

ouput:-

Give me numbers. 0 = exit
1
2
3
4
5
0
Even numbers:
2

4
Reverse

4

2
Odd numbers:
1

3

5
Reverse

5

3

1
Bye


Process exited normally.
Press any key to continue . . .