Question

In: Computer Science

#include<stdlib.h> #include<stdio.h> typedef struct node {    void* dataPtr;    struct node* next; } QUEUE_NODE; typedef...

#include<stdlib.h>
#include<stdio.h>
typedef struct node
{
   void* dataPtr;
   struct node* next;
} QUEUE_NODE;

typedef struct
{
   QUEUE_NODE* front;
   QUEUE_NODE* rear;
   int count;
} QUEUE;

//Prototype Declarations
QUEUE* createQueue(void);
QUEUE* destroyQueue(QUEUE* queue);
bool dequeue(QUEUE* queue, void** itemPtr);
bool enqueue(QUEUE* queue, void* itemPtr);
bool queueFront(QUEUE* queue, void** itemPtr);
bool queueRear(QUEUE* queue, void** itemPtr);
int queueCount(QUEUE* queue);
bool emptyQueue(QUEUE* queue);
bool fullQueue(QUEUE* queue);


/*================= createQueue ================
Allocates memory for a queue head node from dynamic
memory and returns its address to the caller.
Pre nothing
Post head has been allocated and initialized
Return head if successful; null if overflow
*/
QUEUE* createQueue(void)
{
   //Local Definitions
   QUEUE* queue;
   //Statements
   queue = (QUEUE*)malloc(sizeof(QUEUE));
   if (queue)
   {
       queue->front = NULL;
       queue->rear = NULL;
       queue->count = 0;
   } // if
   return queue;
} // createQueue

/*================= enqueue ================
This algorithm inserts data into a queue.
Pre queue has been created
Post data have been inserted
Return true if successful, false if overflow
*/
bool enqueue(QUEUE* queue, void* itemPtr)
{
   //Local Definitions
   QUEUE_NODE* newPtr;
   //Statements
   if (!(newPtr =
       (QUEUE_NODE*)malloc(sizeof(QUEUE_NODE))))
       return false;
   newPtr->dataPtr = itemPtr;
   newPtr->next = NULL;
   if (queue->count == 0)
       // Inserting into null queue
       queue->front = newPtr;
   else
       queue->rear->next = newPtr;
   (queue->count)++;
   queue->rear = newPtr;
   return true;
} // enqueue

/*================= dequeue ================
This algorithm deletes a node from the queue.
Pre queue has been created
Post Data pointer to queue front returned and
front element deleted and recycled.
Return true if successful; false if underflow
*/
bool dequeue(QUEUE* queue, void** itemPtr)
{
   //Local Definitions
   QUEUE_NODE* deleteLoc;
   //Statements
   if (!queue->count)
       return false;
   *itemPtr = queue->front->dataPtr;
   deleteLoc = queue->front;
   if (queue->count == 1)
       // Deleting only item in queue
       queue->rear = queue->front = NULL;
   else
       queue->front = queue->front->next;
   (queue->count)--;
   free(deleteLoc);
   return true;
} // dequeue

/*================== queueFront =================
This algorithm retrieves data at front of the
queue without changing the queue contents.
Pre queue is pointer to an initialized queue
Post itemPtr passed back to caller
Return true if successful; false if underflow
*/
bool queueFront(QUEUE* queue, void** itemPtr)
{
   //Statements
   if (!queue->count)
       return false;
   else
   {
       *itemPtr = queue->front->dataPtr;
       return true;
   } // else
} // queueFront

/*================== queueRear =================
Retrieves data at the rear of the queue
without changing the queue contents.
Pre queue is pointer to initialized queue
Post Data passed back to caller
Return true if successful; false if underflow
*/
bool queueRear(QUEUE* queue, void** itemPtr)
{
   //Statements
   if (!queue->count)
       return false;
   else
   {
       *itemPtr = queue->rear->dataPtr;
       return true;
   } // else
} // queueRear

/*================== emptyQueue =================
This algorithm checks to see if queue is empty.
Pre queue is a pointer to a queue head node
Return true if empty; false if queue has data
*/
bool emptyQueue(QUEUE* queue)
{
   //Statements
   return (queue->count == 0);
} // emptyQueue

/*================== fullQueue =================
This algorithm checks to see if queue is full. It
is full if memory cannot be allocated for next node.
Pre queue is a pointer to a queue head node
Return true if full; false if room for a node
*/
bool fullQueue(QUEUE* queue)
{
   //Local Definitions
   QUEUE_NODE* temp;
   //Statements
   temp = (QUEUE_NODE*)malloc(sizeof(*(queue->rear)));
   if (temp)
   {
       free(temp);
       return true;
   } // if
   // Heap full
   return false;
} // fullQueue

/*================== queueCount =================
Returns the number of elements in the queue.
Pre queue is pointer to the queue head node
Return queue count
*/
int queueCount(QUEUE* queue)
{
   //Statements
   return queue->count;
} // queueCount

/*================== destroyQueue =================
Deletes all data from a queue and recycles its
memory, then deletes & recycles queue head pointer.
Pre Queue is a valid queue
Post All data have been deleted and recycled
Return null pointer
*/
QUEUE* destroyQueue(QUEUE* queue)
{
   //Local Definitions
   QUEUE_NODE* deletePtr;
   //Statements
   if (queue)
   {
       while (queue->front != NULL)
       {
           free(queue->front->dataPtr);
           deletePtr = queue->front;
           queue->front = queue->front->next;
           free(deletePtr);
       } // while
       free(queue);
   } // if
   return NULL;
} // destroyQueue

QUEUES:For this problem, you will write a program using two queues. Generate nrandom numbersbetween 10 and 100 (both inclusive), where n>9. The numbers could be duplicated. Enqueue all thesenumbers to the first queue. The objective is to find the numbers whose sum of digits is odd and enqueue them to the second queue. The remaining numbers (whose sum of digits is even) should stay in the first queue.You need to print the generated random numbers and content of both queues IN C LANGUAGE

Solutions

Expert Solution

What i have changed in above code?

I have changed all bool return types of functions to int as bool is not a dataype in C language. Also I have changed return true to return 1 and return false to return 0.

What I have added to above code?

In main method, firstly user will be asked to input a number n > 9.

Then we will generate n random numbers between 10 to 100(both inclusive).

We will then display the content of our generated array and then enqueue all elements to queue 1.

Then we will check each element of queue 1 by first dequeuing that element and checking for its sum.

If its sum of digits is odd then enqueue that elemnt to queue 2.

if sum of its digits is even, enqueue back the element to queue 1.

After processing all elements of queue 1, print the contents of both queues.

We should have all elements with odd sum of digits in queue 2 and elements with even sum of digits in queue 2.

C program for above problem:

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

typedef struct node
{
   void* dataPtr;
   struct node* next;
} QUEUE_NODE;

typedef struct
{
   QUEUE_NODE* front;
   QUEUE_NODE* rear;
   int count;
} QUEUE;

//Prototype Declarations
QUEUE* createQueue(void);
QUEUE* destroyQueue(QUEUE* queue);
int dequeue(QUEUE* queue, void** itemPtr);
int enqueue(QUEUE* queue, void* itemPtr);
int queueFront(QUEUE* queue, void** itemPtr);
int queueRear(QUEUE* queue, void** itemPtr);
int queueCount(QUEUE* queue);
int emptyQueue(QUEUE* queue);
int fullQueue(QUEUE* queue);


/*================= createQueue ================
Allocates memory for a queue head node from dynamic
memory and returns its address to the caller.
Pre nothing
Post head has been allocated and initialized
Return head if successful; null if overflow
*/
QUEUE* createQueue(void)
{
   //Local Definitions
   QUEUE* queue;
   //Statements
   queue = (QUEUE*)malloc(sizeof(QUEUE));
   if (queue)
   {
       queue->front = NULL;
       queue->rear = NULL;
       queue->count = 0;
   } // if
   return queue;
} // createQueue

/*================= enqueue ================
This algorithm inserts data into a queue.
Pre queue has been created
Post data have been inserted
Return true if successful, false if overflow
*/
int enqueue(QUEUE* queue, void* itemPtr)
{  
   //Local Definitions
   QUEUE_NODE* newPtr;
   //Statements
   if (!(newPtr =
       (QUEUE_NODE*)malloc(sizeof(QUEUE_NODE))))
       return 0;
   newPtr->dataPtr = itemPtr;
   newPtr->next = NULL;
   if (queue->count == 0)
       // Inserting into null queue
       queue->front = newPtr;
   else
       queue->rear->next = newPtr;
   (queue->count)++;
   queue->rear = newPtr;
   return 1;
} // enqueue

/*================= dequeue ================
This algorithm deletes a node from the queue.
Pre queue has been created
Post Data pointer to queue front returned and
front element deleted and recycled.
Return true if successful; false if underflow
*/
int dequeue(QUEUE* queue, void** itemPtr)
{
   //Local Definitions
   QUEUE_NODE* deleteLoc;
   //Statements
   if (!queue->count)
       return 0;
   *itemPtr = queue->front->dataPtr;
   deleteLoc = queue->front;
   if (queue->count == 1)
       // Deleting only item in queue
       queue->rear = queue->front = NULL;
   else
       queue->front = queue->front->next;
   (queue->count)--;
   free(deleteLoc);
   return 1;
} // dequeue

/*================== queueFront =================
This algorithm retrieves data at front of the
queue without changing the queue contents.
Pre queue is pointer to an initialized queue
Post itemPtr passed back to caller
Return true if successful; false if underflow
*/
int queueFront(QUEUE* queue, void** itemPtr)
{
   //Statements
   if (!queue->count)
       return 0;
   else
   {
       *itemPtr = queue->front->dataPtr;
       return 1;
   } // else
} // queueFront

/*================== queueRear =================
Retrieves data at the rear of the queue
without changing the queue contents.
Pre queue is pointer to initialized queue
Post Data passed back to caller
Return true if successful; false if underflow
*/
int queueRear(QUEUE* queue, void** itemPtr)
{
   //Statements
   if (!queue->count)
       return 0;
   else
   {
       *itemPtr = queue->rear->dataPtr;
       return 1;
   } // else
} // queueRear

/*================== emptyQueue =================
This algorithm checks to see if queue is empty.
Pre queue is a pointer to a queue head node
Return true if empty; false if queue has data
*/
int emptyQueue(QUEUE* queue)
{
   //Statements
   if(queue->count == 0)
    return 1;
   return 0;
} // emptyQueue

/*================== fullQueue =================
This algorithm checks to see if queue is full. It
is full if memory cannot be allocated for next node.
Pre queue is a pointer to a queue head node
Return true if full; false if room for a node
*/
int fullQueue(QUEUE* queue)
{
   //Local Definitions
   QUEUE_NODE* temp;
   //Statements
   temp = (QUEUE_NODE*)malloc(sizeof(*(queue->rear)));
   if (temp)
   {
       free(temp);
       return 1;
   } // if
   // Heap full
   return 0;
} // fullQueue

/*================== queueCount =================
Returns the number of elements in the queue.
Pre queue is pointer to the queue head node
Return queue count
*/
int queueCount(QUEUE* queue)
{
   //Statements
   return queue->count;
} // queueCount

/*================== destroyQueue =================
Deletes all data from a queue and recycles its
memory, then deletes & recycles queue head pointer.
Pre Queue is a valid queue
Post All data have been deleted and recycled
Return null pointer
*/
QUEUE* destroyQueue(QUEUE* queue)
{
   //Local Definitions
   QUEUE_NODE* deletePtr;
   //Statements
   if (queue)
   {
       while (queue->front != NULL)
       {
           free(queue->front->dataPtr);
           deletePtr = queue->front;
           queue->front = queue->front->next;
           free(deletePtr);
       } // while
       free(queue);
   } // if
   return NULL;
} // destroyQueue


int main(){

    //seeding time for radndom generation
    srand(time(0)); 

    //making two queues
    QUEUE *q1 = createQueue();
    QUEUE *q2 = createQueue();

    int n;

    printf("\nEnter the numbers you wants to be generated: ");
    scanf("%d", &n);

    //if user inputs value of n less than or equal to 9
    while(n <= 9){
        printf("\nInvalid input!\nPlease enter a number greater than 9: ");
        scanf("%d", &n);
    }

    //create an array of size n
    int arr[n];
    for(int i = 0; i < n; i++){

        //this will give number between 0 to 100
        arr[i] = rand() % 101;

        //if the generated number is one digit number
        if(arr[i]/10 == 0){

            //add 10 to it
            arr[i] += 10;
        }
    }

    printf("\nOur array is: \n");

    //print the randomly generated numbers
    for(int i = 0; i < n; i++){
        printf("%d ", arr[i]);
    }
    printf("\n");


    //enqueue all array elements to queue 1
    for(int i = 0; i < n; i++)
        enqueue(q1, (void*)&arr[i]);


    int el_count = n;
    
    //check all elements of first queue
    while(el_count--){

        int* front_element;

        //get the front element of queue1
        dequeue(q1,(void*)&front_element);

        
        int sum_of_digits = 0;
        sum_of_digits += (*front_element) % 10;
        sum_of_digits += (*front_element/10) % 10;

        //check if its sum of digits is odd
        if(sum_of_digits % 2 == 1){

            //enqueue this element to queue 2
            enqueue(q2, (void*)front_element);
        }
        //if its sum of digits is even
        else{

            //enqueue this element back to queue1
            enqueue(q1, (void*)front_element);
        }
        
    }

    //print the contents of first queue
    el_count = queueCount(q1);


    printf("\nElements in Queue 1: \n");
    while(el_count--){
        int* front_element;

        //get the front element of queue1
        dequeue(q1,(void*)&front_element);

        printf("%d ", *front_element);

        //pushing back the element to queue back
        enqueue(q1, (void*)&front_element);
    }

    printf("\n");


    //print the contents of second queue
    el_count = queueCount(q2);


    printf("\nElements in Queue 2: \n");
    while(el_count--){
        int* front_element;

        //get the front element of queue1
        dequeue(q2, (void*)&front_element);

        printf("%d ", *front_element);

        //pushing back the element to queue back
        enqueue(q2, (void*)&front_element);
    }

    printf("\n");

    return 0;
}

Output:

if it helps you, do upvote as it motivates us a lot!


Related Solutions

Please debug the code and answer the questions: #include <stdio.h> typedef struct node { int value;...
Please debug the code and answer the questions: #include <stdio.h> typedef struct node { int value; struct node *next; } node; int ll_has_cycle(node *first) { node * head = first; while (head->next) { head = head->next; if (head == first) return 1; } return 0; } void test_ll_has_cycle(void) { int i,j; node nodes[5]; for(i=0; i < sizeof(nodes)/sizeof(node); i++) { nodes[i].next = NULL; nodes[i].value = i; } nodes[0].next = &nodes[1]; nodes[1].next = &nodes[2]; nodes[2].next = &nodes[1]; printf("Checking first list for cycles....
#include #include #include int reverse(int); // Stack ADT Type Defintions typedef struct node { void* dataPtr;...
#include #include #include int reverse(int); // Stack ADT Type Defintions typedef struct node { void* dataPtr; struct node* link; } STACK_NODE; typedef struct { int count; STACK_NODE* top; } STACK; /* =============== createStack ============== This algorithm creates an empty stack. Pre Nothing Post Returns pointer to a null stack -or- NULL if overflow */ STACK* createStack(void) { // Local Definitions STACK* stack; // Statements stack = (STACK*)malloc(sizeof(STACK)); if (stack) { stack->count = 0; stack->top = NULL; } // if return...
#include <stdio.h> typedef struct Coordinates { int x; int y; } Coordinate; typedef union uCoordinates {...
#include <stdio.h> typedef struct Coordinates { int x; int y; } Coordinate; typedef union uCoordinates { int x; int y; } uCoordinate; // TODO - Populate 4 different Coordinates with any numbers between 1 and 99 for x & y values // using coordinate1, coordinate2, coordinate3, & coordinate4 as Coordinate names // TODO - Print to screen the x & y values of each coordinate // TODO - Replace the following with your name: Ethin Svoboda int main() { //...
Below is for C language typedef struct _node { Node *next; char *str; } Node; You...
Below is for C language typedef struct _node { Node *next; char *str; } Node; You are given a function that takes in two Node* pointers to two different linked lists. Each linked list node has a string represented by a character array that is properly null terminated. You're function is supposed to return true if the concatenation of strings in a linked list match each other, else return false. EXAMPLES List 1: "h" -> "el" -> "l" -> "o"...
Can anyone change it to double linked list #include<stdio.h> #include<stdlib.h> #include <iostream> using namespace std; struct...
Can anyone change it to double linked list #include<stdio.h> #include<stdlib.h> #include <iostream> using namespace std; struct Node {     int data;     struct Node* next; }; void printMiddle(struct Node *head) {     struct Node *slow_ptr = head;     struct Node *fast_ptr = head;     if (head!=NULL)     {         while (fast_ptr != NULL && fast_ptr->next != NULL)         {             fast_ptr = fast_ptr->next->next;             slow_ptr = slow_ptr->next;         }         printf("The middle element is [%d]\n\n", slow_ptr->data);     } } void...
Assume that struct Node { int item; Node* link; }; typedef Node* NodePtr; 1. Write function...
Assume that struct Node { int item; Node* link; }; typedef Node* NodePtr; 1. Write function void list_head_insert(NodePtr& head, int entry); The function should insert a new Node, in which entry is the value of attribute item, in front of the linked list that is pointed by head. 2. Write function void list_head_remove(NodePtr& head); The function will remove the first node from the linked list that is pointed by head. 3. Write function NodePtr list_search(NodePtr head, int target); The function...
(12) Explain what will be output of the following program? #include <stdio.h> #include <stdlib.h> #include <pthread.h>...
(12) Explain what will be output of the following program? #include <stdio.h> #include <stdlib.h> #include <pthread.h> #define NUM_THREADS 3 /* create thread argument struct for thr_func() */ typedef struct _thread_data_t {   int tid;   double stuff; } thread_data_t; /* thread function */ void *thr_func(void *arg) {   thread_data_t *data = (thread_data_t *)arg;   printf("hello from thr_func, thread id: %d\n", data->tid);   pthread_exit(NULL); } int main(int argc, char **argv) {   pthread_t thr[NUM_THREADS];   int i, rc;   thread_data_t thr_data[NUM_THREADS];   /* create threads */   for (i = 0;...
Assume that struct Node{        int item;        Node* link; }; Write function void list_remove(NodePtr& prev_ptr);...
Assume that struct Node{        int item;        Node* link; }; Write function void list_remove(NodePtr& prev_ptr); The function will remove the node after the node pointed by prev_ptr. c++
#include <stdio.h> #include <stdlib.h> #define K   1024 /** (2pts) * This problem is like p1, except...
#include <stdio.h> #include <stdlib.h> #define K   1024 /** (2pts) * This problem is like p1, except that you should read the number using scanf() * and the string to print using fgets() (up to 1024 characters.) Use "num: " as * the prompt for the number and "str: " as the prompt for the string. Keep in * mind that the newline that is entered will still be in the string when * printing it. NOTE: After the scanf() for...
#include <stdio.h> #include <stdlib.h> int play_game(int *); // Returns 0 if player won, 1 if the...
#include <stdio.h> #include <stdlib.h> int play_game(int *); // Returns 0 if player won, 1 if the computer won, 2 if there is a tie, and -1 if the player decides to quit int menu(int *); // Displays choices to user // Receives score array int main() { srand(42); // Seeding Random with 42 int score[3]; // Array keeping Player, Computer, and Tie Scores score [0] = 0; // Player - initialized to Zero score [1] = 0; // Computer -...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT