Circular Linked List Insertion help!

I need to insert a value to a node BEFORE the cursor.

My program faces a compiler issue when it reaches the traversing portion. I have no idea why, to me it seems as if my program is logically consistent and it should work as intended.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void CLinkedList::insert(int value)
{
	NodeType *ptr = new NodeType;
	ptr->info = value;
	ptr->next = NULL;
	NodeType *temp = cursor; //traversing node
	if (cursor == NULL) //check if empty
	{
		ptr->next = ptr; //pointing to itself
		cursor = ptr;
	}
	while (temp->next != cursor) //traverse until the previous node
	{
		temp = temp->next;
	}
	temp->next = ptr;
	ptr->next = cursor;
}
First of all, THERE'S A MEMORY LEAK.

But other than that, you should show us a small compilable version of this.

Best guess says that you have to define temp with a new. If "next" is also a nodetype pointer, then whenever you define it you use a new.

REMEMBER TO DELETE EVERY NEW, or use smart pointers.
Is it sufficient to do

ptr = temp = NULL;

or must I delete both pointers everytime?

Where is there a memory leak? I cannot show you a compilable version of this because the compiler just crashes when it reaches the while statement
Last edited on
Your compiler cant crash. Otherwise this would be a completely different problem. By compilable, I mean you can put the code into a compiler, and compile it with or without errors.

So show your code.

Overlooking the issue, your logic seems sound. "next" is a NodeType pointer inside its NodeType, "ptr" is the new element, "cursor" is the end element, and temp is used to traverse the pointers. But maybe the compiler simply has a grudge against it.

I did a less than 1 minute google and got this answer, it has _pTail which is like cursor, but it has _pHead which you don't have, so maybe that's your solution:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void LinkedList::tailAppend(int val)
{
    /* The list is empty? */
    if (_pHead == NULL) {
        /* the same to create a new list with a given value */
        _pTail = _pHead = new Node(val);
    }
    else
    {
        /* Append the new node to the tail */
        _pTail->_pNext = new Node(val);
        /* Update the tail pointer */
        _pTail = _pTail->_pNext;
    }
}

from: http://runnable.com/Us52yozciVFWAAbW/how-to-add-a-node-into-a-linked-list-for-c%2B%2B
Last edited on
Once again, where is the memory leak?

There is no "head" or "tail" in a circular linked list, so maybe you should spend more than a minute searching on google mate
No need to show more code: Add a return statement in the if branch.
Otherwise you're accessing a null pointer by using temp->next (remember that if cursor is null, temp is null too).
Since it's a class just make sure you delete your items in the destructor... there's no memory leak in this function since the pointer gets stored and <<will>> be used (that's what containers are for...).
Do I get this right?
you have a node* called cursor stored somewhere in NodeType.
When inserting you want to add the node before the cursor.
Do you want the cursor to point to the new node after the insert?
(I'll assume no)
Can your cursor be a nullptr if there are elements in the buffer?
(I'll assume no)

So you basically need to:
create a new node.
next node of the new node is cursor.
make the previous node's next be the new node

If my assumtions are correct:
you assign *temp=cursor before checking if cursor is a nullptr
so if cursor == nullptr THEN cursor = ptr;
temp is still a nullptr.
so temp->next gives an undefined value

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void CLinkedList::insert(int value)
{
    NodeType *ptr = new NodeType;
    ptr->info = value;

    if ( cursor == NULL) //check if empty
        cursor = ptr; // cursor points to ptr
    ptr->next = cursor; // immediately assign next ptr

// traverse till the node's points to cursor and reassign next node
    NodeType *prev = cursor; // never is a nullptr! :D
    while (prev->next != cursor)
        prev=prev->next;
    prev->next = ptr;
}


Last edited on
I found the source of the problem, I didn't put an else after the if.

Topic archived. No new replies allowed.