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.

Recommended Answers

All 3 Replies

stack and queue are also not defined in the scope

They should be std::stack and std::queue.

q and s are not defined in the scope

Since stack and queue are not defined, the compiler cannot define s and q as well. The namespace resolution (above) should take care about it.

That fixed it thank you! However, when i call insert_equivalent in my testing code i get an error saying that it is an undefined reference to it, I don't understand what that means and how to fix it. Again thank you for the reply and any additional help would be appreciated

// FILE: assignment4Demo.cxx
// A demo program for programming assignment 4
// Written by Shivakant Mishra
// Last Update: April 23, 2010

#include <iostream>
#include <cassert>    // Provides assert
#include <cstdlib>    // Provides NULL and size_t
#include <stack>    // STL stack
#include <queue>    // STL queue
#include "assignment4.h"
using namespace std;
using namespace tobedetermined;

int main( )
{
	char str1[20] = "(())";
	char str2[20] = "({({}{()})()}";
	char str3[20] = "";
	char str4[20] = "{(({))}}";

	if (balanced(str1)) cout << "str1 is balanced" << endl;
	else cout << "str1 is not balanced" << endl;
	if (balanced(str2)) cout << "str2 is balanced" << endl;
	else cout << "str2 is not balanced" << endl;
	if (balanced(str3)) cout << "str3 is balanced" << endl;
	else cout << "str3 is not balanced" << endl;
	if (balanced(str4)) cout << "str4 is balanced" << endl;
	else cout << "str4 is not balanced" << endl;

	stack<int> *s = new stack<int>;
	queue<int> *q = new queue<int>;
	queue<int> *nq = new queue<int>;
	for (int i = 0; i<10; i++) {
		s->push(i);
		q->push(i);
		nq->push(9-i);
	}
	if (insert_equivalent(s, q))
		cout << "s and q are insert equivalent" << endl;
	else cout << "s and q are not insert equivalent" << endl;
	if (insert_equivalent(s, nq))
		cout << "s and nq are insert equivalent" << endl;
	else cout << "s and nq are not insert equivalent" << endl;

}

However, when i call insert_equivalent in my testing code i get an error saying that it is an undefined reference to it, I don't understand what that means and how to fix it.

This means that you didn't set up your project correctly.

PS: using namespace is a very bad practice. Get used to explicit namespace resolution.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.