Transformers : Enqueue, Dequeue, MakeEmpty
Observers : IsEmpty, IsFull, LengthIs
Iterator : front, rear
#include "itemType.h"
template < class Itemtype >
class QueueType{
public:
QueType(); // default Constructor -> static allocation
QueType(int max); // Parameterized Constructor -> Dynamic allocation
~QueType();
bool IsFull() const;
bool IsEmpty() const;
void Enqueue(ItemType item);
void Dequeue(ItemType& item);
private:
int front;
int rear;
int maxQue;
ItemType* items;
}
template < class Itemtype >
QueType< ItemType > :: QueType () {
maxQue = 100 // Default size of array!
front = maxQue - 1;
rear = maxQue - 1;
items = new ItemType(maxQue); // Static allocation!
}
template < class Itemtype >
QueType< ItemType > :: QueType (int max) {
maxQue = max + 1 // get size of array!
front = maxQue - 1;
rear = maxQue - 1;
items = new ItemType(maxQue); // Dynamic allocation!
}
template < class Itemtype >
QueType< ItemType > :: ~QueType () {
delete[] items;
}
template < class Itemtype >
bool QueType< ItemType > :: IsEmpty () {
return ( rear == front );
}
template < class Itemtype >
bool QueType< ItemType > :: IsFull () {
return ( (rear+1) % maxQue == front );
}
template < class Itemtype >
void QueType< ItemType > :: Enqueue(ItemType item) {
rear = (rear+1) % maxQue; // Circular
items[ rear ] = item;
}
template < class Itemtype >
void QueType< ItemType > :: Dequeue(ItemType& item) {
front = (front+1) % maxQue; // Circular
item = items[ front ];
}
class CountedQueueType : public QueueType <ItemType>{
public:
CountedQueueType();
void Enqueue( ItemType newitem );
void Dequeue( ItemType& item);
int LengthIs() const;
private:
int length; // number of items on the counted queue
}
template < class Itemtype >
CountedType<ItemType>::CountedQueueType(){
length = 0;
}
template < class Itemtype >
int CountedType<ItemType>::LengthIs() const{
return length;
}
template < class Itemtype >
void CountedType<ItemType>::Enqueue(ItemType newItem){
length++;
QueType< ItemType >::Enqueue( newItem );
}
template < class Itemtype >
void CountedType<ItemType>::Dequeue(ItemType item){
length--;
QueType< ItemType >::Dequeue( item );
}
#include < iostream >
#include < string >
#include < stack >
#include < queue >
int main(){
string Input;
getline(cin, Input); // Get string from user!
bool IsPalindrome = true;
stack<char> s;
queue<char> q;
for(char ch : Input){
if(ch == ' ') {continue;}
ch = toupper(ch);
s.push(ch);
q.push(ch);
}
char stack_char, queue_char;
while (!s.empty() && !q.empty()) { // stack과 queue의 크기는 모두 같아야 하고, 비어있지 않아야 한다.
stack_char = s.top();
queue_char = q.front();
if (stack_char != queue_char) {
Palindrome = false;
break;
}
s.pop();
q.pop();
}
if (IsPalindrome) { cout << input << " is a Palindrome!" << endl; }
else { cout << input << " isn't a Palindrome!" << endl; }
}