Ejemplo n.º 1
0
Archivo: main.c Proyecto: sebnow/mush
void run()
{
	char *input = NULL;
	char *prompt = NULL;
	queue_t *commandQueue = NULL;
	MushErrorCode errorCode;
	do {
		prompt = getPrompt();
		printf("%s", prompt);
		/* Free input buffer from previous loop run */
		if(input != NULL) {
			free(input);
		}
		input = (char *)getInput();
		commandQueue = commandQueueFromInput(input);
		if(commandQueue == NULL && mushError() != kMushNoError) {
			fprintf(stderr, "mush: %s\n", mushErrorDescription());
		} else {
			errorCode = executeCommandsInQueue(commandQueue);
			if(errorCode != 0 && mushError() != kMushNoError) {
				fprintf(stderr, "mush: %s\n", mushErrorDescription());
			}
			queueFree(commandQueue);
		}
	} while(1);
}
int qExit(Queue *q, productList *products)
{
    int code;
    clearScreen();
    printf("\n\n\n\tThank you for using SUPER MARKET COUNTER!\n");
    printf("\n\tPress any key to exit!");
    
    while(queueIsEmpty(q)==0)
                             freeCustomer(queueRemove(q));
    
    queueFree(q);
    
    code = freeProductList(products);
    if(code==0)
    {
                                    getch();
                                    exit(code);
    }
    else
    {
        printf("Error: Freeing memory failed!");
        wait();
        exit(code);
    }
}
Ejemplo n.º 3
0
void deInitChunkQueue(PLCP_ChunkQueue* queue)
{
	// we can't deInit a non-empty queue
	if (queue->length > 0)
	{
		return;
	}

	queueFree(queue);

	// Logging
	calLogInt(CAL_LOG_DEBUG, "deInitChunkQueue", "QUEUE-DEALLOCATED-B", sizeof(PLCP_ChunkQueue));
}
Ejemplo n.º 4
0
PMD_Chunk* dequeueChunk(PLCP_ChunkQueue* queue)
{
	// Logging
	calLogString(CAL_LOG_DEBUG, "dequeueChunk", "FUN-CALLED", "Attempt to dequeue a chunk");

	// return if the queue is empty
	if (chunkQueueLength(queue) == 0)
	{
		// Logging
		calLogString(CAL_LOG_DEBUG, "dequeueChunk", "CHUNK-DEQUEUE", "Chunk queue is empty");
		return NULL;
	}

	// temp node (needed for the free)
	PLCP_ChunkQueueNode* temp = queue->head;

	// if there is only one element, the tail and head pointers point to the same node (to be extracted)
	if (queue->length == 1)
	{
		// so set them to NULL
		queue->tail = queue->head = NULL;
	}
	else
	{
		// otherwise, we have other nodes, the new head must be updated to the next one
		queue->head = temp->next;
	}

	// give the element pointer back to the user
	PMD_Chunk* element = temp->data;

	// substract the length
	queue->length--;

	// free the node
	queueFree(temp);

	// Logging
	calLogInt(CAL_LOG_DEBUG, "dequeueChunk", "QUEUE-NODE-DEALLOCATED-B", sizeof(PLCP_ChunkQueueNode));

	// return the needed element
	return element;
}
Ejemplo n.º 5
0
int main()
{
    int i, j, k, nNodes, nEdges, initialNode, finalNode, nCritAreas, nCritPoints, criticPoint, *results, trash = 0;

    trash = scanf(" %d %d", &nNodes, &nEdges);

    graph.nNodes = nNodes;
    graph.neighbors = (int **) malloc(sizeof(int *) * nNodes);
    graph.nNeighbors = (int *) malloc(sizeof(int) * nNodes);
    graph.nodesInSearch = (int **) malloc(sizeof(int *) * nNodes);

    queueInit(nNodes);

    parent = (int *) malloc(sizeof(int) * nNodes);
    visited = (int *) malloc(sizeof(int) * nNodes);

    for(i=0; i<nNodes; i++) {
        graph.nNeighbors[i] = 0;
        graph.neighbors[i] = NULL;
        graph.nodesInSearch[i] = NULL;
        parent[i] = -1;
        visited[i] = -1;
    }

    for (i=0; i<nEdges; i++) {
        trash = scanf(" %d %d", &initialNode, &finalNode);

        graph.neighbors[initialNode] = realloc(graph.neighbors[initialNode],
            sizeof(int) * (graph.nNeighbors[initialNode] + 1));
        graph.neighbors[initialNode][graph.nNeighbors[initialNode]] = finalNode;

        graph.neighbors[finalNode] = realloc(graph.neighbors[finalNode],
            sizeof(int) * (graph.nNeighbors[finalNode] + 1));
        graph.neighbors[finalNode][graph.nNeighbors[finalNode]] = initialNode;

        graph.nodesInSearch[initialNode] = realloc(graph.nodesInSearch[initialNode],
                                                   sizeof(int) * (graph.nNeighbors[initialNode] + 1));
        graph.nodesInSearch[initialNode][graph.nNeighbors[initialNode]] = 0;


        graph.nodesInSearch[finalNode] = realloc(graph.nodesInSearch[finalNode],
                                                   sizeof(int) * (graph.nNeighbors[finalNode] + 1));
        graph.nodesInSearch[finalNode][graph.nNeighbors[finalNode]] = 0;

        graph.nNeighbors[finalNode]++;
        graph.nNeighbors[initialNode]++;
    }

    trash = scanf(" %d", &nCritAreas);

    results = (int *) malloc(sizeof(int) * nCritAreas);
    criticPoints = (int **) malloc (sizeof(int *) * nCritAreas);
    nCriticPoints = (int *) malloc(sizeof(int) * nCritAreas);

    for(i = 0; i < nCritAreas; i++) {
        trash = scanf(" %d", &nCritPoints);
        criticPoints[i] = (int *) malloc(sizeof(int) * nCritPoints);
        nCriticPoints[i] = nCritPoints;
        for(j = 0; j < nCritPoints; j++) {
            trash = scanf( " %d", &criticPoint);
            criticPoints[i][j] = criticPoint;
        }
    }

    for (k = 0; k < nCritAreas; k++) {
        results[k] = INT_MAX;
        for (i = 0; i < nCriticPoints[k]; i++) {
            for (j = i+1; j < nCriticPoints[k]; j++) {
                trash = 0;
                queueReset();
                trash = fordFulkerson(criticPoints[k][i], criticPoints[k][j]);
                results[k] = results[k] < trash ? results[k] : trash;
            }

        }
    }


    for (i = 0; i < nCritAreas; i++) {
        printf("%d\n", results[i]);
    }

    /* Frees */
    for (i=0; i<graph.nNodes; i++) {
        free(graph.neighbors[i]);
        free(graph.nodesInSearch[i]);
    }

    free(graph.neighbors);
    free(graph.nNeighbors);
    free(graph.nodesInSearch);
    free(parent);
    free(visited);
    queueFree();
    free(results);
    for (i = 0; i < nCritAreas; i++) { free(criticPoints[i]); }
    free(criticPoints);
    free(nCriticPoints);
    return 0;
}
Ejemplo n.º 6
0
int                   main(void)
{
  queueHead*          queue;
  int                 i1 = 1;
  int                 i2 = 2;
  int                 i3 = 3;
  int                 i4 = 4;

  /*
   * Test 1
   */

  /* Create queue */
  assert(NULL != (queue = queueCreate()));

  /* Fill the queue */
  assert(1    == (queueAdd(queue, &i1)));      /* Insert 1 */
  assert(1    == (queueAdd(queue, &i2)));      /* Insert 2 */
  assert(1    == (queueAdd(queue, &i3)));      /* Insert 3 */
  assert(1    == (queueAdd(queue, &i4)));      /* Insert 4 */

  /* Check the queue */
  assert(1    == (*(int*)queueFirst(queue)));  /* Check  1 */
  assert(1    == (*(int*)queueRemove(queue))); /* Remove 1 */
  assert(2    == (*(int*)queueFirst(queue)));  /* Check  2 */
  assert(2    == (*(int*)queueRemove(queue))); /* Remove 2 */
  assert(3    == (*(int*)queueFirst(queue)));  /* Check  3 */
  assert(3    == (*(int*)queueRemove(queue))); /* Remove 3 */
  assert(4    == (*(int*)queueFirst(queue)));  /* Check  4 */
  assert(4    == (*(int*)queueRemove(queue))); /* Remove 4 */

  assert(NULL == (queueFirst(queue)));         /* Check empty    */
  assert(NULL == (queueRemove(queue)));        /* Queue is empty */

  /* Clear the queue */
  queueFree(queue);

  printf("Queue: Test1 success!\n");

  /*
   * Test 2
   */

  /* Create queue */
  assert(NULL != (queue = queueCreate()));

  /* Fill the queue */
  assert(1    == (queueAdd(queue, &i1)));      /* Insert 1 */
  assert(1    == (*(int*)queueRemove(queue))); /* Remove 1 */

  assert(1    == (queueAdd(queue, &i1)));      /* Insert 1 */
  assert(1    == (queueAdd(queue, &i2)));      /* Insert 2 */
  assert(1    == (*(int*)queueRemove(queue))); /* Remove 1 */
  assert(2    == (*(int*)queueRemove(queue))); /* Remove 2 */

  assert(1    == (queueAdd(queue, &i1)));      /* Insert 1 */
  assert(1    == (*(int*)queueFirst(queue)));  /* Check  1 */
  assert(1    == (queueAdd(queue, &i2)));      /* Insert 2 */
  assert(1    == (*(int*)queueRemove(queue))); /* Remove 1 */
  assert(1    == (queueAdd(queue, &i3)));      /* Insert 3 */
  assert(2    == (*(int*)queueRemove(queue))); /* Remove 2 */
  assert(1    == (queueAdd(queue, &i4)));      /* Insert 4 */

  assert(3    == (*(int*)queueRemove(queue))); /* Remove 3 */
  assert(4    == (*(int*)queueRemove(queue))); /* Remove 4 */

  /* Fill the queue */
  assert(1    == (queueAdd(queue, &i1)));      /* Insert 1 */
  assert(1    == (queueAdd(queue, &i2)));      /* Insert 2 */
  assert(1    == (queueAdd(queue, &i3)));      /* Insert 3 */
  assert(1    == (queueAdd(queue, &i4)));      /* Insert 4 */

  /* Check the queue */
  assert(1    == (*(int*)queueRemove(queue))); /* Remove 1 */
  assert(2    == (*(int*)queueFirst(queue)));  /* Check  2 */
  assert(2    == (*(int*)queueRemove(queue))); /* Remove 2 */
  assert(3    == (*(int*)queueRemove(queue))); /* Remove 3 */
  assert(4    == (*(int*)queueRemove(queue))); /* Remove 4 */

  assert(NULL == (queueFirst(queue)));         /* Check empty    */
  assert(NULL == (queueRemove(queue)));        /* Queue is empty */

  /* Clear the queue */
  queueFree(queue);

  printf("Queue: Test2 success!\n");

  return 0;
}