-
Queue
Ok I ve rewritten the code. I got the stacks to work beautifully but got to the queue and hit a snag. I m getting an error that says unresolved external 'charqueue::front2()' refenenced from s2p1.obj.
[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];
int top;
char newval;
};
class charqueue
{
public: charqueue();
bool isempty2();
bool isfull2();
void enqueue(int val);
char front2();
void dequeue();
private:
char s1[maxsize];
int front;
int rear;
};
----------------------------------------------------------------------------
#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 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();
}
bool b1 = test1(s1, s3);
if (b1 == false)
{
cout<<"this sentence is not a palindrome if applying stacks.";
}
else
{
cout<<"This sentence is a palindrome if applying stacks.";
}
bool b2 = test2(s1, q);
if (b2 == false)
{
cout<<"This sentence is not a palindrome if applying stack and queue. ";
}
else
{
cout<<"This sentence is a palindrome if applying stack and queue.";
}
return 0;
}
//------------------------------------------------------------------------------------------------------------------
bool test1(stackchar s1, stackchar s3)
{
while(!s1.isempty())
{
if(s1.top2() == s3.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;
}
Any help is appreciated. Thank you.
-
try changing
Code:
char charqueue::front2()
{
return q[(front+1)%vecsize];
}
to
Code:
char charqueue::front2()
{
return s1[(front+1)%vecsize];
}
-
Thanks for the try but that gave me about 2 pages worth of similar errors to all my functions.
-
i edited my post, try my new idea
-
Main problem with that is S1 is the array for my stack I need to compair S1 to q to get my results.
-
Noone have other suggestions? I still am getting knowhere though have fixed a few other problems I saw coming up.
-
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.
-
so you dont have to wade through all the code here are the spots I think the problem lies in.
Code:
for(i = 0; i<strlen(ch); ++i)
{
s1.push(ch[i]);
s2.push(ch[i]);
q.enqueue(ch[i]);
}
void charqueue::enqueue(int val)
{
rear =(val)%vecsize;
}
void charqueue::dequeue()
{
front = (front+1)%vecsize;
}
char charqueue::front2()
{
return q[front+1%vecsize];
}
-
How do you expect it to enqueue anything if you don't
modifiy q. Something like this but you might have to
change it.
q[front] = val;
front++;
front %= vecsize;
-
I knew that was the problem but I really didn't know how to fix it. tks I will try that.
-
-
You have to define what you mean by front and rear.
Is q[front] the top element of the queue or the first
empty position above the top element?
If you define front and rear so that front is the index to
the top element and rear is the index to the back index
you would enqueue into the front like this
front++;
front %= vectsize;
q[front] = val;
Adding some checks for full arrays.
Dequeueing could be done like this
back++
back %= vectsize;
You would start out with
back = 1
front = 0