Beispiel #1
0
void insertLeft (DQUEUE_p_t queue, char * item) {
	if (!isEmptyQueue(*queue) && queue->front == 0) {
		printf("\n\tQUEUE LEFT OVERFLOW!\n\n");
		return;
	}
	
	if (isEmptyQueue(*queue))
		queue->front = queue->rear = 0;
	else
		queue->front -= 1;
	
	queue->capacity += 1;
	
	*(queue->arr + queue->front) = item;
}
Beispiel #2
0
void bfs(int startNode)
{
    printf("BFS started\n");
     int * visited = (int*)malloc(nrOfVerteces * sizeof(int));
    int i, v, w;

    for(i=0; i < nrOfVerteces; i++)
    {
        visited[i]=UNVISITED;
    }

    visited[startNode] = VISITED;
    printf("%c ", startNode+65);
    enqueue(startNode);

    while(!isEmptyQueue())
    {
        v = peekQueue()->content;
        dequeue();
        int nrOfNeighbors = getNumberOfNeighborsOfVertex(v);
        int * neighbors = getAllNeighborsOfVertex(v);
        for(i=0; i<nrOfNeighbors; i++)
        {
            w = neighbors[i];
            if(visited[w] == UNVISITED)
            {
                printf("%c ", w+65);
                enqueue(w);
                visited[w] = VISITED;
            }
        }
    }
    printf("\nBFS ended\n\n");
}
Beispiel #3
0
int getQueue(queue q, void ** e)
{
	node * n;
	assert(q);

	if(!e) return -1;

	if(isEmptyQueue(q))
	{
		*e = NULL;
		return -2;
	}

	assert(q->head);
	
	n = q->head;
	*e = n->e;
	if(q->size == 1)
	{
		assert(!n->prev);
		q->head = NULL;
	}
	else q->head = n->prev;

	deleteNode(n);
	q->size -= 1;

	return 0;
}
Beispiel #4
0
void bfs(nodeT** adjList, int searchNode)
{
    int *Visited, *neighbors;
    nodeT *V;
    int i,nr,w;

    Visited = (int *)calloc(nrOfVerteces, sizeof(int));
    Visited[searchNode]=1;
    printf("%c, ", searchNode+65);
    enqueue(searchNode);
    while (!isEmptyQueue())
    {
        V=peekQueue();
        dequeue();
        neighbors=getAllNeighborsOfVertex(V->code, adjList);
        nr=getNumberOfNeighborsOfVertex(V->code, adjList);
        for (i=0; i<nr; i++)
        {
            w=neighbors[i];
            if (Visited[w]==0)
            {
                enqueue(w);
                Visited[w]=1;
                printf("%c, ",w+65);
            }
        }
    }
}
Beispiel #5
0
void bfsMatrix(int searchNode)
{
    int* visited=(int*)malloc(nrOfVertices*sizeof(int));
    int i;
    for (i=0; i<nrOfVertices; i++)
    {
        visited[i]=UNVISITED;
    }
    visited[searchNode]=VISITED;
    enqueue(searchNode);
    printf("%d ",searchNode);
    while(!isEmptyQueue())
    {
        int v=peekQueue()->content;
        dequeue();
        int* aux=getAllNeighborsOfVertexFromMatrix(v);
        for (i=0; i<getNumberOfNeighborsOfVertexFromMatrix(v); i++)
        {
            int w=aux[i];
            if (visited[w]==UNVISITED)
            {
                visited[w]=VISITED;
                enqueue(w);
                printf("%d ",w);
            }
        }
    }
    printf("\n");
}
Beispiel #6
0
void bfs(int startNode)
{
    printf("BFS started\n");
  int *mark=(int*)malloc(sizeof(int)*nrOfVerteces);
  int i;
    for (i=0;i<nrOfVerteces;i++)
    {
        mark[i]=UNVISITED;
    }
  mark[startNode]=VISITED;
  enqueue(startNode);
  printf("%d ",startNode);
  while (!isEmptyQueue())
  {
    int v=peekQueue()->content;
    dequeue();
    int nrOfNeighbours=getNumberOfNeighborsOfVertex(v);
    int *neighbours=getAllNeighborsOfVertex(v);
    for (i=0;i<nrOfNeighbours;i++)
    {
        int w=neighbours[i];
        if (mark[w]==UNVISITED)
        {
            enqueue(w);
            mark[w]=VISITED;
            printf("%d ",w);
        }
    }
  }
    printf("\nBFS ended\n\n");
}
Beispiel #7
0
// Обрабатываем событие
void *worker(void *args) {
    // Получаем аргументы в новом потоке
    struct WorkerArgs *workerArgs = args;
    while (!done) {
        // Ожидание поступления события в очередь
        pthread_mutex_lock(workerArgs->mutex);
        while (isEmptyQueue(workerArgs->queue)) {
            pthread_cond_wait(workerArgs->condition, workerArgs->mutex);
        }
        struct epoll_event event;
        popQueue(workerArgs->queue, &event);
        pthread_mutex_unlock(workerArgs->mutex);
        if (event.data.fd == socketfd) {
            int connectionfd = acceptConnection();
            if (connectionfd == -1) {
                fprintf(stderr, "Error: accepting new connection\n");
                continue;
            }
            if (handleEvent(connectionfd) == -1) {
                fprintf(stderr, "Error: handling event\n");
                continue;
            }
        } else {
            if (handleEvent(event.data.fd) == -1) {
                fprintf(stderr, "Error: handling event\n");
                continue;
            }
        }
    }
    return NULL;
}
Beispiel #8
0
void insertUnique(State s, Queue *qp, State *mem){
    int i = 0;
    while(mem[i]!=NULL){
        if(mem[i] == s){
            return;
        }
        i++;
    }
	List previous;
	if(!isEmptyQueue(*qp)) {
		previous = qp->list;
			if(compare(s, previous->item)==-1){
				qp->list = addItem(s, previous);
			}else{
				while((previous->next) != NULL){
					if(compare(s, (previous->next)->item)==-1){
						previous->next = addItem(s, previous->next);
						return;
					}
					if(compare(s, previous->next->item)==0){
						return;
						
						 }
						previous = previous->next;
				}
				(qp->lastNode)->next = addItem(s, NULL);
				qp->lastNode = (qp->lastNode)->next;
			}
	}else{
	qp->list = addItem(s, NULL);
	qp->lastNode = qp->list;
	}
}
Beispiel #9
0
void breadthFirstSearch(Graph *graph, int vertex) {
    int visited[SIZE];
    int i, currVertex;
    Queue *queue;
    for(i = 0; i < graph->V; i++) {
        visited[i] = 0;    
    }
    queue = initQueue(graph->V);
    enqueue(queue, vertex);
    
    while(!isEmptyQueue(queue)) {
        currVertex = dequeue(queue);
        visited[currVertex] = 1;
        printf(" %d", currVertex);

        for(i = 0; i < graph->V; i++) {
            //if there is an edge and that vertex is not visited
            if((graph->edgeMat[currVertex][i] == 1) && (visited[i] == 0)) {
                enqueue(queue, i);
            }
        }
    }

    releaseQueue(queue);
}
int isPathBfs(Graph g, Vertex v, Vertex w)
{
	int isPath = FALSE;
	Queue q = newQueue(); // create a new queue
	Vertex curV = 0;
	int *visited = calloc(g->nV, sizeof(int)); // allocate + init to 0
	int i = 0;

	assert(visited != NULL);
	enQueue(q, v);
	visited[v] = TRUE;

	while ( !isEmptyQueue(q) ){ // still have vertices to traverse
		curV = deQueue(q); // get a vertex from the queue
		printf("Visiting: %d.\n", curV);
		visited[curV] = TRUE; // mark it as visited
		if (curV == w) { isPath = TRUE; } 
		for (i = 0; i < g->nV; i++){ // find vertices to add to queue
			if (g->edges[curV][i] && !visited[i]){ // found a vertex to add
				enQueue(q, i); // add it to queue
				visited[i] = TRUE; // mark it as visited
			}
		}
	}	
	free(visited);
	deleteQueue(q);
	return isPath;
}
Beispiel #11
0
void lBfs(int startNode)
{
    int *visited = (int*)malloc(nrOfVertices*sizeof(int));
    int i, v, w;

    for (i = 0; i < nrOfVertices; i++)
        visited[i] = UNVISITED;
    visited[startNode] = VISITED;
    printf("\n%d", startNode);
    enqueue(startNode);
    while(!isEmptyQueue())
    {
        v = peekQueue();
        dequeue();
        for (i = 0; i < getNumberOfNeighborsOfVertex(v); i++)
        {
            w = *(getAllNeighborsOfVertex(v) + i);
            if (visited[w] == UNVISITED)
            {
                visited[w] = VISITED;
                printf("\n%d", w);
                enqueue(w);
            }
        }
    }
}
int Dequeue(queue *Q)
{
    int temp=0;
    if(isEmptyQueue(Q))
        printf("Queue is empty!!!! Deletion not possible !!! Queue underflow");
    else
    {
        if(Queuesize(Q)==1)
        {
           temp=Q->head;
           Q->head=Q->tail=-1;
        }
        else if(Q->head==Q->size-1)
        {
            Q->head=0;
            temp=Q->size-1;
        }
        else
        {
            ++Q->head;
            temp=Q->head-1;
        }


    }
    return Q->array[temp];
}
Beispiel #13
0
char * deleteRight (DQUEUE_p_t queue) {
	if (isEmptyQueue(*queue)) {
		printf("\n\tQUEUE EMPTY!\n\n");
		return UNDERFLOW_CHAR;
	}
	
	char * item = *(queue->arr + queue->front);
	
	queue->rear -= 1;
	queue->capacity -= 1;
	
	if (isEmptyQueue(*queue))
		queue->front = queue->rear = -1;
	
	return item;
}
Beispiel #14
0
/**
 * 队列queue的队首元素出列
 */
void pop(Queue * queue) {
	if(!isEmptyQueue(queue)){
		queue->head++;
		printf("队首元素出列!\n");
	}else{
		printf("这是个空队列!\n");
	}
}
Beispiel #15
0
int getFirst(Queue* q, int* firstItem) {
	int ret = 0;
	if (!isEmptyQueue(q)) {
		*firstItem = q->first->element;
		ret = 1;
	}
	return ret;
}
Beispiel #16
0
int main(){
	
	Queue *fila;
	int i, val;
	
	fila = createQueue();
	initializeQueue(fila);
	
	srand(time(NULL));

	for (i=10; i>0; i--)
	{

		if (enqueue(fila, rand()%30), &IntCmp)
		{
			printf("O valor foi inserido\n");
		}
		else
		{
			printf("Não há mais espaço na fila\n");
		}
	
	}
	for (i=0; i<4; i++)
	{
		if (dequeue(fila, &val))
		{
			printf("--%d\n", val);
		}
		else
		{
			printf("Não há nada na fila\n");
		}
	}

	if (peek(fila, &val)) 
	{
		printf("peek: %d\n", val);
	}
	else
	{
		printf("Não há nada na fila\n");
	}
	
	printf("Tamanho da fila: %d\n", sizeQueue(fila));
	
	if (isEmptyQueue(fila))
	{
		printf("A fila está vazia\n");
	}
	else
	{
		printf("A fila não está vazia\n");
	}	 	
	
	printQueue(fila);
return(0);
}
Beispiel #17
0
/**
 * 返回队列queue的队首元素
 */
int front(Queue * queue) {
	if(!isEmptyQueue(queue)){
		return queue->data[queue->head];
	}else{
		printf("队列为空队列!\n");
	}

	return 0;
}
Beispiel #18
0
int peekQueue() {
  assert(initialized);
  if (isEmptyQueue()) {
    printf("UNDERFLOW\n");
    exit(-1);
  } else {
    return memory[front].data.value;
  }
}
Beispiel #19
0
void display (DQUEUE_t queue) {
	if (!isEmptyQueue(queue)) {
		printf("\n | Current Queue (f = %d, r = %d, c = %d) : \n", queue.front, queue.rear, queue.capacity);
		int i;
		for (i = queue.front; i <= queue.rear; ++i)
			printf("\t\t%d | %s", i,  *(queue.arr + i));
		printf("\n\n");
	}
}
Beispiel #20
0
int Dequeue(struct ArrayQueue *Q){
	
	if(isEmptyQueue(Q))
		//printf("%d",isEmptyQueue);
		return NULL;
	Q->num--;
	return Q->array[Q->head++%Q->capacity];

	
}
int dequeue(int* queue){
    if(!isEmptyQueue()){
        front = (front +1) % MAXLENGTH;
        return queue[front];
    }
    else{
        printf("There is no data in a queue");
        return (int)NULL;
    }
}
QueueElementType Front(Queue *Q)
{
  if (isEmptyQueue(Q))  
  {
    printf("\n**Error in Dequeue: Q is empty?!\n\n");
    exit(-1);
  }
  QueueElementType e = Q->Elements[Q->Front];
  return e;
}
Beispiel #23
0
void levelOrderIter (TNODE_p_t root) {
	if (root == NULL)
		return;
	QUEUE_p_t queue = initQueue(root);
	while (!isEmptyQueue(queue)) {
		TNODE_p_t temp = deleteQueue(&queue);
		printf(" %d", temp->data);
		if (temp->left != NULL)
			insertQueue(&queue, temp->left);
		if (temp->right != NULL)
			insertQueue(&queue, temp->right);
	}
}
Beispiel #24
0
void insert (CQUEUE_p_t queue, char * item) {
	if (isFullQueue(*queue)) {
		printf("\n\tQUEUE OVERFLOW!\n\n");
		return;
	}
	
	if (isEmptyQueue(*queue))
		queue->front = queue->rear = 0;
	else
		queue->rear = (queue->rear + 1)%SIZE;
	
	*(queue->arr + queue->rear) = item;
}
/*
Find the level that has the maximum sum in the binary tree

Time Complexity: O(n)
Space Complexity: O(n)
*/
int findLevelwithMaxSum(struct binaryTreeNode *root) {
    struct binaryTreeNode *temp;
    int level = 0, maxLevel = 0;
    struct Queue *q;
    int currentSum = 0, maxSum = 0;

    if (!root)
        return 0;

    q = createQueue();
    enQueue(q, root);
    enQueue(q, NULL);
    while (!isEmptyQueue(q)) {
        temp = deQueue(q);
        // If the current level is completed then compare sums
        if (temp == NULL) {
            if (currentSum > maxSum) {
                maxSum = currentSum;
                maxLevel = level;
            }

            currentSum = 0;
            // place the indicator for end of next level at the end of queue
            if (!isEmptyQueue(q))
                enQueue(q, NULL);
            level++;
        }
        else {
            currentSum += temp->data;
            if (temp->left)
                enQueue(q, temp->left);
            if (temp->right)
                enQueue(q, temp->right);
        }

    }

    return maxLevel;
}
QueueElementType Dequeue(Queue *Q)
{
  if (isEmptyQueue(Q))  
  {
    printf("\n**Error in Dequeue: Q is empty?!\n\n");
    exit(-1);
  }
  QueueElementType e = Q->Elements[Q->Front];
  Q->Front++;
  if (Q->Front >= Q->Capacity)  
    Q->Front = 0;
  Q->NumElements--;
  return e;
}
Graph shortestPath(Graph g, Vertex v)
{
	Graph mst = newGraph(g->nV); // create a new mst graph
	Queue q = newQueue(); // create a new queue
	int *visitedVertices = malloc(sizeof(Vertex) * g->nV);
	int *dist = malloc(sizeof(int) * g->nV); // create the distance array
	int *pred = malloc(sizeof(int) * g->nV); // create the predecessor array, stores vertices passed through
	int total = 0, i = 0;
	Vertex curV = 0, w = 0;

	assert(visitedVertices != NULL && dist != NULL && pred != NULL);
	
	// clear all the memory blocks
	setArray(visitedVertices, UNVISITED, g->nV);
	setArray(dist, INF, g->nV);
	setArray(pred, -1, g->nV);

	visitedVertices[v] = VISITED; // mark the starting vertex as visited
	dist[v] = 0;
	
	enQueue(q, v); // add the starting vertex to the queue

	while ( !isEmptyQueue(q) ){
		curV = deQueue(q); // remvoe first element from queue
		for (w = 0; w < getnV(g); w++){
			if (g->wt[curV][w] == NO_WEIGHT) continue;
			if (dist[curV] + g->wt[curV][w] < dist[w]){ // edge relaxation
				dist[w] = dist[curV] + g->wt[curV][w];
				pred[w] = curV;
				enQueue(q,w);
			}
		}
	}
	

	// add the appropriate edges
	for (i = 0; i < g->nV; i++){
		if (pred[i] != NOT_ASSIGNED){
			addEdge(mst, pred[i], i);
			total += dist[i];
		}
	}

	deleteQueue(q);
	free(dist);
	free(pred);
	printf("Total = %d.\n", total);

	return mst;
}
Beispiel #28
0
int fromQueue() {
  assert(initialized);
  if (isEmptyQueue()) {
    printf("UNDERFLOW\n");
    exit(-1);
  } else {
    queueLength--;
    Pointer p = front;
    front = memory[p].data.next;
    int value = memory[p].data.value;
    toAVAIL(p);
    return value;
  }
}
Beispiel #29
0
void linkedQueueType<Type>::deleteQueue() {
    nodeType<Type> *temp;
    if (!isEmptyQueue()){
        temp = queueFront;
        queueFront = queueFront->link;
        delete temp;
        
        if (queueFront == NULL){
            queueRear = NULL;
        }
    }
    else {
        std::cout << "Cannot remove from an empty queue" << std::endl;
    }
}
Beispiel #30
0
void insertRight (DQUEUE_p_t queue, char * item) {
	if (queue->rear == SIZE - 1) {
		printf("\n\tQUEUE RIGHT OVERFLOW\n\n");
		return;
	}
	
	if (isEmptyQueue(*queue))
		queue->front = queue->rear = 0;
	else
		queue->rear += 1;
	
	queue->capacity += 1;
	
	*(queue->arr + queue->rear) = item;
}