Tipos de Dados Abstractos: Fila: Difference between revisions
From Wiki**3
No edit summary |
|||
Line 4: | Line 4: | ||
#define __QUEUE_H__ | #define __QUEUE_H__ | ||
#include | #include "Item.h" | ||
void QUEUEinit(int); | void QUEUEinit(int); |
Revision as of 08:48, 18 May 2005
Interface
#ifndef __QUEUE_H__ #define __QUEUE_H__ #include "Item.h" void QUEUEinit(int); int QUEUEempty(); void QUEUEput(Item); Item QUEUEget(); #endif
Implementação
Implementação com vector
#include <stdlib.h> #include "Item.h" #include "QUEUE.h" static Item *q; static int N, head, tail; void QUEUEinit(int maxN) { q = malloc((maxN+1)*sizeof(Item)); N = maxN+1; head = N; tail = 0; } int QUEUEempty() { return head % N == tail; } void QUEUEput(Item item) { q[tail++] = item; tail = tail % N; } Item QUEUEget() { head = head % N; return q[head++]; }
Implementação com lista
#include <stdlib.h> #include "Item.h" #include "QUEUE.h" typedef struct QUEUEnode *link; struct QUEUEnode { Item item; link next; }; static link head, tail; link NEW(Item item, link next) { link x = malloc(sizeof *x); x->item = item; x->next = next; return x; } void QUEUEinit(int maxN) { head = NULL; } int QUEUEempty() { return head == NULL; } void QUEUEput(Item item) { if (head == NULL) { head = tail = NEW(item, head); return; } tail->next = NEW(item, tail->next); tail = tail->next; } Item QUEUEget() { Item item = head->item; link t = head->next; free(head); head = t; return item; }