Tipos de Dados Abstractos: Fila: Difference between revisions

From Wiki**3

No edit summary
 
No edit summary
 
(One intermediate revision by the same user not shown)
Line 4: Line 4:
     #define __QUEUE_H__
     #define __QUEUE_H__
    
    
     #include “Item.h”
     #include "Item.h"
    
    
     void QUEUEinit(int);
     void '''QUEUEinit'''(int);
     int  QUEUEempty();
     int  '''QUEUEempty'''();
     void QUEUEput(Item);
     void '''QUEUEput'''(Item);
     Item QUEUEget();
     Item '''QUEUEget'''();
    
    
     #endif
     #endif
Line 24: Line 24:
   static int N, head, tail;
   static int N, head, tail;
    
    
   void QUEUEinit(int maxN) {
   void '''QUEUEinit'''(int maxN) {
     q = malloc((maxN+1)*sizeof(Item));  
     q = malloc((maxN+1)*sizeof(Item));  
     N = maxN+1; head = N; tail = 0;
     N = maxN+1; head = N; tail = 0;
   }
   }
    
    
   int  QUEUEempty()        { return head % N == tail;          }
   int  '''QUEUEempty'''()        { return head % N == tail;          }
   void QUEUEput(Item item) { q[tail++] = item; tail = tail % N; }
   void '''QUEUEput'''(Item item) { q[tail++] = item; tail = tail % N; }
   Item QUEUEget()          { head = head % N; return q[head++]; }
   Item '''QUEUEget'''()          { head = head % N; return q[head++]; }


=== Implementação com lista ===
=== Implementação com lista ===
Line 49: Line 49:
   }                                   
   }                                   
    
    
   void QUEUEinit(int maxN) { head = NULL; }
   void '''QUEUEinit'''(int maxN) { head = NULL; }
   int  QUEUEempty()        { return head == NULL; }
   int  '''QUEUEempty'''()        { return head == NULL; }
   void QUEUEput(Item item) {  
   void '''QUEUEput'''(Item item) {  
     if (head == NULL) { head = tail = NEW(item, head); return; }
     if (head == NULL) { head = tail = NEW(item, head); return; }
     tail->next = NEW(item, tail->next); tail = tail->next;
     tail->next = NEW(item, tail->next);
    tail = tail->next;
   }
   }
    
    
   Item QUEUEget() {
   Item '''QUEUEget'''() {
     Item item = head->item;
     Item item = head->item;
     link t = head->next;  
     link t = head->next;  

Latest revision as of 06:52, 19 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;
 }

Cliente