Queue
a queue is a particular kind of abstract data type or collection in which the entities in the collection are kept in order and the principal (or only) operations on the collection are the addition of entities to the rear terminal position and removal of entities from the front terminal position. This makes the queue a First-In-First-Out (FIFO) data structure. In a FIFO data structure, the first element added to the queue will be the first one to be removed.
Queue Implementation Using Single link list
#include<stdio.h>
#include<conio.h>
/* Queue Implementation using single link list
Develop By Girfa
do not use it on any other website
*/
typedef struct st_que
{
int data;
struct st_que *next;
}que;
/* Function Prototype */
que* create(int);
void enque(int,que **,que **);
void deque(que**,que**);
void print(que*);
void main()
{
que *front=NULL,*rear=NULL;
int n,s,opt;
do
{
clrscr();
printf("\n1. Print\n2. Enquee\n3. Dequee\n0. Exit \nEnter your choice>> ");
fflush(stdin);
scanf("%d",&opt);
switch(opt)
{
case 1:
print(front);
getch();
break;
case 2:
printf("Enter Number for add in Queue>> ");
scanf("%d",&n);
enque(n,&front,&rear);
break;
case 3:
deque(&front,&rear);
break;
case 0:
break;
default:
printf("\nInvalid Choice ");
getch();
}
}while(opt!=0);
}
que * create(int n)
{
que *nw;
nw=(que*) malloc(sizeof(que));
nw->data=n;
nw->next=NULL;
return(nw);
}
void enque(int n,que **front,que **rear)
{
que *nw=create(n);
if(*front==NULL)
{
*front=nw;
*rear=nw;
}
else
{
(*rear)->next=nw;
*rear=nw;
}
}
void deque(que **front,que **rear)
{
que *pt;
if(*front==NULL)
{
printf("\nQueue is empty");
getch();
}
else
{
pt=*front;
*front=(*front)->next;
free(pt);
}
}
void print(que *st)
{
que *pt;
for(pt=st;pt!=NULL;pt=pt->next)
printf("[ %d ]",pt->data);
}
Queue implementation using Array
#include<stdio.h>
#include<conio.h>
#define MAX 5
typedef struct st_tg
{
int data;
int front;
int rear;
int ar[MAX];
}sque;
void enque(int,sque *);
int deque(sque *);
void main()
{
sque q;
int n,opt;
do
{
clrscr();
printf("\n*********************\n1. Enque\n2. Dequeue\n3. Print\n0. Exit\n*********************\n\nEnter your choice>> ");
fflush(stdin);
scanf("%d",&opt);
switch(opt)
{
case 1:
printf("Enter number for enqueue>> ");
scanf("%d",&n);
enque(n,&q);
break;
case 2:
n=deque(&q);
if(n==-1)
printf("Queue is Blank");
else
printf("Value Enqueue is %d",n);
getch();
break;
case 0:
break;
default:
printf("Invalid Choice");
getch();
}
}while(opt!=0);
}
void enque(int n,sque *s)
{
if((s->front==0 && s->rear==MAX-1) || (s->front==s->rear+1))
printf("List is full");
else
{
if(s->front==-1 && s->rear==-1)
{
s->front=0;
s->rear=0;
}
else
{
if(s->rear==MAX-1)
s->rear=0;
else
s->rear++;
s->ar[s->rear]=n;
}
}
}
int deque(sque *s)
{
int n;
if(s->front==-1 && s->rear==-1)
return(-1);
else
{
n=s->ar[s->front];
if(s->front==s->rear)
{
s->front=-1;
s->rear=-1;
}
else
{
if(s->front==MAX-1)
s->front=0;
else
s->front++;
}
return(n);
}
}
a queue is a particular kind of abstract data type or collection in which the entities in the collection are kept in order and the principal (or only) operations on the collection are the addition of entities to the rear terminal position and removal of entities from the front terminal position. This makes the queue a First-In-First-Out (FIFO) data structure. In a FIFO data structure, the first element added to the queue will be the first one to be removed.
Queue Implementation Using Single link list
#include<stdio.h>
#include<conio.h>
/* Queue Implementation using single link list
Develop By Girfa
do not use it on any other website
*/
typedef struct st_que
{
int data;
struct st_que *next;
}que;
/* Function Prototype */
que* create(int);
void enque(int,que **,que **);
void deque(que**,que**);
void print(que*);
void main()
{
que *front=NULL,*rear=NULL;
int n,s,opt;
do
{
clrscr();
printf("\n1. Print\n2. Enquee\n3. Dequee\n0. Exit \nEnter your choice>> ");
fflush(stdin);
scanf("%d",&opt);
switch(opt)
{
case 1:
print(front);
getch();
break;
case 2:
printf("Enter Number for add in Queue>> ");
scanf("%d",&n);
enque(n,&front,&rear);
break;
case 3:
deque(&front,&rear);
break;
case 0:
break;
default:
printf("\nInvalid Choice ");
getch();
}
}while(opt!=0);
}
que * create(int n)
{
que *nw;
nw=(que*) malloc(sizeof(que));
nw->data=n;
nw->next=NULL;
return(nw);
}
void enque(int n,que **front,que **rear)
{
que *nw=create(n);
if(*front==NULL)
{
*front=nw;
*rear=nw;
}
else
{
(*rear)->next=nw;
*rear=nw;
}
}
void deque(que **front,que **rear)
{
que *pt;
if(*front==NULL)
{
printf("\nQueue is empty");
getch();
}
else
{
pt=*front;
*front=(*front)->next;
free(pt);
}
}
void print(que *st)
{
que *pt;
for(pt=st;pt!=NULL;pt=pt->next)
printf("[ %d ]",pt->data);
}
Queue implementation using Array
#include<stdio.h>
#include<conio.h>
#define MAX 5
typedef struct st_tg
{
int data;
int front;
int rear;
int ar[MAX];
}sque;
void enque(int,sque *);
int deque(sque *);
void main()
{
sque q;
int n,opt;
do
{
clrscr();
printf("\n*********************\n1. Enque\n2. Dequeue\n3. Print\n0. Exit\n*********************\n\nEnter your choice>> ");
fflush(stdin);
scanf("%d",&opt);
switch(opt)
{
case 1:
printf("Enter number for enqueue>> ");
scanf("%d",&n);
enque(n,&q);
break;
case 2:
n=deque(&q);
if(n==-1)
printf("Queue is Blank");
else
printf("Value Enqueue is %d",n);
getch();
break;
case 0:
break;
default:
printf("Invalid Choice");
getch();
}
}while(opt!=0);
}
void enque(int n,sque *s)
{
if((s->front==0 && s->rear==MAX-1) || (s->front==s->rear+1))
printf("List is full");
else
{
if(s->front==-1 && s->rear==-1)
{
s->front=0;
s->rear=0;
}
else
{
if(s->rear==MAX-1)
s->rear=0;
else
s->rear++;
s->ar[s->rear]=n;
}
}
}
int deque(sque *s)
{
int n;
if(s->front==-1 && s->rear==-1)
return(-1);
else
{
n=s->ar[s->front];
if(s->front==s->rear)
{
s->front=-1;
s->rear=-1;
}
else
{
if(s->front==MAX-1)
s->front=0;
else
s->front++;
}
return(n);
}
}
No comments:
Post a Comment