Question

In: Computer Science

C++ Modify this to use a separate Boolean member to keep track of whether the queue...

C++

Modify this to use a separate Boolean member to keep track of whether the queue is empty rather than require that one array position remain empty.

#include <stdio.h>

#include <stdlib.h>

#include <limits.h>

// A structure to represent a queue

struct Queue

{

int front, rear, size;

unsigned capacity;

int* array;

};

// function to create a queue of given capacity.

// It initializes size of queue as 0

struct Queue* createQueue(unsigned capacity)

{

struct Queue* queue = (struct Queue*) malloc(sizeof(struct Queue));

queue->capacity = capacity;

queue->front = queue->size = 0;

queue->rear = capacity - 1; // This is important, see the enqueue

queue->array = (int*) malloc(queue->capacity * sizeof(int));

return queue;

}

// Queue is full when size becomes equal to the capacity

int isFull(struct Queue* queue)

{ return (queue->size == queue->capacity); }

// Queue is empty when size is 0

int isEmpty(struct Queue* queue)

{ return (queue->size == 0); }

// Function to add an item to the queue.

// It changes rear and size

void enqueue(struct Queue* queue, int item)

{

if (isFull(queue))

return;

queue->rear = (queue->rear + 1)%queue->capacity;

queue->array[queue->rear] = item;

queue->size = queue->size + 1;

printf("%d enqueued to queue\n", item);

}

// Function to remove an item from queue.

// It changes front and size

int dequeue(struct Queue* queue)

{

if (isEmpty(queue))

return INT_MIN;

int item = queue->array[queue->front];

queue->front = (queue->front + 1)%queue->capacity;

queue->size = queue->size - 1;

return item;

}

// Function to get front of queue

int front(struct Queue* queue)

{

if (isEmpty(queue))

return INT_MIN;

return queue->array[queue->front];

}

// Function to get rear of queue

int rear(struct Queue* queue)

{

if (isEmpty(queue))

return INT_MIN;

return queue->array[queue->rear];

}

// Driver program to test above functions./

int main()

{

struct Queue* queue = createQueue(1000);

enqueue(queue, 210);

enqueue(queue, 111);

enqueue(queue, 121);

enqueue(queue, 122);

enqueue(queue, 134);

enqueue(queue, 144);

printf("%d dequeued from queue\n\n", dequeue(queue));

printf("Front item is %d\n", front(queue));

printf("Rear item is %d\n", rear(queue));

return 0;

}

Solutions

Expert Solution

Solution:

Code implmenetation:

#include <stdio.h>

#include <stdlib.h>
#include<stdbool.h> // #include for bool type.
#include <limits.h>

// A structure to represent a queue

struct Queue

{

int front, rear, size;

unsigned capacity;

int* array;

};

// function to create a queue of given capacity.

// It initializes size of queue as 0

struct Queue* createQueue(unsigned capacity)

{

struct Queue* queue = (struct Queue*) malloc(sizeof(struct Queue));

queue->capacity = capacity;

queue->front = queue->size = 0;

queue->rear = capacity - 1; // This is important, see the enqueue

queue->array = (int*) malloc(queue->capacity * sizeof(int));

return queue;

}

// Queue is full when size becomes equal to the capacity

int isFull(struct Queue* queue)

{ return (queue->size == queue->capacity); }

// Queue is empty when size is 0
// isEmpty() function of boolean type returns true if queue is empty else false.
bool isEmpty(struct Queue* queue)

{
if(queue->front<0 || queue->front > queue->rear)
return true; // returns true.
else
return false; // returns false.
  
}

// Function to add an item to the queue.

// It changes rear and size

void enqueue(struct Queue* queue, int item)

{

if (isFull(queue))

return;

queue->rear = (queue->rear + 1)%queue->capacity;

queue->array[queue->rear] = item;

queue->size = queue->size + 1;

printf("%d enqueued to queue\n", item);

}

// Function to remove an item from queue.

// It changes front and size

int dequeue(struct Queue* queue)

{

if (isEmpty(queue))

return INT_MIN;

int item = queue->array[queue->front];

queue->front = (queue->front + 1)%queue->capacity;

queue->size = queue->size - 1;

return item;

}

// Function to get front of queue

int front(struct Queue* queue)

{

if (isEmpty(queue))

return INT_MIN;

return queue->array[queue->front];

}

// Function to get rear of queue

int rear(struct Queue* queue)

{

if (isEmpty(queue))

return INT_MIN;

return queue->array[queue->rear];

}

// Driver program to test above functions./

int main()

{

struct Queue* queue = createQueue(1000);

enqueue(queue, 210);

enqueue(queue, 111);

enqueue(queue, 121);

enqueue(queue, 122);

enqueue(queue, 134);

enqueue(queue, 144);

printf("%d dequeued from queue\n\n", dequeue(queue));

printf("Front item is %d\n", front(queue));

printf("Rear item is %d\n", rear(queue));

return 0;

}

Code screenshot:

Code output screenshot:


Related Solutions

Solve in C++ program. Modify the use of queue data structure such that the array used...
Solve in C++ program. Modify the use of queue data structure such that the array used to implement the queue is dynamically allocated for a fast food autoservice
You will design a program to keep track of a restaurants waitlist using a queue implemented...
You will design a program to keep track of a restaurants waitlist using a queue implemented with a linked list. Make sure to read pages 1215-1217 and 1227-1251 1. Create a class named waitList that can store a name and number of guests. Use constructors to automatically initialize the member variables. 2. Add the following operations to your program: a. Return the first person in the queue b. Return the last person in the queue c. Add a person to...
Using c++ Design a system to keep track of employee data. The system should keep track...
Using c++ Design a system to keep track of employee data. The system should keep track of an employee’s name, ID number and hourly pay rate in a class called Employee. You may also store any additional data you may need, (hint: you need something extra). This data is stored in a file (user selectable) with the id number, hourly pay rate, and the employee’s full name (example): 17 5.25 Daniel Katz 18 6.75 John F. Jones Start your main...
Use C++ please You will be building a linked list. Make sure to keep track of...
Use C++ please You will be building a linked list. Make sure to keep track of both the head and tail nodes. (1) Create three files to submit. PlaylistNode.h - Class declaration PlaylistNode.cpp - Class definition main.cpp - main() function Build the PlaylistNode class per the following specifications. Note: Some functions can initially be function stubs (empty functions), to be completed in later steps. Default constructor (1 pt) Parameterized constructor (1 pt) Public member functions InsertAfter() - Mutator (1 pt)...
C# The Zookeepers need a system to keep track of all their animals. They need to...
C# The Zookeepers need a system to keep track of all their animals. They need to be able to enter all their animals into the system in a way that allows them to identify and locate them. This requires identifying them by species, age and one characteristic unique to their species. There are three cages and the user must input information about the animal in each one. After accepting input for all three cages, the program should output the contents...
Write a c++ program for the Sales Department to keep track of the monthly sales of...
Write a c++ program for the Sales Department to keep track of the monthly sales of its salespersons. The program shall perform the following tasks: Create a base class “Employees” with a protected variable “phone_no” Create a derived class “Sales” from the base class “Employees” with two public variables “emp_no” and “emp_name” Create a second level of derived class “Salesperson” from the derived class “Sales” with two public variables “location” and “monthly_sales” Create a function “employee_details” under the class “Salesperson”...
C# The Zookeepers need a system to keep track of all their animals. They need to...
C# The Zookeepers need a system to keep track of all their animals. They need to be able to enter all their animals into the system in a way that allows them to identify and locate them. This requires identifying them by species, age and one characteristic unique to their species. There are three cages and the user must input information about the animal in each one. After accepting input for all three cages, the program should output the contents...
Use boolean algebra to prove that: (A^- *B*C^-) + (A^- *B*C) + (A* B^- *C) +...
Use boolean algebra to prove that: (A^- *B*C^-) + (A^- *B*C) + (A* B^- *C) + (A*B* C^-) + (A*B*C)= (A+B)*(B+C) A^- is same as "not A" please show steps to getting the left side to equal the right side, use boolean algebra properties such as distributive, absorption,etc
Write a c++program using queue to find the minimum value in a queue. Use the above...
Write a c++program using queue to find the minimum value in a queue. Use the above program for implementing queue. You must use dequeue function to read values from queue.  
Ava wants to use a database to keep track of the data recordsfor her insurance...
Ava wants to use a database to keep track of the data records for her insurance company and to enforce the following business policies/requirements: USE MS ACCESS TO CREATE A DATABASE & RELATIONASHIP-Every customer must be uniquely identified.-A customer can have many insurance policies.-Every insurance policy must be uniquely identified.-An insurance policy must belong to a valid customer.-Every customer must be served by a valid insurance agent (employee).-An insurance agent (employees) serves many customers.-Every insurance agent (employee) must be uniquely...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT