____________________________________
First: Header file.h (functions prototype).
____________________________________
// File: Queuet.h
// Queue template class definition
// Dynamic array implementation
#ifndef QUEUET_H
#define QUEUET_H
template <class Type>
class Queuet
{
public:
Queuet(int nelements = 128); // Constructor
Queuet (const Queuet <Type> &); // Copy Constructor
~Queuet(); // Destructor
// Member Functions
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
bool queueIsFull() const; // Test for Full queue
int queueLength() const; // Queue Length
private:
Type *queue; // pointer to dynamic array
int front, rear, count, MaxSize;
};
#endif // QUEUET_H
#include "Queuet.cpp"
________________________________________________
second: Implementation file.cpp
_____________________________________
// File: Queuet.cpp
// Queue template class implementation
#include <iostream>
using namespace std;
// Constructor with argument, size is nelements, default 128
template <class Type>
Queuet<Type>::Queuet(int nelements)
{ MaxSize = nelements; queue = new Type[MaxSize];
front = 1; rear = 0; count = 0;
}
// Copy Constructor
template <class Type>
Queuet<Type>::Queuet (const Queuet<Type> &original)
:MaxSize(original.MaxSize), front(original.front), rear(original.rear), count(original.count)
{
queue = new Type[MaxSize];
for (int i = 0; i < MaxSize; i++) queue[i] = original.queue[i];
}
// Destructor
template <class Type>
Queuet<Type>::~Queuet()
{ delete [] queue;}
// Add to rear
template <class Type>
void Queuet<Type>::enqueue(Type v)
{
if(queueIsFull()) cout << "Queue is Full" << endl;
else
{
rear = (rear + 1) % MaxSize;
queue[rear] = v; count++;
}
}
// Remove from front
template <class Type>
void Queuet<Type>::dequeue(Type &v)
{
if(queueIsEmpty()) cout << "Queue is Empty" << endl;
else
{
v = queue[front];
front = (front + 1) % MaxSize; count--;
}
}
// Retrieve front without removing it
template <class Type>
void Queuet<Type>::queueFront(Type &v) const
{
if(queueIsEmpty()) cout << "Queue is Empty" << endl;
else v = queue[front];
}
// Test for Empty queue
template <class Type>
bool Queuet<Type>::queueIsEmpty() const
{ return (count == 0); }
// Test for Full queue
template <class Type>
bool Queuet<Type>::queueIsFull() const
{ return (count == MaxSize); }
// Queue Length
template <class Type>
int Queuet<Type>::queueLength() const
{ return count; }
// Queue template class implementation
#include <iostream>
using namespace std;
// Constructor with argument, size is nelements, default 128
template <class Type>
Queuet<Type>::Queuet(int nelements)
{ MaxSize = nelements; queue = new Type[MaxSize];
front = 1; rear = 0; count = 0;
}
// Copy Constructor
template <class Type>
Queuet<Type>::Queuet (const Queuet<Type> &original)
:MaxSize(original.MaxSize), front(original.front), rear(original.rear), count(original.count)
{
queue = new Type[MaxSize];
for (int i = 0; i < MaxSize; i++) queue[i] = original.queue[i];
}
// Destructor
template <class Type>
Queuet<Type>::~Queuet()
{ delete [] queue;}
// Add to rear
template <class Type>
void Queuet<Type>::enqueue(Type v)
{
if(queueIsFull()) cout << "Queue is Full" << endl;
else
{
rear = (rear + 1) % MaxSize;
queue[rear] = v; count++;
}
}
// Remove from front
template <class Type>
void Queuet<Type>::dequeue(Type &v)
{
if(queueIsEmpty()) cout << "Queue is Empty" << endl;
else
{
v = queue[front];
front = (front + 1) % MaxSize; count--;
}
}
// Retrieve front without removing it
template <class Type>
void Queuet<Type>::queueFront(Type &v) const
{
if(queueIsEmpty()) cout << "Queue is Empty" << endl;
else v = queue[front];
}
// Test for Empty queue
template <class Type>
bool Queuet<Type>::queueIsEmpty() const
{ return (count == 0); }
// Test for Full queue
template <class Type>
bool Queuet<Type>::queueIsFull() const
{ return (count == MaxSize); }
// Queue Length
template <class Type>
int Queuet<Type>::queueLength() const
{ return count; }
________________________________________________
// File: QueuetAppl.cpp
// Test if a string is a palindrome
#include <iostream>
#include <string>
using namespace std;
#include "Stackt.h"
#include "Queuet.h"
bool palindrome(string w);
int main()
{
string w;
cout << "Enter a string:" << endl;
getline(cin,w); cout << w << endl;
if (palindrome(w)) cout << "Palindrome" << endl;
else cout << "NOT Palindrome" << endl;
return 0;
}
bool palindrome(string w)
{
Stackt<char> s;
Queuet<char> q;
int L = w.length(); char c,v;
for (int i = 0; i < L; i++)
{ c = w.at(i); s.push(c); q.enqueue(c); }
while(!q.queueIsEmpty())
{ q.dequeue(c); s.pop(v); if(c != v) return false; }
return true;
}
finally class test
_____________________________________
// File: QueuetAppl.cpp
// Test if a string is a palindrome
#include <iostream>
#include <string>
using namespace std;
#include "Stackt.h"
#include "Queuet.h"
bool palindrome(string w);
int main()
{
string w;
cout << "Enter a string:" << endl;
getline(cin,w); cout << w << endl;
if (palindrome(w)) cout << "Palindrome" << endl;
else cout << "NOT Palindrome" << endl;
return 0;
}
bool palindrome(string w)
{
Stackt<char> s;
Queuet<char> q;
int L = w.length(); char c,v;
for (int i = 0; i < L; i++)
{ c = w.at(i); s.push(c); q.enqueue(c); }
while(!q.queueIsEmpty())
{ q.dequeue(c); s.pop(v); if(c != v) return false; }
return true;
}
____________________________________________
// Passing a stack and a queue as reference parameters
#include <iostream>
#include <string>
using namespace std;
#include "Stackt.h"
#include "Queuet.h"
void fillqs(string ,Stackt<char> &,Queuet<char> &);
int main()
{
string w;
char c;
Stackt<char> ST;
Queuet<char> QU;
cout << "Enter a string:" << endl;
getline(cin,w); cout << w << endl;
fillqs(w,ST,QU);
while(!ST.stackIsEmpty())
{ ST.pop(c); cout << c; }
cout<<endl;
while(!QU.queueIsEmpty())
{ QU.dequeue(c); cout << c; }
cout<< endl;
return 0;
}
void fillqs(string w,Stackt<char> &s,Queuet<char> &q)
{
int L = w.length(); char c;
for (int i = 0; i < L; i++)
{ c = w.at(i); s.push(c); q.enqueue(c); }
}
Another class test
___________________
// File: QueuetAppl2.cpp// Passing a stack and a queue as reference parameters
#include <iostream>
#include <string>
using namespace std;
#include "Stackt.h"
#include "Queuet.h"
void fillqs(string ,Stackt<char> &,Queuet<char> &);
int main()
{
string w;
char c;
Stackt<char> ST;
Queuet<char> QU;
cout << "Enter a string:" << endl;
getline(cin,w); cout << w << endl;
fillqs(w,ST,QU);
while(!ST.stackIsEmpty())
{ ST.pop(c); cout << c; }
cout<<endl;
while(!QU.queueIsEmpty())
{ QU.dequeue(c); cout << c; }
cout<< endl;
return 0;
}
void fillqs(string w,Stackt<char> &s,Queuet<char> &q)
{
int L = w.length(); char c;
for (int i = 0; i < L; i++)
{ c = w.at(i); s.push(c); q.enqueue(c); }
}
No comments:
Post a Comment