1. Dqueue using array
#include
#include
int a[100],i,front,rear,size;
void insertfrnt(int e)
{
if(front==(rear+1)%size)
printf("\nQueue is full\n");
else
{
if(front==-1)
{
front=0;
a[front]=e;
}
front=(front+size-1)%size;
a[front]=e;
}
}
void insertrear(int e)
{
if(front==(rear+1)%size)
printf("\nQueue is full\n");
else
{
if(front==-1)
{
front=0;
}
rear=(rear+1)%size;
a[rear]=e;
}
}
void delfrnt()
{
if((front==-1)&&(rear==-1))
printf("\nQueue is empty.");
else
{
if(front==rear)
{
front=-1;
rear=-1;
}
else
front=(front+1)%size;
}
}
void delrear()
{
if((front==-1)&&(rear==-1))
printf("\nQueue is empty.");
else
{
if(front==rear)
{
front=-1;
rear=-1;
}
else
rear=(rear+size-1)%size;
}
}
void traverse()
{
if((front==-1)&&(rear==-1))
printf("\nQueue is empty\n");
else
{
for(i=front;i!=rear;i=(i+1)%size)
printf("%d \n",a[i]);
printf("%d \n",a[i]);
}
}
main()
{
int ch1,ch2,e;
printf("Enter size of queue:");
scanf("%d",&size);
front=-1;
rear=-1;
do
{
printf("\n1.Input Restricted\n2.Output Restricted\n3.Exit\nEnter your choice of dqueue:");
scanf("%d",&ch1);
switch(ch1)
{
case 1:
do{
printf("\n1.Insert rear\n2.Delete rear\n3.Delete front\n4.Traverse\n5.Exit\nEnter your choice:");
scanf("%d",&ch2);
switch(ch2)
{
case 1:
printf("\nEnter element to be inserted\n");
scanf("%d",&e);
insertrear(e);
break;
case 2: delrear();
break;
case 3: delfrnt();
break;
case 4: traverse();
break;
case 5:
break;
}
}while(ch2!=5);
break;
case 2:
do{
printf("\n1.Insert front\n2.Insert rear\n3.Delete front\n4.Traverse\n5.Exit\nEnter your choice:");
scanf("%d",&ch2);
switch(ch2)
{
case 1: printf("\nEnter element to be inserted:");
scanf("%d",&e);
insertfrnt(e);
break;
case 2: printf("\nEnter element to be inserted:");
scanf("%d",&e);
insertrear(e);
break;
case 3: delfrnt();
break;
case 4: traverse();
break;
case 5:
break;
}
}while(ch2!=5);
break;
case 3:
break;
}
}while(ch1!=3);
getch();
}
2.. Multiple Queue using array
#include
#include
int arr[100];
int front1,rear1,front2,rear2,maxsize;
int isEmpty1(){
if(front1==-1){
return 1;
}
else
return 0;
}
int isFull1(){
if(rear1==maxsize/2-1){
return 1;
}
else
return 0;
}
void insert1(int x){
if(isFull1()){
printf("queue1 is Full");
}
else{
rear1++;
if(front1==-1)
front1=0;
arr[rear1]=x;
}
}
void delete1(){
if(isEmpty1()){
printf("queue is empty");
}
else{
if(front1==rear1)
{
front1=-1;
rear1=-1;
}
else
{
front1=front1+1;
}
}
}
void traverse1(){
int i;
if(isEmpty1()){
printf("queue is empty");
}
else{
for(i=front1;i<=rear1;i++){
printf("%d",arr[i]);
}
}
}
int isEmpty2(){
if(front2==maxsize/2-1){
return 1;
}
else
return 0;
}
int isFull2(){
if(rear2==maxsize-1){
return 1;
}
else
return 0;
}
void insert2(int x){
if(isFull2()){
printf("queue2 is Full");
}
else{
rear2++;
if(front2==maxsize/2-1)
front2=maxsize/2;
arr[rear2]=x;
}
}
void delete2(){
if(isEmpty2()){
printf("queue is empty");
}
else{
if(front2==rear2)
{
front2=maxsize/2-1;
rear2=maxsize/2-1;
}
else
{
front2=front2+1;
}
}
}
void traverse2(){
int i;
if(isEmpty2()){
printf("queue is empty");
}
else{
for(i=front2;i<=rear2;i++){
printf("%d",arr[i]);
}
}
}
main()
{
int x,opt;
printf("enter the size of array");
scanf("%d",&maxsize);
front1=-1;
front2=maxsize/2-1;
rear1=-1;
rear2=maxsize/2-1;
do{
printf("1. insert1 2.delete1 3.TRAVERSE1 4.insert22 5.delete2 6.TRAVERS2 7.EXIT");
scanf("%d",&opt);
switch(opt){
case 1:
printf("enter elemnet ");
scanf("%d",&x);
insert1(x);
break;
case 2:
delete1();
break;
case 3:
traverse1();
break;
case 4:
printf("enter elemnet ");
scanf("%d",&x);
insert2(x);
break;
case 5:
delete2();
break;
case 6:
traverse2();
break;
}
}while(opt!=7);
getch();
}
3. C Program to Implement a Queue using an Array
#include <stdio.h>
#define MAX 50
int queue_array[MAX];
int rear = - 1;
int front = - 1;
main()
{
int choice;
while (1)
{
printf("1.Insert element to queue \n");
printf("2.Delete element from queue \n");
printf("3.Display all elements of queue \n");
printf("4.Quit \n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch (choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice \n");
} /*End of switch*/
} /*End of while*/
} /*End of main()*/
insert()
{
int add_item;
if (rear == MAX - 1)
printf("Queue Overflow \n");
else
{
if (front == - 1)
/*If queue is initially empty */
front = 0;
printf("Inset the element in queue : ");
scanf("%d", &add_item);
rear = rear + 1;
queue_array[rear] = add_item;
}
} /*End of insert()*/
delete()
{
if (front == - 1 || front > rear)
{
printf("Queue Underflow \n");
return ;
}
else
{
printf("Element deleted from queue is : %d\n", queue_array[front]);
front = front + 1;
}
} /*End of delete() */
display()
{
int i;
if (front == - 1)
printf("Queue is empty \n");
else
{
printf("Queue is : \n");
for (i = front; i <= rear; i++)
printf("%d ", queue_array[i]);
printf("\n");
}
} /*End of display() */
4. Circular queue using array
#define SIZE 5 /* Size of Circular Queue */
int CQ[SIZE],f=-1,r=-1; /* Global declarations */
CQinsert(int elem)
{ /* Function for Insert operation */
if( CQfull()) printf("\n\n Overflow!!!!\n\n");
else
{
if(f==-1)f=0;
r=(r+1) % SIZE;
CQ[r]=elem;
}
}
int CQdelete()
{ /* Function for Delete operation */
int elem;
if(CQempty()){ printf("\n\nUnderflow!!!!\n\n");
return(-1); }
else
{
elem=CQ[f];
if(f==r){ f=-1; r=-1;} /* Q has only one element ? */
else
f=(f+1) % SIZE;
return(elem);
}
}
int CQfull()
{ /* Function to Check Circular Queue Full */
if( (f==r+1) || (f == 0 && r== SIZE-1)) return 1;
return 0;
}
int CQempty()
{ /* Function to Check Circular Queue Empty */
if(f== -1) return 1;
return 0;
}
display()
{ /* Function to display status of Circular Queue */
int i;
if(CQempty()) printf(" \n Empty Queue\n");
else
{
printf("Front[%d]->",f);
for(i=f;i!=r;i=(i+1)%SIZE)
printf("%d ",CQ[i]);
printf("%d ",CQ[i]);
printf("<-[%d]Rear",r);
}
}
main()
{ /* Main Program */
int opn,elem;
do
{
clrscr();
printf("\n ### Circular Queue Operations ### \n\n");
printf("\n Press 1-Insert, 2-Delete,3-Display,4-Exit\n");
printf("\n Your option ? ");
scanf("%d",&opn);
switch(opn)
{
case 1: printf("\n\nRead the element to be Inserted ?");
scanf("%d",&elem);
CQinsert(elem); break;
case 2: elem=CQdelete();
if( elem != -1)
printf("\n\nDeleted Element is %d \n",elem);
break;
case 3: printf("\n\nStatus of Circular Queue\n\n");
display(); break;
case 4: printf("\n\n Terminating \n\n"); break;
default: printf("\n\nInvalid Option !!! Try Again !! \n\n");
break;
}
printf("\n\n\n\n Press a Key to Continue . . . ");
getch();
}while(opn != 4);
}
0 comments:
Post a Comment