Wednesday, December 14, 2011

Queue Implementation

Queue:
Implement a queue[ Add( ) and Delete( )] operations using

1.Circular Array
2.Singly Linked Lists and TWO POINTERS
3.Circular Linked List and SINGLE POINTER

Deque:
Implement the following Deque operations  using i)Linked List ii)Stacks

1.Enqueue (  )
2.Dequeue ( )

Priority Queue using LINKED LIST .Discuss advantage of Priority Queue.

http://quiz.geeksforgeeks.org/priority-queue-set-1-introduction/
http://www.geeksforgeeks.org/why-is-binary-heap-preferred-over-bst-for-priority-queue/

void INSERT_QUEUE_TWO(struct node *&front, struct node *&rear,int data)
{struct node *tmp;
 tmp=(struct node *)malloc(sizeof(struct node));
 tmp->info=data;
 tmp->link=NULL;

 if(front==NULL)
    front=tmp;
 else
    rear->link=tmp;//LAST NODE POINTS TO TMP
 rear=tmp;//REAR ALSO POINTS TO TMP
}

void INSERT_QUEUE_SINGLE(struct node *&rear,int data)
//INSERTION is at the beginning of a CIRCULAR LL
{struct node *q,*tmp;
 tmp=(struct node *)malloc(sizeof(struct node));
 tmp->info=data;

if(rear==NULL)
    {rear=tmp;
    tmp->link=rear;}
else
    {tmp->link=rear->link;
     rear->link=tmp;
     rear=tmp;}
}

void DELETE_QUEUE_TWO(struct node *&front,struct node *&rear,int data)
{struct node *tmp;
 if (front==NULL)
    printf("Queue UNDEFLOW");
 else
    {tmp=front;//Delete From Front
    front=front->link;
    free(tmp);}
}

void DELETE_QUEUE_SINGLE(struct node *&rear)
{struct node *tmp,*q;
 if (rear==NULL)
    {printf("Queue UNDEFLOW");
    return;}
   
 if(rear->link==rear)//ONLY ONE ELEMENT
    {tmp=rear;
     rear=NULL;
     free(tmp);
     return;}
   
tmp=rear->link;
rear->link=tmp->link;
free(tmp);
}

No comments:

Post a Comment