top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration

How can I implement a queue using a circular linked list.

+1 vote
124 views

Can someone help me with algorithm and code?

posted Aug 16, 2014 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

I could give u an idea how to implement the queue via circular link list.

Assuming

typedef struct node{
int value;
struct node *next
} node;

1) You need to main the address of two pointer to maintain the queue using circular link list.
a. Head --> For releasing the element from the queue.
b. Tail--->For insertion the element in the queue. i.e whose next will be pointing to the first one.
Other things will be depended where will u call these.

2) While inserting the element in the queue.

InsertElementInQueue(node *head,node *tail,int p)
{
     allocate the memory for the new node. 
     newnode->value=i_value;
      if (Head == NULL)
      {
              head = newnode;
              newnode->next=head;
              tail=head;
      }
      else
      {
            newnode->next=head;
             tail->next=newnode;
      }
      return;
}

3) While removing the element from the queue.

RemoveElementInQueue(node *head,node *tail)
{
      node *temp;
      if (Head == NULL)
      {
           printf("Queue is already empty\n");
           return failue;
      }
      else
      {
           printf("Now the turn is for %d",head->value);
            if(head != tail)
            {
                    temp=head;
                     head=head ->next;
            }
            else
            {
                    temp=head; 
                    head=tail=NULL;
            }
             free temp;
      }
      return;
}
answer Aug 18, 2014 by Double S
Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...