Question

// =================== Support Code ================= // Queue // // // // - Implement each of the functions to create a working circular queue. // - Do not change any of the function declarations // ...

// =================== Support Code =================

// Queue

//

//

//

// - Implement each of the functions to create a working circular queue.

// - Do not change any of the function declarations

//   - (i.e. queue_t* create_queue(unsigned int _capacity) should not have additional arguments)

// - You should not have any 'printf' statements in your queue functions.

//   - (You may consider using these printf statements to debug, but they should be removed from your final version)

// ==================================================

#ifndef MYQUEUE_H

#define MYQUEUE_H

// The main data structure for the queue

struct queue{

              unsigned int back;              // The next free position in the queue

                                                                            // (i.e. the end or tail of the line)

              unsigned int front;              // Current 'head' of the queue

                                                                            // (i.e. the front or head of the line)

              unsigned int size;                // How many total elements we currently have enqueued.

              unsigned int capacity; // Maximum number of items the queue can hold

              int* data;                                    // The 'integer' data our queue holds             

};

// Creates a global definition of 'queue_t' so we

// do not have to retype 'struct queue' everywhere.

typedef struct queue queue_t;

// Create a queue

// Returns a pointer to a newly created queue.

// The queue should be initialized with data on

// the heap.

queue_t* create_queue(unsigned int _capacity){

              queue_t* myQueue = NULL;

              return myQueue;

}

// Queue Empty

// Check if the queue is empty

// Returns 1 if true (The queue is completely empty)

// Returns 0 if false (the queue has at least one element enqueued)

int queue_empty(queue_t* q){

              return 0;

}

// Queue Full

// Check if the queue is Full

// Returns 1 if true (The queue is completely full)

// Returns 0 if false (the queue has more space available to enqueue items)

int queue_full(queue_t* q){

              return 0;

}

// Enqueue a new item

// i.e. push a new item into our data structure

// Returns a -1 if the operation fails (otherwise returns 0 on success).

// (i.e. if the queue is full that is an error).

int queue_enqueue(queue_t* q, int item){

                             return -1; // Note: you should have two return statements in this function.

}

// Dequeue an item

// Returns the item at the front of the queue and

// removes an item from the queue.

// Removing from an empty queue should crash the program, call exit(1)

int queue_dequeue(queue_t *q){

                             return 99999; // Note: This line is a filler so the code compiles.

}

// Queue Size

// Queries the current size of a queue

// A queue that has not been previously created will crash the program.

// (i.e. A NULL queue cannot return the size, call exit(1))

unsigned int queue_size(queue_t* q){

              return 0;

}

// Free queue

// Removes a queue and all of its elements from memory.

// This should be called before the proram terminates.

void free_queue(queue_t* q){

}

#endif

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h> // For IO operations
#include <stdlib.h> // for malloc/free

// The main data structure for the queue
struct queue{
   unsigned int back;        // The next free position in the queue
                                  // (i.e. the end or tail of the line)
   unsigned int front;        // Current 'head' of the queue
                                  // (i.e. the front or head of the line)
   unsigned int size;        // How many total elements we have enqueued.
   unsigned int capacity; // Number of items the queue can hold
   int* data;                   // The 'integer' data our queue holds  
};
// Creates a global definition of 'queue_t' so we
// do not have to retype 'struct queue' everywhere.
typedef struct queue queue_t;

// Create a queue
// Returns a pointer to a newly created queue.
// The queue should be initialized with data on
// the heap.
queue_t* create_queue(unsigned int _capacity){
  
   queue_t* myQueue = NULL;
        int size;
  
   if(_capacity == 0)
   {
   printf(" cannot create a queue of size 0 \n");
        exit(1);
   }


   myQueue =(queue_t*)malloc(sizeof(queue_t));
   myQueue->capacity = _capacity;
   myQueue->size = size = 0 ;
   myQueue->front = 0;
   myQueue->back =0;
   myQueue->data = (int*)malloc(myQueue->capacity * sizeof(int));
          
  
   return myQueue;
}

// Queue Empty
// Check if the queue is empty
// Returns 1 if true (The queue is completely empty)
// Returns 0 if false (the queue has more space available)


int queue_empty(queue_t* q){

        if(q->size == 0)
        {
             printf(" queue is empty");
             return 1;
        }

        else

        return 0;
}


// Queue Full
// Check if the queue is Full
// Returns 1 if true (The queue is completely full)
// Returns 0 if false (the queue has more space available)
int queue_full(queue_t* q){
  
     if (q->size == q->capacity)
   {
   printf(" queue is full\n");
   return 1;
   }
  
   else

   return 0;

}

// Enqueue a new item
// i.e. push a new queue into our data structure
// Returns a -1 if the operation fails (otherwise returns 0 on success).
// (i.e. the queue is full).
int queue_enqueue(queue_t *q, int item){
  
   if(!queue_full(q))            
   {  
  
   q->size = q->size + 1;
        printf(" added an item in queue, %d\n" , item);
       
   if (q->back == q->capacity)
   {
       q->back =0;
   }
   q->data[q->back] = item;
   q->back++;

   return 0;
   }

   else
    return -1;

}

// Dequeue an item
// Returns the item at the front of the queue and
// removes an item from the queue.
int queue_dequeue(queue_t *q){
  
   int item = 0;
   if(queue_empty(q))
   {
   printf(" the queue is empty\n");
   return -1;
   }
  
   item = q->data[q->front];
   q->front++;
   q->size = q->size -1 ;
   if (q->front == q->capacity)
{
   q->front = 0;
}
   return item;
  
}


// Queue Size
// Queries the current size of a queue
// A queue that has not been previously created will crash the program.
// (i.e. A NULL q cannot return the size.)
unsigned int queue_size(queue_t* q){
   if (q == NULL )
   {
       exit(1);
   }

        int queueSize = q->size;
        printf("\n queue size is %d\n " ,queueSize);
   return queueSize;

}


// Free queue
// Removes a queue and all of its elements from memory.
// This should be called before the proram terminates.
void free_queue(queue_t* q){
free(q->data);
free(q);
}


// A sample test of your program
// You can add as many unit tests as you like
// We will be adding our own to test your program.
void unitTest1(){

   queue_t* test1 = create_queue(5);
   printf("Attempting to add %d\n",15);
   queue_enqueue(test1,15);  
   queue_enqueue(test1,3);  
   queue_enqueue(test1,6);  
   printf("\n");
   queue_size(test1);
   printf("\n");
   queue_enqueue(test1,1);  
   queue_enqueue(test1,6);  
   queue_enqueue(test1,7);  
   printf("to check dequeue function\n");
   printf("Removing: %d\n",queue_dequeue(test1));  
        free_queue(test1);
}

void unitTest2(){
   queue_t* test2 = create_queue(2);
   printf("Attempting to add %d\n",10);
   queue_enqueue(test2,10);  
   printf("Removing: %d\n",queue_dequeue(test2));  
   queue_size(test2);

   queue_dequeue(test2);
  
   free_queue(test2);
}


void unitTest3(){
   queue_t* test3 = create_queue(0);
   free_queue(test3);
}

// ====================================================
// ================== Program Entry ===================
// ====================================================
int main(){

   // List of Unit Tests to test your data structure  
   unitTest1();
   unitTest2();  
   unitTest3();
   return 0;
}

Add a comment
Know the answer?
Add Answer to:
// =================== Support Code ================= // Queue // // // // - Implement each of the functions to create a working circular queue. // - Do not change any of the function declarations // ...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In C, Implement each of the functions to create a working stack 6 II-Implement each of...

    In C, Implement each of the functions to create a working stack 6 II-Implement each of the functions to create a working stack. 7 II -Do not change any of the function declarations I-(i.e. stack t* create stack) should not have additional arguments) II-You should not have any 'printf' statements in your stack functions 10 II(You may consider using these printf statements to debug, but they should be removed from your final version) 12 #ifndefMYSTACK_A 13 #define MYSTACKH 14 15...

  • Design and implement a class Q that uses Q.java as a code base. The queue ADT...

    Design and implement a class Q that uses Q.java as a code base. The queue ADT must use class LinkedList from Oracle's Java class library and its underlying data structure (i.e. every Q object has-a (contains) class LinkedList object. class Q is not allowed to extend class LinkedList. The methods that are to be implemented are documented in Q.java. Method comment blocks are used to document the functionality of the class Q instance methods. The output of your program must...

  • PROBLEM- void CBQueue::printF2B( ) If the queue is empty, the method should print “Queue is empty”,...

    PROBLEM- void CBQueue::printF2B( ) If the queue is empty, the method should print “Queue is empty”, otherwise, this method should print the items in the queue starting at the front of the queue and proceeding to the rear of the queue. The items should be printed one per line. Now that this method is written, you can do a more thorough job of testing enqueue( ). You will want to call printF2B( ) and printB2F( ) after implementing each method...

  • Create a program named NonZeroNumQueue.java then copy the contents from NonZeroNumQueue.txt. Then implement enqueue method. To...

    Create a program named NonZeroNumQueue.java then copy the contents from NonZeroNumQueue.txt. Then implement enqueue method. To get credit, DO NOT add or alter any data members/methods except the enqueue(int num). The NonZeroNumQueue will NOT accept zero, which mean you can enqueue any whole numbers EXCEPT 0(zero). You have already noticed that the queue is NEVER full, so the isFull() always returns false. given NonZeroNumQueue.txt public class NonZeroNumQueue{ private int[] data; private int total; private int front; private int tail; public...

  • Are based on the following Queue class code segment class QueueFull {/* Empty exception class */};...

    Are based on the following Queue class code segment class QueueFull {/* Empty exception class */}; Class Queue Empty {/* Empty exception class */}; struct Node//Node structure int data;//Holds an integer Node* next;//Pointer to next node in the queue}; Class Queue//Linked node implementation of Queue ADT {Private: Node* front;//Pointer to front node of queue Node* rear;//pointer to last node of queue Public: Queue ()://default constructor initializes queue to be empty -Queue ();//Deallocates all nodes in the queue Void Add (int...

  • A limited-sized Queue ADT is an abstract data type that has a limit on the length...

    A limited-sized Queue ADT is an abstract data type that has a limit on the length of the queue. It can be created with a static array of size N, where N is the maximum length of the array. In C, this structure can be defined as follows: typedef struct {int * data;//array of the data on the queue//you may add other attributes here but use as few as possible} queue_t; Write an (efficient) pseudocode for the implementation of each...

  • In c, please implement the following 3 functions to the code below is_reachable(graph_t * g, int...

    In c, please implement the following 3 functions to the code below is_reachable(graph_t * g, int source, int dest) returns 0 if I can reach the destination from source, -1 otherwise ( using BFS) has_cycle(graph_t * g) returns 0 if there is a cycle in the graph, -1 otherwise (using BFS or DFS) print_path(graph_t * g, int source, int dest) prints any path from source to destination if there exists one (Choose either BFS or DFS, typically DFS is much...

  • Balment a la medicul Quoc that speciala a circular que has the following private data members...

    Balment a la medicul Quoc that speciala a circular que has the following private data members and public member functions. The circular que simplemented using an atay. Your submission should consist of four separate files the three source code file header file.implementation file and main program or routine and the program otput. When making the submission, please do not submit it as a file Private data members: int the tray int current size oprema prinete the first met of the...

  • Suppose we want to implement a circular queue using an array that has an initial capacity...

    Suppose we want to implement a circular queue using an array that has an initial capacity (maximum number of elements) MAX. A circular queue is like a regular queue except that elements can be enqueued or dequeued by wrapping around it. Assume we enqueue on the tail and dequeue from the head. An example circular queue with sample operations is shown below: head head tail head tail tail head Enqueue(9) a) Write a program in C that implements this circular...

  • Array-based Queue Lecture 6 Two Class Exercises | Class Exercise #1 - Create an array-based queue that holds value...

    Array-based Queue Lecture 6 Two Class Exercises | Class Exercise #1 - Create an array-based queue that holds values of double data type. 1.) Create a program that produces the following output OUTPUT: Q Quit Enter your choice: e Enter an item: 1.1 E Enqueue D Dequeue s-show queue ← showMenuO function called in main) OQuit // screen clears-.. continue enqueuing.screen clearing with each iteration Enter your choice: e Queue is full. E Enqueue D Dequeue s Show queue 0...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT