Question

In: Computer Science

//Source: Gilberg et al., Data Structures: A Pseudocode Approach with C //Queue ADT Type Defintions typedef...

//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

Write a program that generates10 random numbersbetween 1and 100 (both inclusive)and enqueue them to the Queue. Then, traverse the queueto find all odd numbers, and make them even by adding one. Print the original and the modified Queueelements.Example:Original Queue: 2,24,5,13,2,65,34,87,60,23Modified Queue: 2,24,6,14,2,66,34,88,60,24

please in c progamming

Solutions

Expert Solution

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

// function to generate random number in [1-100]
int random1(){
return (rand()%(100-1+1))+1;
}
int main()
{
  
QUEUE *p=createQueue();
int c=0;
// generate random 10 mumbers in [1-100] and put them in queue
while(c<10){
enqueue(p,random1());
c++;
}
QUEUE *q=createQueue();
int a;
void *pp=&a;
  
// dequeue from queue and put them in new queue
printf("Original Queue: ");
while(emptyQueue(p)==false){
dequeue(p,pp);
if(queueCount(p)>0){
printf("%d,",*(int *)pp);
}
if(a%2!=0)// if number is odd then make it even and put it in new queue
enqueue(q,a+1);
else
enqueue(q,a);
  
}
printf("%d\n",*(int *)pp);
// print contents of the final queue made
printf("Modified Queue: ");
while(emptyQueue(q)==false){
dequeue(q,pp);
if(queueCount(q)>0){
printf("%d,",*(int *)pp);
}
}
printf("%d",*(int *)pp);
return 0;
}



Related Solutions

//Source: Gilberg et al., Data Structures: A Pseudocode Approach with C //Queue ADT Type Defintions typedef...
//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...
#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...
In C++, Implement the queue ADT with a singly linked list
In C++, Implement the queue ADT with a singly linked list
Using the linked list abstract data type “Queue ADT”, write a menu dirven user interfece to...
Using the linked list abstract data type “Queue ADT”, write a menu dirven user interfece to teach each of the operations in the ADT. Any errors discovered during the processing should be printed as a part of the test result. Please Use C++ language.
In this lab, using C++, you will create two data structures: a stack and a queue....
In this lab, using C++, you will create two data structures: a stack and a queue. You will use STL containers to demonstrate basic ADTs. Queue For the queue, you will simulate a buffer. Remember it is first-in-first-out. The user will enter a number for the number of rounds to run your simulation. You need one function that randomly generates a number. You will also have a user specified percentage, and the function uses this percentage to randomly put the...
In C++, Design and implement an ADT that represents a triangle. The data for the ADT...
In C++, Design and implement an ADT that represents a triangle. The data for the ADT should include the three sides of the triangle but could also include the triangle’s three angles. This data should be in the private section of the class that implements the ADT. Include at least two initialization operations: one that provides default values for the ADT’s data, and another that sets this data to client-supplied values. These operations are the class’s constructors. The ADT also...
C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you...
C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you will implement the functionality of a stack and a queue using a linked list. Your program must use of the declaration of the Stack and Queue class in Stack.h and Queue.h You have to implement the functionalities of queue (enq, deq, displayQueue) in a file called Queue.cpp. All the functions in Queue.cpp should follow the prototypes declared in Queue.h. Your code should make use...
Program Task (C++) The program should contain an abstract data type (ADT) for an Employee, defined...
Program Task (C++) The program should contain an abstract data type (ADT) for an Employee, defined as follows: struct Employee int id; // unique employee identifier string name; // employee’s full name double rate; // employee’s hourly rate double hours; // how many hours they worked since last pay double taxable; // the current year’s taxable income }; This definition should appear above the main() function. The main() function should include: 1. Declare a single array to hold at most...
Read the “Evaluating the Quality of Open Source Software” article (Spinellis, et al., 2009). What are...
Read the “Evaluating the Quality of Open Source Software” article (Spinellis, et al., 2009). What are the strengths and weaknesses of this particular study? (For example, does the study do an adequate job of collecting and analyzing representative samples?) What would you suggest that the authors do to remedy those weaknesses and why? Use both your text and the article An Introduction to Research Design (Ganster, 2003). You may also draw on any book, article, website, or other reliable resource...
According to Schreiber et al 2019, in a stepwise approach to early mobility what level would...
According to Schreiber et al 2019, in a stepwise approach to early mobility what level would a patient have to reach to most successfully wean from prolonged mechanical ventilation?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT