Question

Exercise 1: Writing Assert Based Black Box Unit Tests for a Queue For this exercise you will need a copy of Queue.h, the queu

typedef struct queue * Queue;


Queue queueCreate(int maxSize);
void enqueue(Queue q, int item);
int queueFront(Queue q);
int dequeue(Queue q);
int queueSize(Queue q);
void queueDestroy(Queue q)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

/*
* Queue.h
*
* Created on: 28-Jul-2020
* Author: naren
*/

#ifndef SRC_QUEUE_H_
#define SRC_QUEUE_H_
#include <stdio.h>
#include <stdlib.h>

/**
* structure for queue
* *array to store items
* capacity maxSize of queue
*/
struct queue{
   int *array;
   int capacity;
   int front;
   int rear;
};


typedef struct queue *Queue;

/**
* function to create queue of given size
*/
Queue queueCreate(int maxSize);

/**
* function to enqueue an item into queue
*/
void enqueue(Queue q, int item);

/**
* function to get an item from front of the queue
*/
int queueFront(Queue q);

/**
* function to dequeue an item from the queue
*/
int dequeue(Queue q);

/**
* function to get size of the given queue
*/
int queueSize(Queue q);

/**
* function to destroy given queue
*/
void queueDestroy(Queue q);

#endif /* SRC_QUEUE_H_ */

/*
* Queue.cpp
*
* Created on: 28-Jul-2020
* Author: naren
*/

#include "Queue.h"


Queue queueCreate(int maxSize){
   Queue q = (Queue) malloc(sizeof(struct queue));
   q->array = (int *) malloc(sizeof(int) * maxSize);
   q->front = -1;
   q->rear = -1;
   q->capacity = maxSize;
   return q;
}

void enqueue(Queue q, int item){
   if (q->rear +1 != q->capacity){
       if (q->rear == -1){
           q->front++;
       }
       q->rear++;
       q->array[q->rear] = item;
   }
}

int queueFront(Queue q){
   if (q->front == -1){
       return -1;
   }
   return q->array[q->front];
}

int dequeue(Queue q){
   int front = q->array[q->front];
   q->front++;
   if (q->front > q->rear){
       q->front = -1;
       q->rear = -1;
   }
   return front;
}

int queueSize(Queue q){
   if(q->front == -1){
       return 0;
   }

   return (q->rear - q->front + 1);
}
void queueDestroy(Queue q){
   free(q);
}

/*
* testQueue.c
*
* this is my testQueue file. you can use your own
*
* Created on: 28-Jul-2020
* Author: naren
*/

#include "Queue.h"

int main(){
   // create a queue
   Queue q = queueCreate(5);
   enqueue(q, 23);
   enqueue(q, 20);
   enqueue(q, 34);

   // test for function queueSize
   if (queueSize(q) == 3){
       printf("test Queue size passed.\n");
   } else {
       printf("test Queue size failed.\n");
   }

   // test for function queueFront
   if (queueFront(q) == 23){
       printf("test Queue front passed.\n");
   } else{
       printf("test Queue front failed.\n");
   }

   // test for function dequeue;
   dequeue(q);
   if (queueFront(q) == 20){
       printf("test deQueue passed.\n");
   } else{
       printf("test deQueue front failed.\n");
   }

   queueDestroy(q);
}

Output:

test Queue size passed. test Queue front passed. test deQueue passed.

Add a comment
Know the answer?
Add Answer to:
typedef struct queue * Queue; Queue queueCreate(int maxSize); void enqueue(Queue q, int item); int queueFront(Queue q);...
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
  • 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...

  • Recall queueADT structure which had the following queue.h interface: /* queue.h */ #ifndef _queue_h #define _queue_h...

    Recall queueADT structure which had the following queue.h interface: /* queue.h */ #ifndef _queue_h #define _queue_h #include "genlib.h“ typedef void *queueElementT; typedef struct queueCDT *queueADT; queueADT NewQueue(void); void FreeQueue(queueADT queue); void Enqueue(queueADT queue, queueElementT element); queueElementT Dequeue(queueADT queue); bool QueueIsEmpty(queueADT queue); bool QueueIsFull(queueADT queue); int QueueLength(queueADT queue); queueElementT GetQueueElement(queueADT queue, int index); #endif Suppose its implementation is available as queue.o, so you can use all the functions in queue.h, but you cannot change their implementation. Now you are asked to...

  • Write a function that takes a string parameter and determines whether the string contains matching grouping...

    Write a function that takes a string parameter and determines whether the string contains matching grouping symbols. Grouping symbols are parenthesis ( ) , brackets [] and curly braces { }. For example, the string {a(b+ac)d[xy]g} and kab*cd contain matching grouping symbols. However, the strings ac)cd(e(k, xy{za(dx)k, and {a(b+ac}d) do not contain matching grouping symbols. (Note: open and closed grouping symbols have to match both in number and in the order they occur in the string). Your function must use...

  • AQueue.java class AQueue implements Queue { private E queueArray[]; // Array holding queue elements private static...

    AQueue.java class AQueue implements Queue { private E queueArray[]; // Array holding queue elements private static final int DEFAULT_SIZE = 10; private int maxSize; // Maximum size of queue private int front; // Index of front element private int rear; // Index of rear element // Constructors @SuppressWarnings("unchecked") // Generic array allocation AQueue(int size) { //BUG #1: maxSize = size maxSize = size+1; // One extra space is allocated rear = 0; front = 1; queueArray = (E[])new Object[maxSize]; //...

  • I need this to be in C Write the implementation file, priority queue.c, for the interface...

    I need this to be in C Write the implementation file, priority queue.c, for the interface in the given header file, priority queue.h. Turn in your priority queue.c file and a suitable main program, main.c, that tests the opaque object. priority queue.h is attached as a file to this assignment but is also listed here for your convenience. Your implementation file should implement the priority queue using a heap data structure. Submissions that implement the priority queue without using a...

  • C++ C. In the file c_final_practice.cpp: (1) Write a function void replace(vector<int>& v, int old, int...

    C++ C. In the file c_final_practice.cpp: (1) Write a function void replace(vector<int>& v, int old, int new) that replaces all occurrences of the integer old in v with the integer new. For example, if v is the vector {1,2,1,3}, then after calling replace(v, 1, 3) v should be {3,2,3,3}. (2) Write the main-function so that it prompts the user for a list of integers, and then uses your replace function to display the following lists: • The user's list with...

  • To solve real world problem, the programer uses ADT like list, stack, queue, set, map, ... etc. t...

    To solve real world problem, the programer uses ADT like list, stack, queue, set, map, ... etc. to build our data model. In AS8, we pick and choose STL queue and stack to build a postfix calculator. In this assignment you are to Design your own linked-list, a series of integer nodes. The private attributes of this IntLinked Queue including a integer node struct and private control variables and methods: private: struct Node { int data; Node *next; }; Node...

  • C++ -- Event processing simulation using a transaction queue Hi! it is queue simulation please read...

    C++ -- Event processing simulation using a transaction queue Hi! it is queue simulation please read the instructions, write codes, and explain the code with comments. Thank you Transactions enter the system and are stored in a queue. Each transaction represents some work that needs to be accomplished. Servers exist which process transactions. Servers take transactions off the queue and process them. you’re building the simulation framework. The idea is that somebody would take your framework, and add the specifics...

  • Getting started with Java on elvis Download Greeting.java from the class web site. Use FileZilla to...

    Getting started with Java on elvis Download Greeting.java from the class web site. Use FileZilla to place it into your Lab5 directory. Look at the content of your directory to see the file using the command ls Look at the content of the file in your directory using the command more Greeting.java Compile the HelloClass program using the command javac Greeting.java. Then use ls to see your class file. Run the program without parameters using the command java Greeting Run...

  • 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...

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