Mya Lu
Mya Lu

Reputation: 1

How to convert singly linked list to circular list

I am trying to create a circular list out of my singly linked list that puts numbers in ascending order but sadly it is not turning out so well. It seems like it is making the list, but for some reason it is not putting the numbers entered ind ascending order. Please help.

list::list()
{
 head =NULL;
}

void list::insertElement(int element)
{
    //ascending order
    node *temp, *curr, *prev;
    temp = new node;
    temp->item = element;

    for(prev = head, curr = head/*value set*/; (curr !=head)&&
    (temp->item>curr ->item)/*condition*/; 
    prev = curr, curr = curr->next/*loop expression*/);

    if (prev == head)
    {
    temp-> next = head;
    head = temp;
    }
    else
    {
    temp -> next = curr;
    prev -> next = temp;
    }
  }//end of function

Upvotes: 0

Views: 1834

Answers (1)

Logicbomb
Logicbomb

Reputation: 541

If you want to convert your singly linked list in to circular linked list, you just need to set pointer like this.

node *first;
temp = first;
while(temp->link != NULL)
{
   temp = temp->link;
// its traversing upto last node.
}

//here assign last node of link address to first node;

temp->link = first;

Upvotes: 1

Related Questions