Asked in
Database Programming
C Programming

C program for insertion and deletion in queue?

Answer

User Avatar
Wiki User
April 20, 2014 9:01AM

Linear Queue C Program using structure

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

#define MAX 50

typedef enum{true,false} bool;

typedef struct{

int front,rear;

int elements[MAX];

}queue;

bool isfull(queue *);

bool isempty(queue *);

void create(queue *);

void enqueue(queue *,int);

void dequeue(queue *);

void peek(queue *);

void trav(queue *);

void main()

{

int choice,element;

queue *pq, q;

//printf("hello22");

create(&q);//printf("hello11");

clrscr();

while(1){

printf("\n1-enqueue\n2-dequeue\n3-peek\n4-traverse\n5-exit\n");

scanf("%d",&choice);

switch(choice)

{

case 1: clrscr();

if(isfull(&q)) { printf("queue full");

getch();

}

else{ printf("enter the value");

scanf("%d",&element);

enqueue(&q,element);

}

break;

case 2: clrscr();

if(isempty(&q)) { printf("queue empty\nenter any key to continue");

getch();

}

else { dequeue(&q);

printf("\npress any key to continue");

getch();

}

break;

case 3:clrscr();

if(isempty(&q)) { printf("stack empty\nenter any key to continue");

getch();

}

else { peek(&q);

printf("\npress any key to continue");

getch();

}

break;

case 4:trav(&q);

printf("\npress any key to continue");

getch();

break;

case 5:exit(1);

}

}

getch();

}

void create(queue *pq)

{

pq->front=pq->rear=-1;

}

bool isfull(queue *pq)

{

if((pq->front==0)&&(pq->rear==MAX-1)) {//printf("heewuey%d",ps->top);

return 1; }

else {//printf("wreryteud%d",ps->top);

return 0;}

}

bool isempty(queue *pq)

{

if(pq->front==-1) return 1;

else return 0;

}

void enqueue(queue *pq,int element)

{int i;

if(pq->front==-1) pq->front=pq->rear=0;

else if (pq->rear==MAX-1)

{

for(i=pq->front;i<=pq->rear;i++)

pq->elements[i-pq->front]=pq->elements[i];

pq->rear=pq->rear+pq->front-1;

pq->front=0;

}

else pq->rear++;

pq->elements[pq->rear]=element;

}

void dequeue(queue *pq)

{

int temp;

temp=pq->elements[pq->front];

if(pq->front==pq->rear) pq->front=pq->rear=-1;

else pq->front++;

printf("deleted val is %d",temp);

}

void peek(queue *pq)

{

printf("front element is %d",pq->elements[pq->front]);

}

void trav(queue *pq)

{

int i;

for(i=pq->front;i<=pq->rear;i++)

printf("%d\t",pq->elements[i]);

}

Circular Queue C program

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

#define MAX 50

typedef enum{true,false} bool;

typedef struct{

int front,rear;

int elements[MAX];

}queue;

bool isfull(queue *);

bool isempty(queue *);

void create(queue *);

void enqueue(queue *,int);

void dequeue(queue *);

void peek(queue *);

void trav(queue *);

void main()

{

int choice,element;

queue *pq, q;

//printf("hello22");

create(&q);

clrscr();

while(1){

printf("\n1-enqueue\n2-dequeue\n3-peek\n4-traverse\n5-exit\n");

scanf("%d",&choice);

switch(choice)

{

case 1: clrscr();

if(isfull(&q)) { printf("queue full");

getch();

}

else{ printf("enter the value");

scanf("%d",&element);

enqueue(&q,element);

}

break;

case 2: clrscr();

if(isempty(&q)) { printf("queue empty\nenter any key to continue");

getch();

}

else { dequeue(&q);

printf("\npress any key to continue");

getch();

}

break;

case 3:clrscr();

if(isempty(&q)) { printf("stack empty\nenter any key to continue");

getch();

}

else { peek(&q);

printf("\npress any key to continue");

getch();

}

break;

case 4:trav(&q);

printf("\npress any key to continue");

getch();

break;

case 5:exit(1);

}

}

getch();

}

void create(queue *pq)

{

pq->front=pq->rear=-1;

}

bool isfull(queue *pq)

{

if((pq->front==0)&&(pq->rear==MAX-1)) {

return 1; }

else {

return 0;}

}

bool isempty(queue *pq)

{

if(pq->front==-1) return 1;

else return 0;

}

void enqueue(queue *pq,int element)

{int i;

if(pq->front==-1) pq->front=pq->rear=0;

else if (pq->rear==MAX-1) pq->rear=0;

else pq->rear++;

pq->elements[pq->rear]=element;

}

void dequeue(queue *pq)

{

int temp;

temp=pq->elements[pq->front];

if(pq->front==pq->rear) pq->front=pq->rear=-1;

else if(pq->front==MAX-1) pq->front=0;

else pq->front++;

printf("deleted val is %d",temp);

}

void peek(queue *pq)

{

printf("front element is %d",pq->elements[pq->front]);

}

void trav(queue *pq)

{

int i;

for(i=pq->front;i<=pq->rear;i++)

printf("%d\t",pq->elements[i]);

}