FIFO.h (interface)
1#include"Item.h"2#include <stdlib.h>3 4typedefstructStacknode *link;5 structStacknode6 {7 item Item;8 link next;9 };Ten One voidQueueinit (int); A intQueueempty (void); - voidQueueput (Item); -Item Queueget (void); the voidQueueerror (void);
Item.h (custom type)
1 typedef char Item;
FIFO.C (Interface Implementation)
1 StaticItem *Q;2 Static intN, head, tail;3 4 voidQueueinit (intMAXN)5 {6q=malloc((maxn+1)*sizeof(Item));7n=maxn+1;8Head=N;9Tail=0; Ten } One intQueueempty (void) A { - returnhead%n==tail; - } the voidqueueput (item item) - { - if(tail==head-1) - queueerror (); +q[tail++]=item; -tail=tail%N; + } AItem Queueget (void) at { - if(Queueempty ()) - queueerror (); -head=head%N; - returnq[head++]; - } in voidQueueerror (void) - { toprintf"queue is empty or full"); +Exit1); -}
MAIN.C (main program)
1#include <stdio.h>2#include"FIFO.h"3 4 intMainvoid)5 {6 intN;7printf"input string Size:");8 if(SCANF ("%d", &N))9 Queueinit (N);Ten GetChar (); Oneprintf"input string \ n"); A while((N=getchar ())! =EOF) - { - if(n=='*') the { - Putchar (Queueget ()); -Putchar ('\ n'); - } + Else - Queueput (N); + } A at return 0; -}
Implementation of the FIFO queue ADT interface Array