Source Code : Program on FIFO

Program on FIFO.cpp

# include

int MAX = 5;

int n[5];

int front = -1;

int rear = -1;

int main()

{

int choice;

int a=1;

while(a)

{

printf("1.producer\n");

printf("2.consumer\n");

printf("3.Display\n");

printf("4.Quit\n");

printf("Enter your choice : ");

scanf("%d",&choice);

switch(choice)

{

case 1 :

insert();

break;

case 2 :

del();

break;

case 3:

display();

break;

case 4:

a=0;

break;

//exit(0);

default:

printf("Wrong choice\n");

}/*End of switch*/

}/*End of while */

}/*End of main()*/

insert()

{

int added_item;

if((front == 0 && rear == MAX-1) || (front == rear+1))

{

printf("Buffer Overflow \n");

return;

}

if (front == -1) /*If queue is empty */

{

front = 0;

rear = 0;

}

else

if(rear == MAX-1)/*rear is at last position of queue */

rear = 0;

else

rear = rear+1;

printf("Input the element for insertion in Buffer : ");

scanf("%d", &added_item);

n[rear] = added_item ;

}/*End of insert()*/

del()

{

if (front == -1)

{

printf("Buffer Underflow\n");

return ;

}

printf("Element deleted from Buffer is : %d\n",n[front]);

if(front == rear) /* queue has only one element */

{

front = -1;

rear=-1;

}

else

if(front == MAX-1)

front = 0;

else

front = front+1;

}/*End of del() */

display()

{

int front_pos = front,rear_pos = rear;

if(front == -1)

{

printf("Buffer is empty\n");

return;

}

printf("Buffer elements :\n");

if( front_pos <= rear_pos )

while(front_pos <= rear_pos)

{

printf("%d ",n[front_pos]);

front_pos++;

}

else

{

while(front_pos <= MAX-1)

{

printf("%d ",n[front_pos]);

front_pos++;

}

front_pos = 0;

while(front_pos <= rear_pos)

{

printf("%d ",n[front_pos]);

front_pos++;

}

}/*End of else */

printf("\n");

}/*End of display() */