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"...
I need the following C code converted to java or C++ #include <stdio.h> #include <stdlib.h> typedef...
I need the following C code converted to java or C++ #include <stdio.h> #include <stdlib.h> typedef struct node { struct node *left; struct node *right; long data; long leftSize; } node; void btreeInsert(node *new, node **rootptr) { node *parent = NULL, *cursor; /* Find parent */ cursor = *rootptr; while (cursor != NULL) { parent = cursor; if (new->data < cursor->data) { cursor->leftSize += 1; cursor = cursor->left; } else { cursor = cursor->right; } } /* Insert node below...
example_thread.c #include <stdio.h> #include <stdlib.h> #include <pthread.h> int shared= 0; void race(void); int main(){     pthread_t...
example_thread.c #include <stdio.h> #include <stdlib.h> #include <pthread.h> int shared= 0; void race(void); int main(){     pthread_t player1, player2, player3;     pthread_create(&player1, NULL, (void *)race, NULL);     pthread_create(&player2, NULL, (void *)race, NULL);     pthread_create(&player3, NULL, (void *)race, NULL);     pthread_join(player1, NULL);     pthread_join(player2, NULL);     pthread_join(player3, NULL);     printf("Total Number = %d\n", shared);     return 0; } void race(void) {     long i,tmp;     for(i=1; i<=200000; i++) {         tmp = shared;         tmp = tmp + 1;         shared =...
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...
How to reverse linked list below,thank you! #include <stdlib.h> #include <stdio.h> struct list { int data;...
How to reverse linked list below,thank you! #include <stdlib.h> #include <stdio.h> struct list { int data; struct list *next; }; typedef struct list node; typedef node *link; int main() { link ptr,head; int num,i; head = ( link ) malloc(sizeof(node)); ptr = head; printf("enter 10 data \n"); for ( i = 0; i <= 9; i++ ) { scanf("%d",&num); ptr->data = num; ptr->next = ( link ) malloc(sizeof(node)); if ( i == 9 ) ptr->next = NULL; else ptr =...
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...
#include <stdio.h> #include <stdlib.h> #include <math.h> #include "../include/cis1057.h" /* * Programmer: << MA >> * Class:...
#include <stdio.h> #include <stdlib.h> #include <math.h> #include "../include/cis1057.h" /* * Programmer: << MA >> * Class: Introduction to C Programming 1057 Spring 2019 Section 004 * Assignment: Number 5 “estimate the value of a factorial using Gosper's algorithm." * Date: << 02-19-2019 >> * Version: 1 * Description: Program will prompt for some data, read in the values, perform the calculation using library math functions, and display the result. * File: lab5.c */ # define M_PI 3.14159265358979323846 /* pi */...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT