Hi, first year programmer and the assignment is to use the stack and queue classes to write two functions. One checks to see if an array of parenthesis are balanced, the other to see if a stack and queue are insert equivalent. Here is my code.
Assignment4.h
#include <iostream>
#include <cassert>how to code a template in the header c++
#include <cstdlib>
#include <stack>
#include <queue>
namespace tobedetermined
{
bool insert_equivalent (stack<Item> *s, queue<Item> *q);
bool balanced(char* p);
template <class Item>
bool insert_equivalent (stack<Item> *s, queue<Item> *q)
{
int slength = s->size();
int qlength = q->size();
if(slength != qlength)
{
return false;
}
Item* sarray = new Item[slength];
Item* qarray = new Item[qlength];
for(int x = slength; x > 0; x--)
{
sarray[x] = s->top(); //stores the stack into an array
s->pop();
}
for(int x = 0; x < qlength; x++)
{
qarray[x] = q->front(); //stores the queue into an array
q->pop();
}
for(int x = 0; x < slength; x++)
{
s->push(sarray[x]); //restores the stack
}
for(int x = qlength; x > 0; x--)
{
q->push(qarray[x]); //restores the queue
}
for(int x = 0; x < slength; x++)
{
if(sarray[x] != qarray[x])
{
return false;
}
}
return true;
}
}
Assignment4.cxx
#include <iostream>
#include <cassert>
#include <cstdlib>
#include <stack>
#include <queue>
#include "assignment4.h"
using namespace std;
namespace tobedetermined
{
bool balanced(char* p)
// Precondition: Each character of string p is '(', ')', '{' or
// '}'.
// Postcondition: The method returns true if the characters form a
// sequence of correctly balanced parentheses with each '('
// matching a ')' and each '{' matching a '}'. Note that a
// sequence such as ( { ) } is NOT balanced. On the other hand,
// ( { } ) and { ( ) } are both balanced.
{
int length = strlen(p);
if(length == 0)
{
cout << "empty string";
return false;
}
bool flag;
stack<char> stackvar;
if(p[0] == ')'|| p[0] == '}')
{
return false;
}
stackvar.push(p[0]);
for(int x = 1; x < length; x++)
{
flag = true;
if(p[x] == '(' || p[x] == '{')
{
stackvar.push(p[x]);
}
else
{
if(p[x] == '}' && stackvar.top() == '{')
{
cout << "test";
stackvar.pop();
flag = false;
}
if(p[x] == ')' && stackvar.top() == '(')
{
if(flag == true)
{
cout << "test1";
stackvar.pop();
flag = false;
}
}
if(p[x] == '}' && stackvar.top() == '(')
{
if(flag)
{
cout << "test2";
return false;
}
}
if(p[x] == ')' && stackvar.top() == '{')
{
if(flag)
{
cout << "test3";
return false;
}
}
}
}
return true;
}
}
}
The error i am getting is in the header file
it says that q and s are not defined in the scope, and that stack and queue are also not defined in the scope. This confuses me to no end as they are include in the file and I'm just completely stuck and anything I do just brings up more errors, any help would be greatly appreciated.