Queue List Implementation

 Linked List implementation of Queue

typedef struct Node *PtrToNode;

typedef PtrToNode Queue, Front, Rear;

 

int IsEmpty(Queue Q);

Queue CreateQueue();

void DisposeQueue(Queue Q);

void MakeEmpty(Queue Q);

void Enqueue(int X, Queue Q);

void Dequeue(Queue Q);

 

struct Node

{

         int Element;

         struct Node * Next;

};

 

// Check for empty Queue

int IsEmpty(Queue Q)

{

         return (Q == NULL);

}

 

// Create empty queue

Queue CreateQueue()

{

         return NULL;

}

 


 

// Insert element at Rear

void Enqueue(int X, Queue Q)

{

         PtrToNode Tmpcell;

         Tmpcell = malloc(sizeof(struct Node));

         Tmpcell->Element = X;

Tmpcell->Next = NULL;

         if(Q == NULL)

         {

                 Front = Rear = Tmpcell;

return;

}

Rear->Next = Tmpcell;

Rear = Tmpcell;

}

 

// Delete element from Front

void Dequeue(Queue Q)

{

         PtrToNode Tmpcell;

         if(IsEmpty(Q))    

                 printf(“Error – Empty queue. Cant delete.”);

         else

{

                 Tmpcell = Front;

                 Front = Front->Next;

                 free(Tmpcell);

}      

}


No comments:

Post a Comment

Don't be a silent reader...
Leave your comments...

Anu