int main()
{
    LinkedQueue<string> frontmen;
    frontmen.enqueue("Dave Grohl");
    frontmen.enqueue("Bono");
    frontmen.enqueue("Chester Bennington");
    while (!frontmen.empty())
    {
        cout << frontmen.front() << endl;
        cout << frontmen.size() << endl;
        frontmen.dequeue();
    }
    return 0;
}
Exemple #2
0
void copyConstructorTester()
{
	LinkedQueue<string> queue;
   string items[] = {"zero", "one", "two", "three", "four", "five"};
	for (int i = 0; i < 6; i++)
   {
		cout << "Adding " << items[i] << endl;
      bool success = queue.enqueue(items[i]);
      if (!success)
         cout << "Failed to add " << items[i] << " to the queue." << endl;
	}
   cout << "Queue contains, from front to back, zero one two three four five." << endl;
   
   LinkedQueue<string> copyOfQueue(queue);
   cout << "Copy of queue contains, from front to back, ";
	for (int i = 0; i < 6; i++)
   {
      cout << " " << copyOfQueue.peekFront();
      copyOfQueue.dequeue();
   }
   cout << "." << endl;
   
   cout << "Original queue contains, from front to back,";
	for (int i = 0; i < 6; i++)
   {
      cout << " " << queue.peekFront();
      queue.dequeue();
   }
   cout << "." << endl << endl;
}  // end copyConstructorTester
void main()
{
	LinkedQueue LQ;
	string command;

	cout << "추가 : en, 제거 : de, 종료 : quit" << endl;

	while (true)
	{
		try
		{
			cin >> command;

			if (command.compare("en") == 0)
			{
				int data;
				cin >> data;
				LQ.enqueue(data);
			}
			else if (command.compare("de") == 0)
				LQ.dequeue();
			else if (command.compare("quit") == 0)
				return;

			LQ.show();
			cout << endl;
		}
		catch (const char *s)
Exemple #4
0
void Solver::addIfPossibleAndNotVissited(LinkedQueue<Cell*>& queue, Cell* pCell){
    
    if(pCell && !pCell->isVisited() && !pCell->isWall()){
        
        pCell->mark();
        queue.enqueue(pCell);
        
    }
}
Exemple #5
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);
                
            }
        }
    }
}
Exemple #6
0
void horse_queue(Point start, Point end) {
	LinkedQueue<Position> q;
	q.enqueue(start);
	board[start.first][start.second] = true;
	Position current;
	LinkedStack<LinkedQueue<Position> > history;
	LinkedQueue<Position> level;
	int currentMove = 0;
	history.push(level);
	while (!q.empty() && (current = q.dequeue()).p != end) {
		if (current.move == currentMove) {
			history.peek().enqueue(current);
		} else {
			// current.move == currentMove + 1
			currentMove = current.move;
			LinkedQueue<Position> level;
			level.enqueue(current);
			history.push(level);
		}
		for(int dx = -2; dx <= 2; dx++)
			if (dx != 0)
				for(int sign = -1; sign <= 1; sign += 2) {
					int dy = sign * (3 - abs(dx));
					Point newstart(current.p.first + dx,
								   current.p.second + dy);
					Position newposition(newstart, current.move + 1);
					if (inside_board(newstart)
						&&
						!board[newstart.first][newstart.second]) {
						q.enqueue(newposition);
						clog << newposition << endl;
						board[newstart.first][newstart.second] = true;
					}

				}
	}
	// current == end -- успех
	// q.empty() -- неуспех
	clog << history;
	history.pop();
	if (current.p == end) {
		cout << "Успех за " << current.move << " хода!" << endl;
		LinkedStack<Point> path;
		path.push(current.p);
		while(!history.empty()) {
			// в history.peek() търсим първата позиция position,
			// за която isValidMove(current.p,position.p)
			Position position;
			while (!isValidMove(current.p,
					           (position = history.peek().dequeue()).p));
			// знаем, че isValidMove(current.p,position.p)
			// добавим position в пътя
			path.push(position.p);
			history.pop();
			current = position;
		}
		cout << path << endl;
	}
	else
		cout << "Неуспех!" << endl;
}
Exemple #7
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