The need:
To show how circular queue can be implemented on integer array with user defined functions (UDFs). This is general implementation of queues. This lets you handle more than one queue by using same functions for all queues. For this some changes are made UDFs.
The code:
-----------------------------------------------------------
-----------------------------------------------------------
To show how circular queue can be implemented on integer array with user defined functions (UDFs). This is general implementation of queues. This lets you handle more than one queue by using same functions for all queues. For this some changes are made UDFs.
The code:
-----------------------------------------------------------
/* This is a program showing the circular queue implimentation on array. This program shows how we can implement queue on local variables. In this first I have defined a structure containinf an array and front and rear so that I can have front and rear for each local queue. You can have multiple queues in this program all you need to do is declare another variable of type node. In this convention front indexeses to the first item and rear indexes the location after last item this convention lets you fill maximum (SIZE-1) items in a queue where SIZE is size of the array being used program writtem by RP Singh compiled and tested on C-free4.0 standard */ #define SIZE 5 #include<stdio.h> #include<stdlib.h> #include<string.h> struct node1 { int array[SIZE]; int front,rear; }; typedef struct node1 queue; void enqueue(int, queue *); int dequeue(queue *); int queue_length(queue *); void view_queue(queue *); int main() { int i=0,j,current_item; char c; queue que; //local implementation of queue que.front=0; que.rear=0; printf("This is a demo program to show working of queues"); anchor: //anchor is a label printf("\n\nyou have following options\n"); printf("1. enqueue an item\n2. dequeue an item\n3. view queue\n"); printf("4. count items in queue\n5. exit program\n\n"); scanf("%d",&j); switch(j) { case 1: printf("\nEnter a number to be enqueued =>\t"); scanf("%d",¤t_item); enqueue(current_item, &que); goto anchor; case 2: current_item = dequeue(&que); goto anchor; case 3: view_queue(&que); goto anchor; case 4: printf("total items in the queue are %d\n",queue_length(&que)); goto anchor; case 5: printf("Thank you\n"); exit(0); goto anchor; default: printf("Invalid choice...!!!\n try choosing again\n\n"); goto anchor; } return 0; } void enqueue(int a, queue *p) //definition of enqueue function { if((p->rear+1)%SIZE==p->front) printf("cannot enqueue because the queue is already full.\n"); else { p->array[p->rear]=a; //enqueing an item p->rear=(p->rear+1)%SIZE; printf("item %d inserted into queue successfully\n\n",a); } } int dequeue(queue *p) //definition of dequeue function { int temp; if(p->rear==p->front) //this is the case when queue is empty { printf("queue is empty hence can not be dequeued\n"); } else { temp=p->array[p->front]; p->front=(p->front+1)%SIZE; printf("item %d dequeued successfully\n",temp); } return temp; } int queue_length(queue *p) { int length=0; if(p->rear>=p->front) length=p->rear-p->front; else length=(p->rear+SIZE)-p->front; return length; } void view_queue(queue *p) { int temp_front; temp_front=p->front; if(queue_length(p)==0) { printf("\nThe queue is empty...!!!\n"); return; } printf("The queue is\n"); while(temp_front!=p->rear) { printf("%d -> ",p->array[temp_front]); temp_front=(temp_front+1)%SIZE; } printf("\n"); return; }
-----------------------------------------------------------
Remarks:
This is a program showing implementation of circular queue on array. For simplicity I have taken a simple integer array. This program may look different from previous ones but this is also simply follows the concept of queue.
No comments:
Post a Comment
feel free to ask your doubts... if any
you can post your doubts on
www.facebook.com/programsimply