____________________________________
First: Header file.h (functions prototype).
____________________________________
// File: QueueL.h
// Linked List Queue class definition
#ifndef QUEUEL_H
#define QUEUEL_H
template <class Type>
class QueueL
{
public:
QueueL(); // Constructor
~QueueL(); // Destructor
void enqueue(Type ); // Add to rear
void dequeue(Type &); // Remove from front
void queueFront(Type &) const; // retrieve front
bool queueIsEmpty() const; // Test for Empty queue
int queueLength() const; // Queue Length
private:
// Node Class
class node
{
public:
Type e; // queue element
node *next; // pointer to next node
}; // end of class node declaration
typedef node * NodePointer;
NodePointer front , rear; // Pointers
int count; // length
};
#endif // QUEUEL_H
#include "QueueL.cpp"
// Linked List Queue class definition
#ifndef QUEUEL_H
#define QUEUEL_H
template <class Type>
class QueueL
{
public:
QueueL(); // Constructor
~QueueL(); // Destructor
void enqueue(Type ); // Add to rear
void dequeue(Type &); // Remove from front
void queueFront(Type &) const; // retrieve front
bool queueIsEmpty() const; // Test for Empty queue
int queueLength() const; // Queue Length
private:
// Node Class
class node
{
public:
Type e; // queue element
node *next; // pointer to next node
}; // end of class node declaration
typedef node * NodePointer;
NodePointer front , rear; // Pointers
int count; // length
};
#endif // QUEUEL_H
#include "QueueL.cpp"
________________________________________________
second: Implementation file.cpp
_____________________________________
// File:QueueL.cpp
// Linked List Queue Class implementation
#include <iostream>
using namespace std;
// Class Constructor
template <class Type>
QueueL<Type>::QueueL()
{
front = NULL; rear = NULL; count = 0;
}
// Class Destructor
template <class Type>
QueueL<Type>::~QueueL()
{
NodePointer cursor;
while(front != NULL)
{
cursor = front;
front = front->next;
delete cursor;
}
}
// Add a node with data (v) at the rear of the queue;
// the new node becomes the rear node.
template <class Type>
void QueueL<Type>::enqueue(Type v)
{
NodePointer pnew = new node;
pnew->e = v; pnew->next = NULL;
if(queueIsEmpty())
{ front = pnew; rear = pnew;}
else
{ rear->next = pnew; rear = pnew; }
count++;
}
// dequeue front node. Next node becomes front node.
template <class Type>
void QueueL<Type>::dequeue(Type &v)
{
NodePointer cursor;
if(queueIsEmpty()) cout << "Queue is Empty" << endl;
else
{
v = front->e;
cursor = front;
front = front->next;
delete cursor; count--;
}
}
// Retrieve front node without removing it
template <class Type>
void QueueL<Type>::queueFront(Type &v) const
{
NodePointer cursor;
if(queueIsEmpty()) cout << "Queue is Empty" << endl;
else { v = front->e; }
}
// return True if queue is empty
template <class Type>
bool QueueL<Type>::queueIsEmpty() const
{
return (count == 0);
}
// Queue Length
template <class Type>
int QueueL<Type>::queueLength() const
{ return count; }
________________________________________________
// Linked List Queue Class implementation
#include <iostream>
using namespace std;
// Class Constructor
template <class Type>
QueueL<Type>::QueueL()
{
front = NULL; rear = NULL; count = 0;
}
// Class Destructor
template <class Type>
QueueL<Type>::~QueueL()
{
NodePointer cursor;
while(front != NULL)
{
cursor = front;
front = front->next;
delete cursor;
}
}
// Add a node with data (v) at the rear of the queue;
// the new node becomes the rear node.
template <class Type>
void QueueL<Type>::enqueue(Type v)
{
NodePointer pnew = new node;
pnew->e = v; pnew->next = NULL;
if(queueIsEmpty())
{ front = pnew; rear = pnew;}
else
{ rear->next = pnew; rear = pnew; }
count++;
}
// dequeue front node. Next node becomes front node.
template <class Type>
void QueueL<Type>::dequeue(Type &v)
{
NodePointer cursor;
if(queueIsEmpty()) cout << "Queue is Empty" << endl;
else
{
v = front->e;
cursor = front;
front = front->next;
delete cursor; count--;
}
}
// Retrieve front node without removing it
template <class Type>
void QueueL<Type>::queueFront(Type &v) const
{
NodePointer cursor;
if(queueIsEmpty()) cout << "Queue is Empty" << endl;
else { v = front->e; }
}
// return True if queue is empty
template <class Type>
bool QueueL<Type>::queueIsEmpty() const
{
return (count == 0);
}
// Queue Length
template <class Type>
int QueueL<Type>::queueLength() const
{ return count; }
________________________________________________
finally class test
_____________________________________
// File: QueueLAppl.cpp
// Applies Linked List Queue Class to a string
#include <iostream>
#include <string>
using namespace std;
#include "QueueL.h"
int main()
{
QueueL<char> q;
string instring, outstring;
char c;
int i;
cout << q.queueLength() << endl;
// Read a string
cout << "Enter a string:" << endl;
getline(cin,instring);
cout << instring << endl;
outstring = "";
for (i = 0; i < instring.length(); i++)
{
c = instring.at(i);
q.enqueue(toupper(c));
}
cout << q.queueLength() << endl;
while(!q.queueIsEmpty())
{
q.dequeue(c);
outstring = outstring + c;
}
cout << outstring << endl;
cout << q.queueLength() << endl;
return 0;
}
// Applies Linked List Queue Class to a string
#include <iostream>
#include <string>
using namespace std;
#include "QueueL.h"
int main()
{
QueueL<char> q;
string instring, outstring;
char c;
int i;
cout << q.queueLength() << endl;
// Read a string
cout << "Enter a string:" << endl;
getline(cin,instring);
cout << instring << endl;
outstring = "";
for (i = 0; i < instring.length(); i++)
{
c = instring.at(i);
q.enqueue(toupper(c));
}
cout << q.queueLength() << endl;
while(!q.queueIsEmpty())
{
q.dequeue(c);
outstring = outstring + c;
}
cout << outstring << endl;
cout << q.queueLength() << endl;
return 0;
}
No comments:
Post a Comment