Queue "Linked List Implementation" In C++



im going to show you the implementation of of "Queue" "First In First Out" In Linked List 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 Linked List

Queue.h

  1 typedef int INFO;
  2  
  3 typedef struct node
  4 { 
  5     INFO data; 
  6     struct node *next; 
  7 }Node; 
  8  
  9 typedef struct listqueue
 10 { 
 11     Node *front; 
 12     Node *rear; 
 13     int size; 
 14 }ListQueue; 
 15  
 16 void CreateQueue(ListQueue *); 
 17 int QueueEmpty(ListQueue *); 
 18 int QueueFull(ListQueue *); 
 19 void Append(ListQueue *,INFO *); 
 20 void Serve(ListQueue *,INFO *); 
 21 int QueueSize(ListQueue *); 
 22 void TraverseQueue(ListQueue *,void(*pf) (INFO)); 
 23 void CopyQueue(ListQueue *,ListQueue *); 
 24 

Queue.Cpp

  1 #include "QUEUELIST.h" 
  2 #include <stdlib.h> 
  3 #include <stddef.h> 
  4  
  5 void CreateQueue(ListQueue *pq) 
  6 { 
  7     pq->front=NULL; 
  8     pq->rear=NULL; 
  9     pq->size=0; 
 10 } 
 11  
 12 int QueueFull(ListQueue *pq) 
 13 { 
 14     return 0;
 15 } 
 16  
 17 int QueueEmpty(ListQueue *pq) 
 18 { 
 19      return (pq->front==NULL); 
 20 } 
 21  
 22 void Append(ListQueue *pq,INFO *item) 
 23 { 
 24     Node *p=(Node *)malloc(sizeof(Node)); 
 25     p->data=*item; 
 26     p->next=NULL; 
 27     if(!pq->rear) 
 28         pq->front=p; 
 29     else 
 30     pq->rear->next=p; 
 31     pq->rear=p; 
 32     pq->size++; 
 33 } 
 34  
 35 void Serve(ListQueue *pq,INFO *item) 
 36 { 
 37     Node *q; 
 38     q=pq->front; 
 39     *item=q->data; 
 40     pq->front=pq->front->next; 
 41     free(q); 
 42     if(!pq->front) 
 43     pq->rear=NULL; 
 44     pq->size--; 
 45 } 
 46  
 47 int QueueSize(ListQueue *pq) 
 48 { 
 49     return pq->size; 
 50 } 
 51  
 52 void TraverseQueue(ListQueue *pq,void(*pf) (INFO)) 
 53 { 
 54         Node *q=pq->front; 
 55         while(q) 
 56         { 
 57             (*pf)(q->data); 
 58             q=q->next; 
 59         } 
 60 } 
 61  
 62 void CopyQueue(ListQueue *pq1,ListQueue *pq2)
 63 { 
 64     Node *q=pq1->front; 
 65     Node *pt=NULL; 
 66     while(q) 
 67     { 
 68         Node *Nq=(Node *) malloc(sizeof(Node)); 
 69         Nq->data=q->data; 
 70         Nq->next=NULL; 
 71         if(!pq2->rear) 
 72             pq2->front=Nq; 
 73         else 
 74         pq2->rear->next=Nq; 
 75         pq2->rear=Nq; 
 76         pq2->size++; 
 77         q=q->next; 
 78     } 
 79 }

Main.Cpp


  1 #include "QUEUELIST.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     ListQueue l1,l2; 
 16     CreateQueue(&l1); 
 17     CreateQueue(&l2); 
 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  
 24         if(ch==1) 
 25         { 
 26             printf("\nEnter Data To Append It In Queue\n"); 
 27             scanf("%d",&e); 
 28             if(!QueueFull(&l1)) 
 29             { 
 30                 Append(&l1,&e); 
 31                 printf("Successfully Appended\n"); 
 32             } 
 33             else 
 34                 printf("Queue Full !!... \n"); 
 35         } 
 36  
 37         else if(ch==2) 
 38         { 
 39             if(!QueueEmpty(&l1)) 
 40             { 
 41             printf("\nItem Which Has Been Served\n"); 
 42             Serve(&l1,&e); 
 43             printf("Values Of Items Is :%d\n",e); 
 44             } 
 45             else 
 46                 printf("Queue Empty !!...\n"); 
 47         } 
 48  
 49         else if(ch==3) 
 50         { 
 51             printf("Size Of Queue is %d\n",QueueSize(&l1)); 
 52         } 
 53  
 54         else if(ch==4) 
 55         { 
 56             printf("Items Which Has Been Traversed\n"); 
 57             TraverseQueue(&l1,&Display); 
 58             printf("Traversed Successfully\n"); 
 59         } 
 60  
 61         else if(ch==5) 
 62         { 
 63             CopyQueue(&l1,&l2); 
 64         } 
 65  
 66         else if(ch==6) 
 67         { 
 68             if(!QueueEmpty(&l2)) 
 69             { 
 70             printf("\nItem Which Has Been Served\n"); 
 71             Serve(&l2,&e); 
 72             printf("Values Of Items Is :%d\n",e); 
 73             } 
 74             else 
 75             printf("Queue Empty !!...\n"); 
 76         } 
 77  
 78     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 "); 
 79     scanf("%d",&ch); 
 80     } 
 81     return 0;
 82 }


You Can Download It From Here : http://goo.gl/dSwUpz

No comments

Powered by Blogger.