Queue "Array-Based Implementation" In C++
im going to show you the implementation of of "Queue" "First In First Out" In Array-Based Implementation in C++ Language
You will find the code splitted to three parts at the first part queue.h we are going to define function's header and at the second part queue.cpp we are going to implement this function and in the last part main.cpp we are going call this function
Implementation Of Queue Array-Based
Queue.h
1 #define MAXQUEUE 5 2 typedef int INFO; 3 4 typedef struct Queue 5 { 6 int front; 7 int rear; 8 int size; 9 INFO Entry[MAXQUEUE]; 10 }QUEUE; 11 12 13 14 void CreateQueue(QUEUE *); 15 int QueueEmpty(QUEUE *); 16 int QueueFull(QUEUE *); 17 void Append(QUEUE *,INFO *); 18 void Serve(QUEUE *,INFO *); 19 int QueueSize(QUEUE *); 20 void TraverseQueue(QUEUE *,void(*pf) (INFO)); 21 void CopyQueue(Queue *,Queue *);
Queue.cpp
1 #include "Queue.h" 2 #include <stdio.h> 3 #include <stddef.h> 4 5 void CreateQueue(QUEUE *pq) 6 { 7 pq->front=0; 8 pq->rear=-1; 9 pq->size=0; 10 } 11 12 int QueueEmpty(QUEUE *pq) 13 { 14 return (pq->size==0); 15 } 16 17 int QueueFull(QUEUE *pq) 18 { 19 return (pq->size==MAXQUEUE); 20 } 21 22 void Append(QUEUE *pq,INFO *item) 23 { 24 pq->rear=(pq->rear+1)%MAXQUEUE; 25 pq->Entry[pq->rear]=*item; 26 pq->size++; 27 } 28 29 void Serve(QUEUE *pq,INFO *item) 30 { 31 *item=pq->Entry[pq->front]; 32 pq->front=(pq->front+1)%MAXQUEUE; 33 pq->size--; 34 } 35 36 int QueueSize(QUEUE *pq) 37 { 38 return pq->size; 39 } 40 41 void TraverseQueue(QUEUE *pq,void(*pf) (INFO)) 42 { 43 int i,pos; 44 for(pos=pq->front,i=0;i<pq->size;i++) 45 { 46 (*pf)(pq->Entry[pos]); 47 if(pos==MAXQUEUE-1) 48 { 49 pos=(pos+1)%MAXQUEUE; 50 } 51 else 52 pos++; 53 } 54 } 55 56 void CopyQueue(Queue *pq,Queue *pq1) 57 { 58 int i,pos; 59 for(pos=pq->front,i=0;i<pq->size;i++) 60 { 61 pq1->Entry[pos]=pq->Entry[pos]; 62 63 if(pos==MAXQUEUE-1) 64 pos=0; 65 else 66 pos++; 67 } 68 pq1->front=pq->front; 69 pq1->rear=pq->rear; 70 pq1->size=pq->size; 71 }
Main.cpp
1 #include "Queue.h" 2 #include <stdio.h> 3 #include <stddef.h> 4 5 void Display(INFO e) 6 { 7 printf("%d\n",e); 8 } 9 10 11 int main() 12 { 13 int ch; 14 INFO e; 15 QUEUE q,q1; 16 CreateQueue(&q); 17 CreateQueue(&q1); 18 printf("\n1 To Append In Queue\n2 To Serve From Queue\n3 To Get Size Of Queue\n4 To Traverse Queue\n5 To Copy Queue To Another\n6 To Print Copied Queue\n "); 19 scanf("%d",&ch); 20 21 while(ch!=7) 22 { 23 if(ch==1) 24 { 25 printf("\nEnter Data To Append It In Queue\n"); 26 scanf("%d",&e); 27 if(!QueueFull(&q)) 28 { 29 Append(&q,&e); 30 printf("Successfully Appended\n"); 31 } 32 else 33 printf("Queue Full !!... \n"); 34 } 35 else if(ch==2) 36 { 37 if(!QueueEmpty(&q)) 38 { 39 printf("\nItem Which Has Been Served\n"); 40 Serve(&q,&e); 41 printf("Values Of Items Is :%d\n",e); 42 } 43 else 44 printf("Queue Empty !!...\n"); 45 } 46 else if(ch==3) 47 { 48 printf("Size Of Queue is %d\n",QueueSize(&q)); 49 } 50 51 else if(ch==4) 52 { 53 printf("Items Which Has Been Traversed\n"); 54 TraverseQueue(&q,&Display); 55 printf("Traversed Successfully\n"); 56 } 57 58 else if(ch==5) 59 { 60 CopyQueue(&q,&q1); 61 } 62 63 else if(ch==6) 64 { 65 if(!QueueEmpty(&q1)) 66 { 67 printf("\nItem Which Has Been Served\n"); 68 Serve(&q1,&e); 69 printf("Values Of Items Is :%d\n",e); 70 } 71 else 72 printf("Queue Empty !!...\n"); 73 } 74 printf("\n1 To Append In Queue\n2 To Serve From Queue\n3 To Get Size Of Queue\n4 To Traverse Queue\n5 To Copy Queue To Another\n6 To Print Copied Queue\n "); 75 scanf("%d",&ch); 76 77 } 78 79 return 0; 80 } 81
You Can Download It From Here : http://goo.gl/K4a3dh

Leave a Comment