Saturday, August 20, 2011

Queue Implimentation using linked list

//Queue Implimentation using linked list
#include<stdio.h>
#include<conio.h>
struct queue
{
int data;
struct queue *next;    
};
main()
{
int item,i;
struct queue *f;
struct queue *r;
void insert(struct queue **r,struct queue **f,int item);
int delet(struct queue **f);  
 f=NULL;r=NULL;
 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(&r,&f,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(&f);
         if(item==-1000)
          {
                        printf("\nUnderflow");
          break;
          }
         else          
         printf("\nitem deleted from queue is: %d",item);
               
 }
 else
 break;
 }
 getch();
 return 0;
}
void insert(struct queue **r,struct queue **f,int item)
{
     struct queue *q;
     q=(struct queue *)malloc(sizeof(struct queue));
     q->data=item;
     q->next=NULL;
   
     if((*r)==NULL&&(*f)==NULL)
      {
       *f=q;                
      }
     else
     {
     (*r)->next=q;
   
     }
   *r=q;
}
int delet(struct queue **f)
{
    int item;
    struct queue *q;
 
    if((*f)==NULL)
     return -1000;
     q=*f;
    item=q->data;
    *f=(*f)->next;
    free(q);
    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