Пример #1
0
void Solver::getAllCells(Table matrix, Cell* startingCell){
    
    LinkedQueue<Cell*> linkedQueue;
    linkedQueue.enqueue(startingCell);
    
    Cell* newCell = NULL;
    while (!linkedQueue.isEmpty()) {
        
        Cell* curCell;
        linkedQueue.dequeue(curCell);
        curCell->mark();
        dArray.addElement(curCell);
        for(int index = 0; index < 4; ++index){
            
            if(matrix.validNextMove(curCell, index)){
                
                newCell = matrix.getElement(curCell->getRow() + dx[index], curCell->getCol() + dy[index]);
                addIfPossibleAndNotVissited(linkedQueue, newCell);
                
            }
        }
    }
}
Пример #2
0
int main()
{
	ifstream inputFile;
	string fileName;  // Holds the user-inputted file name
	string fileLine;  // Holds the line extracted from the file
	string expression; // Holds the expression to be tested
	bool ableToOpen;  // Flag to see if file exists
	bool isBalanced;  // Flag for balanced expression

	cout << "Create queue object lq\n\n";
	LinkedQueue<string> lq;

	cout << "Get the fle input\n\n";
	do
	{	// Ask user for file name and attempt to open
		cout << "Enter input file name: ";
		cin >> fileName;
		ableToOpen = openFileIn(inputFile, fileName);
		if (!ableToOpen)
			cout << fileName << " cannot be opened." << endl;
	} while (!ableToOpen);

	// Read lines from the file and put into queue object
	getline(inputFile, fileLine);
	while (inputFile)
	{
		lq.enqueue(fileLine);
		getline(inputFile, fileLine);
	} // end while
	inputFile.close();

	cout << "\n\n\nAfter getting the file input, test other functions*******\n";
	cout << "Create lq1, a copy of the queue\n\n";
	LinkedQueue<string> lq1(lq);
	cout << "Display the contents of the copy queue lq1:\n";
	cout << lq1 << endl;

	cout << "\nAttempt to peek the now empty queue lq1:\n";
	try
	{
		lq1.peekFront();
	}
	catch (PrecondViolatedExcep e)
	{
		cout << e.what();
	}

	cout << "\nAssign lq to lq1 and then display lq1:\n";
	lq1 = lq;
	cout << lq1;

	cout << "\nPut the first string in lq into a stack of chars, ls1\n\n";
	LinkedStack<char> ls1;
	string firstLine = lq.peekFront();
	for (unsigned int i = 0; i < firstLine.length(); i++)
	{
		ls1.push(firstLine[i]);
	} // end for

	cout << "Create a copy, ls2, of the stack and display the copy:\n";
	LinkedStack<char> ls2(ls1);
	cout << ls2;

	cout << "\n\nAssign the first stack, ls1, to the second stack, ls2, and then display the second stack:\n";
	ls2 = ls1;
	cout << ls2;

	cout << "\n\n\n\nDo the expression checking:\n\n\n";
	while (!lq.isEmpty())
	{
		expression = lq.peekFront();
		lq.dequeue();
		cout << "The next string is:\t" << expression;
		isBalanced = checkExpression(expression);
		if (isBalanced)
			cout << "\tis a correct expression\n\n\n";
		else
			cout << "\tis NOT a correct expression\n\n\n";
	} // end while

	// Program Over
	cout << "Program Over\n\n";
	cin.ignore();
	cout << "Press Enter to end --> ";
	cin.ignore();
	cout << endl;
	return 0;
} // end main