Skip to content

Instantly share code, notes, and snippets.

@rafaeltoledo
Last active October 12, 2019 00:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rafaeltoledo/ab4b7d3623904589ed61e9095c1fc2c2 to your computer and use it in GitHub Desktop.
Save rafaeltoledo/ab4b7d3623904589ed61e9095c1fc2c2 to your computer and use it in GitHub Desktop.
Filas
// Em C
struct dado
{
char conteudo[20];
struct dado *link;
};
struct dado *enqueue(struct dado *queue, const char *novoItem)
{
struct dado *novaEstrutura = (struct dado*) malloc(sizeof(struct dado)), *ponteiroAuxiliar;
strcpy(novaEstrutura->conteudo, novoItem);
novaEstrutura->link = NULL;
if (queue == NULL) return novaEstrutura;
for(ponteiroAuxiliar = queue; ponteiroAuxiliar->next != NULL; ponteiroAuxiliar = ponteiroAuxiliar->next);
ponteiroAuxiliar->link = novaEstrutura;
return queue;
}
struct dado *dequeue(struct dado *queue, char *item)
{
// Fila vazia - não há o que remover
if (queue == NULL)
{
item = NULL;
return NULL;
}
// Somente um item na fila
if (queue->link == NULL)
{
strcpy(item, queue->conteudo);
free(queue); // desaloca da memória
return NULL;
}
struct dado *ponteiroAuxiliar;
strcpy(item, queue->conteudo);
ponteiroAuxiliar = queue;
queue = queue->link;
free(queue);
return queue;
}
// Em C++
struct dado
{
char conteudo[20]; // Pode ser qualquer tipo... char[20] é um exemplo
struct dado *link; // para o próximo item.
};
struct dado *enqueue(struct dado *queue, const char *novoItem)
{
struct dado *novaEstrutura = new struct message, *ponteiroAuxiliar;
strcpy(novaEstrutura->conteudo, novoItem);
novaEstrutura->link = NULL;
if (queue == NULL) return novaEstrutura;
for(ponteiroAuxiliar = queue; ponteiroAuxiliar->next != NULL; ponteiroAuxiliar = ponteiroAuxiliar->next);
ponteiroAuxiliar->link = novaEstrutura;
return queue;
}
struct dado *dequeue(struct dado *queue, char *item)
{
// Fila vazia - não há o que remover
if (queue == NULL)
{
item = NULL;
return NULL;
}
// Somente um item na fila
if (queue->link == NULL)
{
strcpy(item, queue->conteudo);
delete queue; // desaloca da memória
return NULL;
}
struct dado *ponteiroAuxiliar;
strcpy(item, queue->conteudo);
ponteiroAuxiliar = queue;
queue = queue->link;
delete queue;
return queue;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment