Exemplo n.º 1
0
StackLinked<DataType>::StackLinked(const StackLinked& other){
	if(this != other){
		while(!other->isEmpty()){
			this->push(other->pop());
		}
	}
}
Exemplo n.º 2
0
bool Maze::traverse()
{
   //DO THIS
   //complete several sections in this method

   bool done = false; //assume we are not done unless proven otherwise
   StackLinked<Cell> stack;

   maze->setElement(1, 1, TRIED);
   gui->update();

   Cell* start_cell = new Cell(1, 1);
   stack.push(start_cell);  //start from the top left corner

   while(!stack.isEmpty())
   {
      Cell* top_cell = processBackTrack(&stack);
      if (top_cell == NULL) break;  //no solution (back tracked all the way to the beginning)

      //call a method in the Cell class to give you a new Cell in a new direction relative to top_cell (initially, DOWN)
      //DO THIS
      Cell* curr_cell = top_cell -> nextCell();

      //does this new Cell solve the maze?
      done = isSolved(curr_cell, &stack);
      if (done) break;

      //DO THIS
      //get the row and col from curr_cell
      int row = curr_cell -> getRow();
      int col = curr_cell -> getCol();
      double element = maze -> getElement(row, col);

      //check that the current cell corresponds to SPACE, otherwise delete it
      if (element == SPACE)
      {
         //update the cell to TRIED
         //put the cell on the stack (move forward through the maze)
         maze -> setElement(row, col, TRIED);
         stack.push(curr_cell);

         Sleep(75);  //slow down the maze traversal
         gui->update();
      }
      else //look for a different route 
      {
         //DO THIS
         //delete the cell
         delete curr_cell;
      }
   }

   //did we make it to the bottom right?
   if (done)
   {
      processSolution(&stack);
   }
   else
   {
      cout << "No solution." << endl;
   }

   return done;
}
Exemplo n.º 3
0
double calculatePostfixExpression(char* expression) {
  // variables
  int ndx = 0;
  char current_char = char(4);
  StackLinked<double> C;
  StackLinked<double>* contents = new StackLinked<double>;
  double operand1 = -8.88;    // arbitrary values for garbage
  double operand2 = -8.88;    // clearing and debugging
  double result = -8.88;

  // calculate the expression
  for(ndx = 0; ndx < MAX_EXPRESSION; ndx++) {
    // extract item from expression
    current_char = expression[ndx];

    // take appropriate action
    switch(current_char) {
      case '\0':
        // stop processing when \0 is encountered 
        ndx = MAX_EXPRESSION;   // this will break loop
        break;

      // ignore whitespace
      case ' ':
        // do nothing
        break;

      // store numbers in stack
      case '0':
      case '1':
      case '2':
      case '3':
      case '4':
      case '5':
      case '6':
      case '7':
      case '8':
      case '9':
        contents->push( double(current_char - '0') );
        break;

      // perform specified operations
      case '+':
         // gather operands
         operand2 = contents->pop();
         operand1 = contents->pop();

         // do the math
         result = operand1 + operand2;

         // store the result
         contents->push(result);
         break;

      case '-':
         // gather operands
         operand2 = contents->pop();
         operand1 = contents->pop();

         // do the math
         result = operand1 - operand2;

         // store the result
         contents->push(result);
         break;

      case '*':
         // gather operands
         operand2 = contents->pop();
         operand1 = contents->pop();

         // do the math
         result = operand1 * operand2;

         // store the result
         contents->push(result);
         break;

      case '/':
         // gather operands
         operand2 = contents->pop();
         operand1 = contents->pop();

         // do the math
         result = operand1 / operand2;

         // store the result
         contents->push(result);
         break;

      default:
        // ignore all other characters, i.e. do nothing
        break;
    }
  }

  // save the result before returning dynamic memory
  result = contents->pop();
  
  // return dynamic memory
  delete contents;
  contents = NULL;

  // return the result
  return result;
}