
Patrick B. answered 07/06/20
Math and computer tutor/teacher
using namespace std;
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
// change these sizes accordinagly
#define MAX_REC_SIZE (4096) //data records can be serialized via csv: comma separated values if necessary
#define MAX_QUEUE_SIZE (3) //(2500)
typedef struct _TQueue
{
char Q[MAX_QUEUE_SIZE][MAX_REC_SIZE];
int count;
} * TQueue;
#define QUEUE_SIZE (sizeof(struct _Queue))
void Queue_Init(TQueue q) //intializes the queue
{
q->count = 0;
memset(q->Q,0,MAX_QUEUE_SIZE*MAX_REC_SIZE);
}
int Queue_Push(TQueue q, char * newDataStr)
{
int iReturn=0;
if (q->count<MAX_QUEUE_SIZE) // pushes the new record on the back
{
strncpy(q->Q[ (q->count)++],newDataStr,MAX_REC_SIZE); // count incremented
}
else //Queue is full !!!!
{
iReturn = -1;
}
return(iReturn);
}
int Queue_Pop(TQueue q, char * dataStr)
{
int iReturn=0;
if (q->count>0) // pops the first record off the queue
{
strncpy(dataStr,q->Q[0],MAX_REC_SIZE); // copies the first record to buffer
memset(q->Q[0],0,MAX_REC_SIZE); // nullifies the first record
for (int iLoop=0; iLoop<q->count; iLoop++) // moves the records over 1 position towards the front
{
strcpy(q->Q[iLoop],q->Q[iLoop+1]);
}
memset(q->Q[ --(q->count)],0,MAX_REC_SIZE); // nullifies the last record; count decremented
}
else // Cannot pop an empty queue
{
iReturn=-1;
}
return(iReturn);
}
void Queue_Display(TQueue q, char * debugMsg=NULL)
{
if (debugMsg != NULL)
{
cout << "***********************************************************************" << endl;
cout << debugMsg << endl;
}
cout << "********************************************************************" << endl;
for (int iLoop=0; iLoop<q->count; iLoop++)
{
cout << "(" << (iLoop+1) << ")" << " " << q->Q[iLoop] << endl;
}
}
int main()
{
struct _TQueue q;
char name[MAX_REC_SIZE];
int iReturn;
char outbuff[25];
Queue_Init(&q);
strcpy(name,"MIKE THOMPSON");
iReturn = Queue_Push(&q,name);
sprintf(outbuff,"1st push : iReturn = %d",iReturn);
Queue_Display(&q,outbuff); //Mike
strcpy(name,"PATRICK BALDWIN");
iReturn =Queue_Push(&q,name);
sprintf(outbuff,"2nd push : iReturn = %d",iReturn);
Queue_Display(&q,outbuff); //Mike,Patrick
strcpy(name,"STEVE LINDSTROM");
iReturn = Queue_Push(&q,name);
sprintf(outbuff,"3rd push : iReturn = %d",iReturn);
Queue_Display(&q,outbuff); //Mike,Patrick,Steve
strcpy(name,"BILL SUSANY");
iReturn = Queue_Push(&q,name); //fails, returns -1: queue is full
sprintf(outbuff,"4th push : iReturn = %d",iReturn);
Queue_Display(&q,outbuff); //Mike,Patrick,Steve
iReturn = Queue_Pop(&q,name);
cout << "Now serving :" << name << endl;
sprintf(outbuff,"1st pop : iReturn = %d",iReturn);
Queue_Display(&q,outbuff); //Patrick,Steve
strcpy(name,"BILL SUSANY");
iReturn = Queue_Push(&q,name);
sprintf(outbuff,"5th push : iReturn = %d",iReturn);
Queue_Display(&q,outbuff); //Patrick,Steve,Bill
for (int iLoop=0; iLoop<4; iLoop++) // pops off Patrick, Steve, Bill, then an error
{
iReturn = Queue_Pop(&q,name);
if (iReturn>=0)
{
cout << "Now serving :" << name << endl;
}
else
{
cout << "Cannot pop the Empty Queue" << endl;
}
sprintf(outbuff,"pop : iReturn = %d",iReturn);
Queue_Display(&q,outbuff);
}
}