Queue:
A queue is a
linear list of element in which
è Deletion can take
place at one end that is Front .
è Insertion take place at one end that is Rear ..
That's why Also known as FILO :-first in last out
That's why Also known as FILO :-first in last out
Property of Queue:
è Front == NULL (or -1) =>> shows Empty
queue
è Since deletion take place at front so after
deletion front=front+1
è Similarly on insertion rear=rear+1
è If rear =N then on insertion rear=1
èIf front=N then on deletion front =1
è Front=0 and rear =N shows queue is full and no
further insertion possible
è If front ==rear !=NULL then queue contain only
one element
Algorithm to
Insertion in Queue :
Insertion in queue always take place at rear
1:- if front==0 and
rear=N or front=rear+1 then
Print
overflow and exit .
2:- if Front==NULL
set front=0 and rear=0
else if rear==N
set rear=0
else
rear=rear+1
3:- set queue[rear]=item
4:- exit
Algorithm to Deletion from Queue :
Deletion from queue always take place at front .
1:- if front==NULL
then print underflow condition and exit
2:- set item=queue[front]
3:- if front==rear //i.e only one element in queue
set front=NULL and rear=NULL
else if front=N (N=max index of array )
set front=N
else
front=front+1
4:- Exit
Queue implementation in C array :
- //Queue Implemented in array
- // C-Program
- #include<stdio.h>
- #include<malloc.h>
- #include<stdlib.h>
- void insert(int *ar,int *front,int *rear,int N){
- int n;
- printf("Enter number to insert :");
- scanf("%d",&n);
- if((*front==0 && *rear== N) || *front==*rear+1){
- printf("overflow condition -> queue is full");
- }
- else if(*front==-1){
- *front=0;
- *rear=0;
- *(ar+(*rear))=n;
- }
- else if(*rear==N && *front !=0){
- *rear=0;
- *(ar+(*rear))=n;
- }
- else{
- *rear=*rear+1;
- *(ar+(*rear))=n;
- }
- }
- int del(int *ar,int *front,int *rear,int N){
- int x;
- if(*front == -1 ){ //queue is empty
- printf("\nUnderflow cond! -> No element in queue to delete");
- }
- else if(*front==*rear){ //only one element in queue
- x=*(ar+(*front));
- *front=-1;
- *rear=-1;
- }
- else if(*front ==N){
- x=*(ar+(*front));
- *front=0;
- }
- else{
- x=*(ar+(*front));
- *front=*front+1;
- }
- return x;
- }
- void display(int *ar,int f,int r,int N){
- printf("\n");
- if(f<=r){
- while(f<=r){
- printf(" %d ",*(ar+f));
- f=f+1;
- }
- }
- else{
- while(f<=N){
- printf(" %d ",*(ar+f));
- f=f+1;
- }
- f=0;
- while(f<=r){
- printf(" %d ",*(ar+f));
- f=f+1;
- }
- }
- printf("\n");
- }
- main(){
- int maxsi,front=-1,rear=-1;
- int ch,x;
- int *ar;
- printf("Enter Max size of Queue : ");
- scanf("%d",&maxsi); // max number of element you wanna put in queue
- ar=(int*)malloc(sizeof(int)*maxsi); //create array of size maxsi
- while(1){
- printf(" 1 : Insert \n 2 : Delete \n 3 : Exit\n Enter your choice :");
- scanf("%d",&ch);
- switch(ch){
- case 1:
- insert(ar,&front,&rear,maxsi-1);
- display(ar,front,rear,maxsi-1);
- break;
- case 2:
- x=del(ar,&front,&rear,maxsi-1);
- printf("\n%d is deleted from queue",x);
- display(ar,front,rear,maxsi-1);
- break;
- case 3:
- exit(0);
- break;
- default:
- printf("Wrong choice try again ??");
- break;
- }
- }
- }
OutPUt :
No comments:
Post a Comment
THANKS FOR UR GREAT COMMENT