Exemplo n.º 1
0
int main()
{
    //queue<char*> words;
    Queue<char*> words;
	//Kristian: Липсват подсказващи съобщения.
    int n;
    cin>>n;
    //81094: I would insert spaces around the operators for a better esthetical view.
	//Kristian: Решението определено не решава задачата. При аргумент 3 имаш повтарящи се поддуми. Пример: aaa.
	//Kristian: При аргумент 4, не се генерират всички възможни поддуми. Пример: липсва bbbb.Освен това, отново има повтарящи се: abcd.
    for(int i=1;i<=n;i++)
    {
        int* arr=new int[i];
        int symbol=(int)'a';
        for(int j=0;j<pow(3,i);j++)
        {
            IntToAnyNumSystem(j,n,arr,i);
            char* word=new char[i];
            for(int i1=0;i1<i;i1++)
                word[i1]=(char)(arr[i1]+symbol);
            word[i]='\0';
            words.Enqueue(word);
        }
        delete[] arr;
    }
    while(!words.Empty())
       printf ("%s \n", words.Dequeue());
    return 0;
}
Exemplo n.º 2
0
int main()
{
	Queue Q;

	for(int i = 0; i < 10; ++i)
	{
		int* p = new int(i);
		Q.EnQueue(p);
		cout << "Size = " << Q.Size() << endl;
	}

	for(QueueNode* node = Q.Head(); node != 0; node = node->next)
	{
		int* p =(int*)node->data;
		cout << *p<< " ";
	}
	cout << endl;


	while(Q.Empty() == false)
	{
		int* p = (int*)Q.DeQueue();
		cout << *p << " ";
		cout << "Size = " << Q.Size() << endl;
		
	}
	cout << endl;
	return 0;
}
Exemplo n.º 3
0
/*----- S o l v e M a z e (  ) -----

PURPOSE
Attempt to find the shortest path through the maze.

INPUT PARAMETERS
maze           -- the maze object to be traversed
positionQueue  -- the queue of current and future positions

RETURN VALUE
true  -- a path was found.
false -- failed to find a path.
*/
bool SolveMaze(Maze &maze, Queue &positionQueue)
{
/*
const int Open     = -1;	// Cell is open
const int Obstacle = -2;	// Cell is an obstacle
const int StartCell= -3;	// Cell is the start cell
const int GoalCell = -4;	// Cell is the goal cell
const int PathCell = -5;	// Cell is on the shortest path
*/ 
	Position curPos = maze.Start();
	Position neighbor;
	positionQueue.Enqueue(curPos);
	maze.Mark(curPos, 0);
	int distance;

	while(!positionQueue.Empty()){
		curPos = positionQueue.Dequeue();
		distance = maze.State(curPos);
		neighbor = openPosition(maze, curPos);
		while(curPos != neighbor){
			maze.Mark(neighbor, distance + 1);
			if(neighbor == maze.Goal())
				return true;
			positionQueue.Enqueue(neighbor);
			neighbor = openPosition(maze, curPos);
		}
	}
	return false;
}
Exemplo n.º 4
0
int main()
{
   Queue<int> line;                                  // Line-up waiting to get in

   int patrons = InitialPatrons;                     // Number people in the Inn

   int time, i, arrivals, departures, entry_time;

   Randomize();                                      // Seed the random numbers

   for (time=0; time<300; time++)                    // Each minute from 8 - 1.
   {
      arrivals = RandomNum(num_arrive[time/60]);     // arriving this minute

      for (i=0; i<arrivals; i++) line.Enqueue(time); // End of the line

      departures = RandomNum(num_depart[time/60]);   // leaving this minute

      patrons -= departures;                         // bye-bye

      while (patrons < Capacity && !line.Empty())
      {
         entry_time = line.Front();                  // move from line into Inn
         line.Dequeue();
         patrons++;
      }
      cout << setw(4) << time << ":  " << arrivals << "  " << departures
           << setw(5) << patrons << endl;
   }

   cout << setw(4) << time << ":  " << arrivals << "  " << departures
        << setw(5) << patrons << endl;

   return (0);
}
Exemplo n.º 5
0
void Test()
{
	Queue<int> q;
	q.Push(1);
	q.Push(2);
	q.Push(3);
	q.Push(4);
	cout<<q.Front()<<endl<<endl;
	cout<<q.Back()<<endl<<endl;
	cout<<q.Empty()<<endl<<endl;
	cout<<q.Size()<<endl<<endl;
	q.Pop();
	q.Pop();
	q.Pop();
	q.Pop();
	q.Pop();
	cout<<endl<<q.Empty()<<endl<<endl;
}
Exemplo n.º 6
0
/*----- S o l v e M a z e (  ) ------------------------------

PURPOSE
Attempt to find the shortest path through the maze.

INPUT PARAMETERS
maze           -- the maze object to be traversed
positionQueue  -- the queue of current and future positions

RETURN VALUE
true  -- a path was found.
false -- failed to find a path.
-------------------------------------------------------------*/
bool SolveMaze(Maze &maze, Queue &positionQueue)
{
	maze.Mark(maze.Start(), 0);					// Mark the maze start with distance 0
	positionQueue.Enqueue(maze.Start());		// Add maze start to queue
	CellState distance = 0;						// cell distance from start

	while (!positionQueue.Empty())
	{
		while (((maze.State(positionQueue.Head() + StepEast)) == Open)			// While head position has any unmarked neighbors
			|| ((maze.State(positionQueue.Head() + StepSouth)) == Open) 
			|| ((maze.State(positionQueue.Head() + StepWest)) == Open) 
			|| ((maze.State(positionQueue.Head() + StepNorth)) == Open)) 	
		{
			distance = maze.State(positionQueue.Head());										// Set distance

			if ((maze.State(positionQueue.Head() + StepEast)) == Open)							// Is east cell open?
			{
				maze.Mark(positionQueue.Head() + StepEast, distance + 1);						// Mark cell with proper distance
				if ((positionQueue.Head() + StepEast) == maze.Goal())							// Is open cell the goal?
					return true;
				
				positionQueue.Enqueue(positionQueue.Head() + StepEast);							// Add it to the queue
			}
			else if ((maze.State(positionQueue.Head() + StepSouth)) == Open)					// Is south cell open?
			{
				maze.Mark(positionQueue.Head() + StepSouth, distance + 1);						// Mark cell with proper distance
				if ((positionQueue.Head() + StepSouth) == maze.Goal())							// Is open cell the goal?
					return true;
				
				positionQueue.Enqueue(positionQueue.Head() + StepSouth);						// Add it to the queue
			}
			else if ((maze.State(positionQueue.Head() + StepWest)) == Open)						// Is West cell open?
			{
				maze.Mark(positionQueue.Head() + StepWest, distance + 1);						// Mark cell with proper distance
				if ((positionQueue.Head() + StepWest) == maze.Goal())							// Is open cell the goal?
					return true;
				
				positionQueue.Enqueue(positionQueue.Head() + StepWest);							// Add it to the queue
			}
			else if ((maze.State(positionQueue.Head() + StepNorth)) == Open)					// Is North cell open?
			{
				maze.Mark(positionQueue.Head() + StepNorth, distance + 1);						// Mark cell with proper distance
				if ((positionQueue.Head() + StepNorth) == maze.Goal())							// Is open cell the goal?
					return true;
				
				positionQueue.Enqueue(positionQueue.Head() + StepNorth);						// Add it to the queue
			}
		}
		positionQueue.Dequeue();
	}
	return false;
}
Exemplo n.º 7
0
void MinHblt<T>::LevelOrder(HbltNode<T>* t) {
    Queue<HbltNode<T> *> q;
    while (t != NULL) {
        Visit_(t);
        if (t->left_ != NULL)
            q.push(t->left_);
        if (t->right_ != NULL)
            q.push(t->right_);
        if (q.Empty()) {
            return;
        }
        else {
            t = q.Remove();
        }
    }
}
Exemplo n.º 8
0
int main()
{
    Queue<int, 3> Q;

    try
    {
        Q.Enqueue(1);
        Q.Enqueue(3);
        Q.Enqueue(4);
        while (!Q.Empty())
            std::printf("%d\n", Q.Dequeue());
    }
    catch (Queue<int, 3>::BadOp& e)
    {
        std::puts("Queue::BadOp caught!");
    }
}
Exemplo n.º 9
0
int main() {
    Elem n1 = Elem(12, NULL);
    Elem n2 = Elem(99, NULL);
    Elem n3 = Elem(37, NULL);
    Queue q = Queue();

    q.Insert(n1);
    q.Insert(n2);
    q.Insert(n3);

    q.Print();

    Elem *n;
    while (!q.Empty()) {
        n = q.RemoveFirst();
        cout << "remove " << n->state << endl;
    }
}
Exemplo n.º 10
0
/*
It would be sufficient to only flush messages belonging to object th
But then the order of sent messages is not as intended
*/
void flext_base::QFlush(flext_base *th)
{
    FLEXT_ASSERT(!IsThreadRegistered());
    while(!queue.Empty()) QWork(false,th);
}