0% found this document useful (0 votes)
16 views3 pages

Simple Queue of Strings

Uploaded by

Sidhesh mishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views3 pages

Simple Queue of Strings

Uploaded by

Sidhesh mishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

 Simple queue of strings:

o Header file: SimpleQueue.h


o Code to implment the class: SimpleQueue.C
o Main program to drive it: main.C
o Makefile to build the code: Makefile

#ifndef _BUFFER_H_
#define _BUFFER_H_

#include
#include

class SimpleQueue {
public:
SimpleQueue(int = 3);
~SimpleQueue() ;
int no_more ();
int insert (const string&);
int remove (string*&, int&);

private:
string **array;
int size; // size of the queue
int head; // first ocupied location
int tail; // first empty location
int count; // number of elements in queue
};

#endif

using namespace std;

#include "SimpleQueue.h"

SimpleQueue::SimpleQueue(int sz) {
size = sz;
count = 0;

array = new string* [size];


for (int i = 0; i < size; i++)
array[i] = NULL;
head = tail = 0;
}

SimpleQueue::~SimpleQueue() {
for (int i = 0; i < size; i++)
delete array[i];
delete []array;
}

int
SimpleQueue::no_more () {
if (count < size) {
array[head] = NULL;
head = (head + 1) % size;
count++;
return 1;
} else
return 0;
}

int
SimpleQueue::insert (const string& buffer) {
if (count < size) {
array[head] = new string (buffer);
head = (head + 1) % size;
count++;
return 1;
} else
return 0;
}

int
SimpleQueue::remove (string*& buffer, int& is_done) {
is_done = 0;
if (count > 0) {
buffer = array[tail];
if (buffer == NULL)
is_done = 1;
array[tail] = NULL;
tail = (tail + 1) % size;
count --;
return 1;
} else
return 0;
}

#include
#include

using namespace std;

#include "SimpleQueue.h"

void CheckRV (char *msg, int rv, int done, string *sp)
{
cout << msg << ", rv=" << rv;
if (done)
cout << ", done"; else cout << ", not done";
if (rv) {
cout << ", string='" << *sp << "'";
delete sp;
}
cout << endl;
return;
}

int
main(int argc, char* argv[]) {
int rv;
SimpleQueue q;
string *sp;
int done;

if (argc != 1) {
cerr << "usage : No arguments to this program!" << endl;
exit(1);
}

rv = q.insert("Hi mom!");
cout << "After 1st insert, rv=" << rv << endl;
rv = q.insert("Four score and seven beers ago....");
cout << "After 2nd insert, rv=" << rv << endl;

rv = q.remove(sp, done);
CheckRV ("After 1st remove", rv, done, sp);

rv = q.remove(sp, done);
CheckRV ("After 2nd remove", rv, done, sp);

rv = q.remove(sp, done);
CheckRV ("After 3rd remove", rv, done, sp);
}

CFLAGS = -g -Wall -pedantic


LIBS =

test: SimpleQueue.o main.o


g++ -o test SimpleQueue.o main.o $(LIBS)

main.o: main.C SimpleQueue.h


g++ -c $(CFLAGS) main.C

SimpleQueue.o: SimpleQueue.C SimpleQueue.h


g++ -c $(CFLAGS) SimpleQueue.C

You might also like