Beispiel #1
0
void Queue_is_Empty()
{
    Queue Q;
    Q = CreateQueue();
    assert(IsEmpty(Q) == true); 
    DisposeQueue(Q);
}
Beispiel #2
0
int main( )
{
    Queue Q;
    int i;

    Q = CreateQueue( 12 );

    FAIL_ON_NULL(Q->data)
    
    i=0;
    for( i = 0; i < 10; i++ )
        Enqueue( i, Q );

    while( !IsEmpty( Q ) )
    {
        printf( "%d\n", Front( Q ) );
        Dequeue( Q );
    }
    for( i = 0; i < 20; i++ )
        Enqueue( i, Q );

    while( !IsEmpty( Q ) )
    {
        printf( "%d\n", Front( Q ) );
        Dequeue( Q );
    }

    DisposeQueue( Q );
    return 0;
}
int main()
{
	Queue TestQueue = CreateQueue(10);
	cout << "Begin test of queue " << endl;
	for (int i = 0, n = 1; i < 10; i++, n *= 2)
	{
		cout << i + 1 << "> Enqueue : " << n<<endl;
		Enqueue(n, TestQueue);
	}
	PrintQueue(TestQueue);
	cout << "Is Full ? " << IsFull(TestQueue)<<endl;

	for (int i = 0; i < 5; i++)
	{
		cout << i + 1 << " >Dequeue :" << Front(TestQueue) << endl;
		Dequeue(TestQueue);
	}
	PrintQueue(TestQueue);
	cout << "Front and dequeue: " << FrontAndDequeue(TestQueue)<<endl;
	cout << "Now add more data to test the cicular array..." << endl;
	for (int i = 0; i < 5; i++)
	{
		cout << i + 1 << "> Enqueue : " << i << endl;
		Enqueue(i, TestQueue);
	}
	PrintQueue(TestQueue);
	cout << "Now make the queue empty...";
	MakeEmpty(TestQueue);
	cout << "Is Empty ? "<<IsEmpty(TestQueue)<<endl;
	cout << "Now dipose the queue!" << endl;
	DisposeQueue(TestQueue);
	cout << "Test Succeed!" << endl << "Good bye!"<<endl;
	getchar();
}
Beispiel #4
0
void Queue_is_not_Empty()
{
    Queue Q;
    Tree tree[10] = {(Tree )0x1, (Tree )0x2, (Tree )0x3, (Tree )0x4, (Tree )0x5, (Tree )0x6, (Tree )0x7, (Tree )0x8, (Tree )0x9, (Tree )0xA };
    Tree tmp;

    Q = CreateQueue();
    Enqueue(tree[0], Q);
    assert(IsEmpty(Q) == false); 
    //Print(Q);
    tmp = FrontAndDequeue(Q);
    assert(tmp == tree[0]);
    DisposeQueue(Q);
}
Beispiel #5
0
int SearchReuse(Queue readyQ, struct PE *pRRs, int qSize )
{

	Queue qtmp;
	qtmp=CreateQueue(qSize);
	ElementType tmp,task=-1;
	int found=NO;

	while(!IsEmpty(readyQ))
	{
		tmp=FrontAndDequeue(readyQ);
		if ( (ReusePRR_V2(dfg1[tmp].TypeID, pRRs)) >=0 && !found)
		{
			task=tmp;
			found=YES;
		}else
		{
			Enqueue(tmp,qtmp);
		}



	}

	MakeEmpty(readyQ);
	if (task>=0){
		Enqueue(task,readyQ);
	}
	while(!IsEmpty(qtmp))
		{
			tmp=FrontAndDequeue(qtmp);
			Enqueue(tmp,readyQ);

		}

	DisposeQueue(qtmp);
	return task;



}
Beispiel #6
0
void Queue_is_correct_for_En_and_Dequeue()
{
    Queue Q;
    int i;
    Tree tree[10] = {(Tree )0x1, (Tree )0x2, (Tree )0x3, (Tree )0x4, (Tree )0x5, (Tree )0x6, (Tree )0x7, (Tree )0x8, (Tree )0x9, (Tree )0xA };
    Tree tmp;

    Q = CreateQueue();
    for(i=0; i<10; i++) {
        Enqueue(tree[i], Q);
    }

    assert(IsEmpty(Q) == false); 
    //Print(Q);
    for(i=0; i<10; i++) {
        tmp = FrontAndDequeue(Q);
        assert(tmp == tree[i]);
    }
    assert(IsEmpty(Q) == true); 
    DisposeQueue(Q);
}
Beispiel #7
0
int main(void){
	Queue Q;
	int i;

	Q = CreateQueue();
	for(i=0;i<10;i++)
		EnQueue(i,Q);
	while(!IsEmpty(Q)){
		printf("%d\n",Front(Q));
		DeQueue(Q);
	}
	for(i=0;i<10;i++)
		EnQueue(i,Q);
	while(!IsEmpty(Q)){
		printf("%d\n",Front(Q));
		DeQueue(Q);
	}
	DisposeQueue(Q);

	return 0;
}
Beispiel #8
0
int main(int argc, char * argv[])
{
	int idblocom; //Identificador do Bloco de memória
	int fifod; //fifo file descriptor
	pthread_t tidSecr, tidReS;
	pthread_t *tidMec, *tidEle, *tidCha, *tidPin;
	int duracao,nMec, nEle, nCha, nPin;
	int i;
	
	Qmec = CreateQueue(15);		// queue mecanico
	Qele = CreateQueue(15);		// queue electricista
	Qcha = CreateQueue(15);		// queue chapeiro
	Qpin = CreateQueue(15);		// queue pintor	
	Qdone = CreateQueue(15);	//queue final
	
	if(argc!=6){		// verificação do input
		printf("\nUso: %s [TempodeFuncionamento] [nrMecanicos] [nrElectricistas] [nrChapeiros] [nrPintores]\n", argv[0]);
		return -1;
	}
	
	// guarda os argumentos do programa
	duracao = atoi(argv[1]);
	nMec = atoi(argv[2]);
	nEle = atoi(argv[3]);
	nCha = atoi(argv[4]);
	nPin = atoi(argv[5]);
	
	//abertura/Criação do ficheiro registos.dat
	if((registos = fopen(REGISTO, "wt"))==NULL){
		printf("Failled to create file: registos.dat .\n");
		return 1;
	}
	pthread_mutex_lock(&mutexRegis);
	regista(CHEFE, NULL, "Abriu a Oficina");
	pthread_mutex_unlock(&mutexRegis);

	//Criação do FIFO
	if(mkfifo(FIFO, 0660)!=0){
		perror(FIFO);
	}
	else
		fprintf(registos, "FIFO Criado\n");
	
	//Alocar memoria
	if((idblocom = aloca_mem(argv[0]))==1){
		return 1;
	}
	
	//Criar o Semaforo com key SEMKEY
	semid=semcreate(SEMKEY,0);
	if (semid==-1)
	{
		printf("Chefe: Erro na criacao do semaforo c/key = %d\n",SEMKEY);
		exit(1);
	}

	
	// Inicia o funcionamento da oficina!!
	work = 1;
	if(pthread_create(&tidSecr,NULL, recepcionista_C, (void *) &fifod)!=0){
		perror("Recepcionista C");
	}
	
	tidMec = (pthread_t *)calloc(nMec, sizeof(pthread_t));
	for(i=0; i<nMec; i++){		//Multi-thread
		if(pthread_create(&tidMec[i],NULL, mecanico, (void*)(i+1))!=0){
			perror("Mecanico");
		}
	}
	
	tidEle = (pthread_t *)calloc(nEle, sizeof(pthread_t));
	for(i=0; i<nEle; i++){		//Multi-thread
		if(pthread_create(&tidEle[i],NULL, electricista, (void*)(i+1))!=0){
			perror("Electricista");
		}
	}
	
	tidCha = (pthread_t *)calloc(nCha, sizeof(pthread_t));
	for(i=0; i<nCha; i++){		//Multi-thread
		if(pthread_create(&tidCha[i],NULL, chapeiro, (void*)(i+1))!=0){
			perror("Chapeiro");
		}
	}
	
	tidPin = (pthread_t *)calloc(nPin, sizeof(pthread_t));
	for(i=0; i<nPin; i++){		//Multi-thread
		if(pthread_create(&tidPin[i],NULL, pintor, (void*)(i+1))!=0){
			perror("Pintor");
		}
	}

	if(pthread_create(&tidReS,NULL, recepcionista_S,NULL)!=0){
		perror("Recepcionista S");
	}
	
	//O chefe pos a malta a trabalhar e agora vai tirar uma sesta...
	pthread_mutex_lock(&mutexRegis);
	regista(CHEFE, NULL, "Agora vou trabalhar... bem, dormir...");
	pthread_mutex_unlock(&mutexRegis);
	sleep(duracao);
	pthread_mutex_lock(&mutexRegis);
	regista(CHEFE,NULL, "Dei sinal para fechar a oficina! Bam'bora!");	
	pthread_mutex_unlock(&mutexRegis);
	work = 2;
	//printf("CHEFE: Dei sinal de terminar\n");
	
	sleep(60);	// aguarda 60 unidades de tempo
	feito = 1;	// encerra o funcionamento
	
	// aguarda que os threads terminem
	pthread_join(tidSecr,NULL);
	pthread_join(tidReS,NULL);
	
	for(i=0; i<nMec; i++) {
		pthread_join(tidMec[i],NULL); }

	for(i=0; i<nEle; i++) {
		pthread_join(tidEle[i],NULL); }
	
	for(i=0; i<nCha; i++) {
		pthread_join(tidCha[i],NULL); }
	
	for(i=0; i<nPin; i++) {
		pthread_join(tidPin[i],NULL); }	
		
		
	//desalocar a memoria
	if(desaloca_mem(idblocom) ==1){
		return 1;
	}
	else{
		pthread_mutex_lock(&mutexRegis);
		regista(CHEFE, NULL, "Parque fechado");
		pthread_mutex_unlock(&mutexRegis);
	}
	
	//elimina as filas
	//printf("Dispose of the queue...\n");
	DisposeQueue(Qmec);
	DisposeQueue(Qele);
	DisposeQueue(Qcha);
	DisposeQueue(Qpin);
	DisposeQueue(Qdone);
	
	
	//destroi os mutexes
	//printf("Destruir os mutexes\n");
	
	pthread_mutex_destroy(&mutexRegis);
	pthread_mutex_destroy(&mutexMec);
	pthread_mutex_destroy(&mutexEle);
	pthread_mutex_destroy(&mutexCha);
	pthread_mutex_destroy(&mutexPin);
	pthread_mutex_destroy(&mutexDone);

	//Destroi o semaforo
	//printf("Destruir o Semaforo\n");
	semremove(semid);
	
	unlink(FIFO);
	return 1;
	
	//adeus e boa viagem!
}
Beispiel #9
0
int Isomorphic( Tree T1, Tree T2 )
{
    Queue Q1 = CreateQueue();
    Queue Q2 = CreateQueue();

    Tree tree1 = T1;
    Tree tree2 = T2;

    bool doExit = false;
    bool result = false;

    RebuildTreeInOrder(tree1);
    RebuildTreeInOrder(tree2);
//   PrintTreeInfixOrder(tree1);
//   printf("\n");
//   PrintTreeInfixOrder(tree2);
//   printf("\n");

    Enqueue(tree1, Q1);
    Enqueue(tree2, Q2);
    while((!IsEmpty(Q1)) && (!IsEmpty(Q2))) { 
        tree2 = FrontAndDequeue(Q2);
        tree1 = FrontAndDequeue(Q1);

        if(tree1 && (!tree2)) {
            result = false; 
            doExit = true;
            break;
        }
        if((!tree1) && tree2) {
            result = false; 
            doExit = true;
            break;
        }

        if(tree1 && tree2) {
            if(tree1->Element != tree2->Element) {
                result = false;
                doExit = true;
                break;
            }

            Enqueue(tree1->Left, Q1);
            Enqueue(tree1->Right, Q1);

            Enqueue(tree2->Left, Q2);
            Enqueue(tree2->Right, Q2);
        }
    }

    if(doExit) {
        DisposeQueue(Q1);
        DisposeQueue(Q2);
        return false;
    }

    if(IsEmpty(Q1) && IsEmpty(Q2)) {
        result = true;
    }

    DisposeQueue(Q1);
    DisposeQueue(Q2);
    return result;
}
Beispiel #10
0
int MoveDependentTask2TheFront(Queue readyQ,int qSize , int t)
{

	Queue qtmp;
	qtmp = CreateQueue(qSize);
	ElementType tmp, task1 = -1, task2 = -1;
	int found1 = NO;
	int found2 = NO;
	int NoAdd1 = NO;
	int NoAdd2 = NO;

	if (st[t])
		return 0;
	while (!IsEmpty(readyQ)) {
		tmp = FrontAndDequeue(readyQ);
		NoAdd1 = NoAdd2 = NO;
		if (!found1) {
			if (dfg1[t].D.isAdd_op1 && !isTaskDone(dfg1[dfg1[t].D.op1].D.op1)) {
				if (tmp == dfg1[t].D.op1) {
					st[t] = 1;
					task1 = tmp;
					found1 = YES;
					NoAdd1 = YES;

				}
			}
		}

		if (!found2) {
			if (dfg1[t].D.isAdd_op2 && !isTaskDone(dfg1[dfg1[t].D.op2].D.op2)) {
				if (tmp == dfg1[t].D.op2) {
					st[t] = 1;
					//	fprintf(stderr,"FoundTask_2 [%d] is dependent on task [%d] \n",t,dfg1[dfg1[t].D.op2].D.op2);
					task2 = tmp;
					found2 = YES;
					NoAdd2 = YES;

				}
			}
		}

		if (!NoAdd1 && !NoAdd2) {
			Enqueue(tmp, qtmp);
		}

	}

	MakeEmpty(readyQ);
	if (task1 >= 0) {
		Enqueue(task1, readyQ);
	}
	if (task2 >= 0) {
		Enqueue(task2, readyQ);
	}
	while (!IsEmpty(qtmp)) {
		tmp = FrontAndDequeue(qtmp);
		Enqueue(tmp, readyQ);

	}

	DisposeQueue(qtmp);
	return found1 || found2;

}