开发者

Circularly Linked Lists C++

I've some prob to add a new elment in my Circularly Linked Lists... When I try to add the first...

if( empty() && pos ==1 )
{

    newnode = new Node(ele);
    newnode->next = newnode;

    last = newnode;
}

.. I've a glibc detected :double free or corruption (fasttop) ... do you know why ?

It's because i must point the .next on himself no? What should开发者_开发百科 i do, to point my forst on himself ?


The main reason to use a circularly linked list is simplification.

And for that, you should always (with circular list) use a header node.

For an empty circular list, the header node is linked back to itself, and is a permanent part of the list, it's not carrying any data – except in certain cases, a sentinel value.

With the header node in place, inserting a first node is just the same as inserting a second or third, exact same code.

So, do give it a try.

Cheers & hth.,


This is supposed to be your top node, considering its empty(), right? So, you should set your LL head node here. Also, in your code, you're setting a node's next node to itself. You should:

currentNode->Next = new Node();
lastNode = currentNode;

Do this after your head node is set.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜