In: Computer Science
#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
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!