Question

In: Computer Science

I am to write a code that uses two queues, and print the generated random numbers...

I am to write a code that uses two queues, and print the generated random numbers and content of both queues. I need to use the enqueue and dequeue functions in the code.  The instructions are below. Program should be in C

Instructions:

Generate n random numbers with values between 10 - 100. Note: n>9

if u write a function (for example, called generateRand) to do this - what is the data or input we have to give it?

  1. Create a queue with all these numbers in it.
    Hint: length of queue = n

  2. Iterate through Queue1 - for each item , calculate sum of digits, and if sum of digits is odd -> move it to Queue2
    move means enqueue to Queue2 + remove only that value from Queue1

  3. Print what is asked

We are to use the following header file to write the code as well .

//Source: Gilberg et al., Data Structures: A Pseudocode Approach with C
//Queue ADT Type Defintions
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


Solutions

Expert Solution

Here is Solution for above Question :

#include <stdio.h>
#include <stdio.h> 
#include <stdlib.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);


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

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

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


int queueFront (QUEUE* queue, void** itemPtr)
{
//Statements
if (!queue->count)
return 0;
else
{
*itemPtr = queue->front->dataPtr;
return 1;
} // else
} // queueFront

int queueRear (QUEUE* queue, void** itemPtr)
{
//Statements
if (!queue->count)
return 0;
else
{
*itemPtr = queue->rear->dataPtr;
return 1;
} // else
} // queueRear

int emptyQueue (QUEUE* queue)
{
//Statements
return (queue->count == 0);
} // emptyQueue

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

int queueCount(QUEUE* queue)
{
//Statements
return queue->count;
} // queueCount

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()
{
     QUEUE* queue1;
     QUEUE* queue2;
     
     queue1=createQueue();
     queue2=createQueue();
    int n;
    
    scanf("%d",&n);
     int a[n];
     printf("Enter the value of n = %d\n",n);
     for (int i = 0; i <n; i++)
     { 
        int num = (rand()%(100- 10 + 1)) + 10; 
        a[i]=num;
        printf("%d ", num); 
        printf("Inserting into Queue1 successful %d!!\n",enqueue(queue1,&num));
     }
 
     printf("Insertion in Queue1 is Complete!!\n");
     
     for(int j=0;j<n;j++)
     { 
         int m,sum=0;
         int p=a[j];
          while(a[j]>0)    
         {    
           m=a[j]%10;    
           sum=sum+m;    
           a[j]=a[j]/10;    
         }
         
            
           // printf("%d ", x); 
            if(sum%2!=0)
            {
            printf("%d ",p);            
            printf("Inserting into Queue2 successful %d!!\n",enqueue(queue2,&p));
             printf("%d ",p);  
            printf("Dequeue From Queue1 %d!!\n", dequeue(queue1,&p));
            
            }
         
     }
     
    return 0;
}

Test Cases :

Input :

13

Output :

Enter the value of n = 13
88 Inserting into Queue1 successful 1!!
84 Inserting into Queue1 successful 1!!
47 Inserting into Queue1 successful 1!!
92 Inserting into Queue1 successful 1!!
11 Inserting into Queue1 successful 1!!
76 Inserting into Queue1 successful 1!!
48 Inserting into Queue1 successful 1!!
26 Inserting into Queue1 successful 1!!
11 Inserting into Queue1 successful 1!!
69 Inserting into Queue1 successful 1!!
12 Inserting into Queue1 successful 1!!
78 Inserting into Queue1 successful 1!!
23 Inserting into Queue1 successful 1!!
Insertion in Queue1 is Complete!!
47 Inserting into Queue2 successful 1!!
47 Dequeue From Queue1 1!!
92 Inserting into Queue2 successful 1!!
92 Dequeue From Queue1 1!!
76 Inserting into Queue2 successful 1!!
76 Dequeue From Queue1 1!!
69 Inserting into Queue2 successful 1!!
69 Dequeue From Queue1 1!!
12 Inserting into Queue2 successful 1!!
12 Dequeue From Queue1 1!!
78 Inserting into Queue2 successful 1!!
78 Dequeue From Queue1 1!!
23 Inserting into Queue2 successful 1!!
23 Dequeue From Queue1 1!!

Related Solutions

Write assembler code to print even numbers from 1-20. Submit code and screenshot. I am coding...
Write assembler code to print even numbers from 1-20. Submit code and screenshot. I am coding in NASM on a SASM system.
For this problem, you will write a program using two queues. Generate n random numbers between...
For this problem, you will write a program using two queues. Generate n random numbers between 10 and 100 (both inclusive), where n>9. The value of n should be taken as input from the user and n should be >9. The numbers could be duplicated. Enqueue all these numbers 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...
Examples Example 1: Write pseudo code that reads two numbers and multiplies them together and print...
Examples Example 1: Write pseudo code that reads two numbers and multiplies them together and print out their product. Example 2: Write pseudo code that tells a user that the number they entered is not a 5 or a 6. Example 3: Write pseudo code that performs the following: Ask a user to enter a number. If the number is between 0 and 10, write the word blue. If the number is between 10 and 20, write the word red....
I need it in java. Write a program that will print if n numbers that the...
I need it in java. Write a program that will print if n numbers that the user will input are or not within a range of numbers. For that, your program needs to ask first for an integer number called N that will represent the number of times that will ask for other integer numbers. Right after, it should ask for two numbers that will represent the Min and Max for a range. Lastly. it will iterate N number times...
I am trying to sort a group of 10 randomly generated numbers from least to greatest...
I am trying to sort a group of 10 randomly generated numbers from least to greatest in c++. Right now, my code isn't working and is giving odd results. There is extra unecessary code since I was trying to find out what wasn't working. In this example I am trying to use the bubble sort method but bucket sort would also be okay. Please keep the format the same and don't use any other functions not currently in the code,...
I need solution of this homework Question: Queues, Deques and Priority Queues. Enter the necessary code
I need solution of this homework Question: Queues, Deques and Priority Queues. Enter the necessary code
Write a c++ code that prompts the user to enter three float numbers and print them...
Write a c++ code that prompts the user to enter three float numbers and print them in acsending order
Write an Arduino code that does the following. Generate 50 random numbers between the numbers 100...
Write an Arduino code that does the following. Generate 50 random numbers between the numbers 100 and 300. Pick a number at random out of these 50 random variables. a. Determine the probability of the chosen number being greater than 200. This may be achieved by counting the numbers that are greater than 200 and dividing the count by 50. Make sure you, i.Formulate the appropriate if-conditions to check for a number being greater than 200 ii. Use a for-loop...
I am trying to write a code in C for a circuit board reads in a...
I am trying to write a code in C for a circuit board reads in a temperature from a sensor on the circuit board and reads it onto a 7-segment LED display. How exactly would you code a floating 2 or 3 digit reading onto the LED display? I am stuck on this part.
Palindrome Javascript - NUMBERS I am trying to write this javascript function to check if a...
Palindrome Javascript - NUMBERS I am trying to write this javascript function to check if a number is Palindrome.. Palindrome means - a word, phrase, or sequence that reads the same backward as forward, e.g., 12321 This is the code i have, but it doesnt work. PLEASE MAKE SURE IT WORK BEFORE POST ANSWER, I GOT @ CODE THAT DID WORK BEFORE HTML JavaScript Palindrome Exercise rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.1/css/bootstrap.min.css" /> Palindrome Enter a positive number: Is this a palindrome? No...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT