ok got past that problem and found I am doing something wrong in building or checking the queue. The stacks work fine the que simply wont come out right here is the revised code. Pls help if you can.
Code:
#include<iostream>
const int maxsize = 50;
class stackchar
{
public: stackchar();
void push(char newval);
char top2();
void pop();
bool isfull();
bool isempty();
private:
char s2[maxsize];
char s1[maxsize];
char s3[maxsize];
int top;
};
class charqueue
{
public: charqueue();
bool isempty2();
bool isfull2();
char front2();
void enqueue(int val);
void dequeue();
private:
char q[maxsize];
int rear;
int front;
};
-----------------------------------------------------------------------
#include "s2p1.h"
#include<iostream>
const int vecsize = maxsize;
charqueue::charqueue()
{
rear = front = 0;
}
bool charqueue::isempty2()
{
return(rear == front);
}
bool charqueue::isfull2()
{
return((rear+1)%vecsize == front);
}
void charqueue::enqueue(int val)
{
rear =(val)%vecsize;
}
void charqueue::dequeue()
{
front = (front+1)%vecsize;
}
char charqueue::front2()
{
return q[front+1%vecsize];
}
stackchar::stackchar()
{
top=-1;
}
bool stackchar::isfull()
{
return(top == maxsize-1);
}
bool stackchar::isempty()
{
return(top == -1);
}
void stackchar::push(char newval)
{
s2[++top] = newval;
}
char stackchar::top2()
{
return s2[top];
}
void stackchar::pop()
{
top--;
}
---------------------------------------------------------------------
#include"s2p1.cpp"
#include<iostream>
#include<fstream.h>
#include <string>
#include <istream>
int main()
{
stackchar s1;
stackchar s2;
stackchar s3;
charqueue q;
char ch[50];
int i;
bool test1(stackchar s1, stackchar s3, stackchar s2);
bool test2(stackchar s1, charqueue q);
cout<<"Please enter a sentence: ";
cin.getline(ch, 49);
for(i = 0; i<strlen(ch); ++i)
{
s1.push(ch[i]);
s2.push(ch[i]);
q.enqueue(ch[i]);
}
while(!s2.isempty())
{
cout<<s2.top2();
s3.push(s2.top2());
s2.pop();
}
cout<<'\n';
bool b1 = test1(s1, s3, s2);
if (b1 == false)
{
cout<<"this sentence is not a palindrome if applying stacks."<<'\n';
}
else
{
cout<<"This sentence is a palindrome if applying stacks."<<'\n';
}
while(!s2.isempty())
{
s1.push(s2.top2());
s2.pop();
}
bool b2 = test2( s1, q);
if (b2 == false)
{
cout<<"This sentence is not a palindrome if applying stack and queue. "<<'\n';
}
else
{
cout<<"This sentence is a palindrome if applying stack and queue.";
}
return 0;
}
//------------------------------------------------------------------------------------------------------------------
bool test1(stackchar s1, stackchar s3, stackchar s2)
{
while(!s1.isempty())
{
if(s1.top2() == s3.top2())
{
s2.push(s1.top2());
s1.pop();
s3.pop();
}
else return false;
}
return true;
}
bool test2(stackchar s1, charqueue q)
{
while(!s1.isempty())
{
if (s1.top2() == q.front2() )
{
s1.pop();
q.dequeue();
}
else return false;
}
return true;
}
My only problem now is it gives the wrong response for que. It always said false.