Exemplo n.º 1
0
int main()
{
	Queue<int> q;
	q.Push(1);
	q.Push(2);
	q.Push(3);
	int h=q.Front();
	cout<<h<<endl;
	h=q.Front();
	cout<<h<<endl;
}
Exemplo n.º 2
0
int main()
{
    Queue<int> queue;
    queue.Enqueue(10);
    queue.Enqueue(20);
    cout<<queue.IsEmpty()<<endl;
    cout<<queue.IsFull()<<endl;
    cout<<queue.Front()<<endl;
    queue.Dequeue();
    cout<<queue.Front()<<endl;
    return 0;
}
Exemplo n.º 3
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.º 4
0
bool testQueue()
{
	Queue<int> myQ;
	myQ.Enqueue(1);
	myQ.Enqueue(2);
	myQ.Enqueue(3);

	assert(myQ.Front() == 1);

	myQ.Dequeue();
	assert(myQ.Front() == 2);

	Queue<int> myQcopy(myQ);
	assert(myQcopy.Front() == 2);
	myQcopy.Dequeue();
	assert(myQcopy.Front() == 3);

	return true;
}
Exemplo n.º 5
0
int main(){

  Queue q;

  for(int i=0;i<9;i++){
    q.Enqueue(i);
  }

  q.print();
  std::cout << "front is : "<< q.Front() << std::endl;

  for(int i=0;i<=10;i++){
    q.Dequeue();
  }

  q.print();
  std::cout << "front is : "<< q.Front() << std::endl;


  return 0;
}
Exemplo n.º 6
0
int main()
{
    Stack S;
    Queue Q;
    
    // insert values 10, 20, 30, 40 and 50 onto the stack
    for(int x = 1; x<=5; x++){
        int n = x*10;
        S.push(n);
        Q.AddQ(n);
    }
    
    
    //Display the content of the stack and queue to the screen
    std::cout<<"Contents of Stack"<<std::endl;
    S.display();
    std::cout<<endl<<std::endl;
    std::cout<<"Contents of Queue"<<std::endl;
    Q.display();
    std::cout<<endl<<std::endl;
    

    //Remove and display each value on the stack
    std::cout<<"Removing values from Stack"<<std::endl;
    while (!S.empty())
    {    int x;
        S.Top(x);
        std::cout<<std::endl;
        std::cout<<"Popping --- "<<x<<endl;
        S.pop();
        S.display();
    }
    
    if (S.empty())
        std::cout<<"Stack is empty."<<std::endl;
    
    
    //Remove and display each values on the Queue
    std::cout<<"Removing values from Queue"<<std::endl;
    while (!Q.empty())
    {    int x;
        Q.Front(x);
        std::cout<<std::endl;
        std::cout<<"Removing --- "<<x<<endl;
        Q.RemoveQ();
        Q.display();
    }
    
    if (Q.empty())
        std::cout<<"Queue is empty."<<std::endl;
}
Exemplo n.º 7
0
int main(int argc, char** argv)
{
	// Stack test
	Stack<int> s;
	s.Push(10);
	s.Push(20);
	s.Push(30);
	s.Push(40);
	s.Push(1);
	s.Push(2);
	s.Push(3);
	s.Push(4);
	s.Push(5);
	s.Push(6);
	s.Push(7);
	s.Push(8);
	while(!s.IsEmpty())
	{
		printf("%d\n", s.Top());
		s.Pop();
	}
	printf("-----\n");
	
	// Queue test
	Queue<int> q;
	q.Enque(10);
	q.Enque(20);
	q.Enque(30);
	q.Enque(40);	
	q.Enque(1);
	q.Enque(2);
	q.Enque(3);
	q.Enque(4);
	q.Enque(5);
	q.Enque(6);
	q.Enque(7);
	q.Enque(8);
	while(!q.IsEmpty())
	{
		printf("%d\n", q.Front());
		q.Deque();
	}
	printf("-----\n");

	// Maze test
	path(argv[1]);
		
	return 0;
}
void LevelOrders(Tree T){
	Queue<node_t> Q;
	node_t tmp = Root(T);
	while (Label(tmp,T) != ""){
		cout << Label(tmp,T);
		node_t child = Leftmost_Child(tmp,T);
		while (Right_Sibling(child,T) != 0){
			Q.Enqueue(child);
			child = Right_Sibling(child,T);
		}
		Q.Enqueue(child);
		tmp = Q.Front();
		Q.Dequeue();
	}

}
Exemplo n.º 9
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.º 10
0
void Matrix::BFS(Point start, int rows, int cols, LinkedList<Point>& path)
{
	if (matrix[start.getX()][start.getY()] == '#')
	{
		cout << "The position is invalid!\n";
		return;
	}

	int dr[] = { 0, -1, 0, 1 };
	int dc[] = { -1, 0, 1, 0 };
	Queue<Point> queue;

	queue.Enqueue(start);
	matrix[start.getX()][start.getY()] = '?';

	cout << "Starting from point: ";
	cout << "(" << start.getX() << ", " << start.getY() << ")" << " : " << endl;

	while (!queue.isEmpty())
	{
		start = queue.Front();
		queue.Dequeue();

		for (int d = 0; d < 4; d++)
		{
			Point nextPos(start.getX() + dr[d], start.getY() + dc[d]);
			
			if (canPass(rows, cols, nextPos))
			{
				path.Push_Back(nextPos);
				queue.Enqueue(nextPos);

				matrix[nextPos.getX()][nextPos.getY()] = '?';			
			}
		}
	}
}
Exemplo n.º 11
0
int main(void){
    
    int N = 10;    

    // instantiate a Queue that stores integers
    //
    Queue<int> *q = new Queue<int>();
     
    for(int i=1; i < N; ++i)
        q->Enqueue(i); 
    q->PrintItems();
    if(q->IsEmpty()) cout << "Queue is empty\n";    

    // Access the front in the Queue
    //
    cout << "Front Item: " << q->Front() << endl;
    cout << "Removing Front Item\n";
    // Remove the front item in the Queue
    q->Dequeue();

    // Print the Items
    q->PrintItems();
    return 0;
}
int main()
{
	Queue<string> myStringQueue;

	myStringQueue.Push( "Hello" );
	myStringQueue.Push( "Saluton" );
	myStringQueue.Push( "Konnichiwa" );
	myStringQueue.Push( "Bonjour" );
	myStringQueue.Push( "Hola" );

	Stack<string> myStringStack;

	myStringStack.Push( "Hello" );
	myStringStack.Push( "Saluton" );
	myStringStack.Push( "Konnichiwa" );
	myStringStack.Push( "Bonjour" );
	myStringStack.Push( "Hola" );

	int count = 0;
	while ( myStringStack.GetSize() != 0 )
	{
		cout << "Loop " << count << endl;
		cout << "\t Stack: " << myStringStack.Top() << endl;
		cout << "\t Queue: " << myStringQueue.Front() << endl;
		cout << endl;
		count++;

		myStringStack.Pop();
		myStringQueue.Pop();
	}

	cout << "Press a key to continue..." << endl;
	cin.get();

	Queue<int> myIntQueue;

	myIntQueue.Push( 1 );
	myIntQueue.Push( 1 );
	myIntQueue.Push( 2 );
	myIntQueue.Push( 3 );
	myIntQueue.Push( 5 );

	Stack<int> myIntStack;

	myIntStack.Push( 1 );
	myIntStack.Push( 1 );
	myIntStack.Push( 2 );
	myIntStack.Push( 3 );
	myIntStack.Push( 5 );

	count = 0;
	while ( myIntStack.GetSize() != 0 )
	{
		cout << "Loop " << count << endl;
		cout << "\t Stack: " << myIntStack.Top() << endl;
		cout << "\t Queue: " << myIntQueue.Front() << endl;
		cout << endl;
		count++;

		myIntStack.Pop();
		myIntQueue.Pop();
	}

	cout << "Press a key to exit..." << endl;
	cin.get();

	return 0;
}
Exemplo n.º 13
0
void QueueTest()
{
	Queue<int> aqueue;

	cout << "Testing isEmpty(): " << aqueue.isEmpty() << endl;
	cout << "Testing Enqueue()" << endl;
	for (int i = 0; i < 10; i++)
	{
		try
		{
			aqueue.Enqueue(i);
		}
		catch (Exception & except)
		{
			cout << except << endl;
		}
	}

	cout << "Testing isEmpty(): " << aqueue.isEmpty() << endl;
	cout << endl << "Testing Dequeue()" << endl;
	for (int i = aqueue.Size(); i >= 0; i--)
	{
		try
		{
			aqueue.Dequeue();
		}
		catch (Exception & except)
		{
			cout << except << endl;
		}
		cout << aqueue.Size() << endl;
	}

	for (int i = 0; i < 10; i++)
	{
		aqueue.Enqueue(i);
		cout << aqueue.Front() << endl;
	}

	cout << endl << "Testing copy constructor" << endl;
	Queue<int> bqueue = aqueue;
	cout << endl << "BSIZE " << bqueue.Size() << endl << endl;
	for (int i = bqueue.Size(); i > 4; i--)
	{
		cout << bqueue.Dequeue() << endl;
	}

	cout << endl << "Testing assignment operator" << endl;
	aqueue = bqueue;

	cout << "Testing isEmpty(): " << aqueue.isEmpty() << endl;
	for (int i = aqueue.Size(); i > 0; i--)
	{
		cout << aqueue.Dequeue() << endl;
	}

	cout << "Testing the ability to manipulate data at the front of the queue" << endl;
	aqueue.Enqueue(99);
	int &num = aqueue.Front();

	cout << "The value stored at the front of the queue is: " << num << endl;
	num = num + 1;
	cout << "The value stored at the front of the queue is now: " << aqueue.Front() << endl;
}
Exemplo n.º 14
0
void MatrixGraph::bfs()
{
	bool* visit = new bool[size];
	memset(visit, false, size);
	Queue<int> queue;

	int visited = 0;	//	0번 vertex부터 출발
	visit[visited] = true;

	queue.Push(visited);

	int connecctComponent = 1;	//	연결요소 개수

	IoHandler ioh;
	ioh.putString("인접행렬 + BFS");
	ioh.putNewLine();

	for (int i = 0; i < size; i++)
	{

		//	다음 vertex로 넘어왔는데 false면 연결요소가 끊어진 것
		if (visit[i] == false)
		{
			ioh.putString("연결요소");
			ioh.putInteger(connecctComponent++);
			ioh.putString(" - ");

			while (1)
			{	//	출력
				ioh.printConnectedComponent(queue.Front());
				queue.Pop();

				if (queue.isEmpty() == true)
				{
					break;
				}
			}
			ioh.putNewLine();
		}

		if (visit[i] == false)
		{
			visit[i] = true;
			queue.Push(i);
		}


		for (int j = 0; j < size; j++)
		{
			if (adjMatrix[i][j] == true)
			{
				if (visit[j] == false)
				{
					visit[j] = true;
					queue.Push(j);
				}
			}
		}
	}	//	for end

	if (queue.isEmpty() == false)
	{
		ioh.putString("연결요소");
		ioh.putInteger(connecctComponent++);
		ioh.putString(" - ");

		while (1)
		{	//	출력
			ioh.printConnectedComponent(queue.Front());
			queue.Pop();

			if (queue.isEmpty() == true)
			{
				break;
			}
		}
		ioh.putNewLine();
	}

	return;
}