Saturday, August 20, 2011

Queue Implimentation using array

//Queue Implimentation using array
#include<stdio.h>
#include<conio.h>
struct queue
{
int a[5];
int f;
int r;    
};
main()
{
      int item,i;
struct queue q;
void insert(struct queue *q,int item);
int delet(struct queue *q);  
 q.f=q.r=-1;
 while(1)
 {
 printf("\n Do you insert element is queue if Yes press \"1\"  otherwise press \"0\" :");
 scanf("%d",&i);
 if(i==1)
 {
         printf("\nEnter the element to be insert in queue: ");
         scanf("%d",&item);
         insert(&q,item);
 }
 else
 break;
 }

 while(1)
 {
 printf("\n Do you delete element from queue if Yes press \"1\"  otherwise press \"0\" :");
 scanf("%d",&i);
 if(i==1)
 {
         item=delet(&q);
         if(item==-1000)
          {
                        printf("\nUnderflow");
          break;
          }
         else          
         printf("\nitem deleted from queue is: %d",item);
               
 }
 else
 break;
 }
 getch();
 return 0;
}
void insert(struct queue *q,int item)
{
     if (q->f==0&&q->r==4||q->f==q->r+1)
     {
         printf("\nOverflow");
         return;                          
     }
     if(q->f==-1&&q->r==-1)
      {
      q->f=0;
      q->r=0;                
      }
     else if (q->r==4&&q->f!=0)
      q->r=0;
      else
      q->r=q->r+1;
   
      q->a[q->r]=item;
}
int delet(struct queue *q)
{
    int item;
    if(q->f==-1)
     return -1000;
    item=q->a[q->f];
    if(q->f==q->r)
    q->f=q->r=-1;
    else if(q->f==4)
    q->f=0;
    else
    q->f=q->f+1;
    return item;
}

Previous Next Home
0 Comments
Comments

Leave a Comment

:)) ;)) ;;) :D ;) :p :(( :) :( :X =(( :-o :-/ :-* :| 8-} :)] ~x( :-t b-( :-L x( =))

Twitter Delicious Facebook Favorites More