简体   繁体   English

如何将单链接列表转换为循环列表

[英]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

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM