Deque ADT Interface DEQUEUE.h:
1#include <stdlib.h>2#include"Item.h"3 4 voidDequeueinit (int);5 voidDequeueerror (void);6Item Dequeueheadget (void);7Item Dequeuetailget (void);8 voidDequeueheadput (Item);9 voidDequeuetailput (Item);Ten intDequeueisempty (void); One intDequeueisfull (void);
The Deque ADT interface implements DEQUEUE.C:
1#include"DEQUEUE.h"2 3 StaticItem *Q;4 Static intN,head,tail;5 6 voidDequeueinit (intMAXN)7 {8q=malloc(maxn*sizeof(Item));9 if(q==NULL)Ten dequeueerror (); OneHead=0; ATail=0; -n=MAXN; - } the voidDequeueerror (void) - { -printf"\ n The space may be full or empty"); -Exit1); + } -Item Dequeueheadget (void) + { A if(Dequeueisempty ()) at dequeueerror (); -Item temp=Q[head]; -Head= (head+1)%N; - returntemp; - } -Item Dequeuetailget (void) in { - if(Dequeueisempty ()) to dequeueerror (); +Tail= (tail-1+n)%N; - returnQ[tail]; the } * voiddequeueheadput (Item ch) $ {Panax Notoginseng if(Dequeueisfull ()) - dequeueerror (); theHead= (head-1+n)%N; +q[head]=ch; A } the voiddequeuetailput (Item ch) + { - if(Dequeueisfull ()) $ dequeueerror (); $q[tail]=ch; -Tail= (tail+1)%N; - } the intDequeueisempty (void) - {Wuyi if(head==tail) the return 1; - return 0; Wu } - intDequeueisfull (void) About { $ if(((tail+1)%N) = =head) - return 1; - return 0; -}
Item.h:
1 Char Item;
Main program MAIN.C:
1#include <stdio.h>2 3 intMainvoid)4 {5 intN;6 7printf"input needs to request memory size:");8 if(SCANF ("%d", &N))9 Dequeueinit (N);Ten Else One dequeueerror (); A GetChar (); -printf"Enter%d characters", N); -printf"(' + ' from Team head get ' * ' from team tail get ' capital letters '" the "Put ' small letter ' from team end put ' from Team Head ': \ n"); - - while((N=getchar ())! ='\ n') - { + Switch(N) - { + Case '+': A Putchar (Dequeueheadget ()); at Break; - Case '*': - Putchar (Dequeuetailget ()); - Break; - default: - if(n> -&&N< the) in Dequeueheadput (N); - Else to Dequeuetailput (N); + } - } the * return 0; $}
Implementation of a double-ended queue ADT Interface array