Circular Linked Lists
Hi. I was trying to implement a circularly linked list in my ADT queue class. I was wondering how to change the one pointer that points to the back of the queue to come back around to the front, rather then just pointing to Null. Do I have to declare it somewhere? Thanks for any assistance.
[291 byte] By [
kb8coolj] at [2007-11-20 10:48:51]

# 1 Re: Circular Linked Lists
When you insert the first element into the list, make sure its 'next' pointer (and 'prev' pointer if it's double-linked) points to itself. Likewise be careful when removing the last element from the list to actually put the list into empty state (however you implement this).
Other than that, just make sure you don't have any functions traversing the list generate endless loops.
# 2 Re: Circular Linked Lists
So everytime I push on a new value, I need to reassign the next to my back pointer? Basically, I have an enqueue which has a newptr which was inputted and my code is simply:
if (isEmpty()){
mbackptr = newptr;
mbackptr -> next = mbackptr;}
does that look correct? Thanks again.
# 3 Re: Circular Linked Lists
So everytime I push on a new value, I need to reassign the next to my back pointer?Not every time, only for the first value (i.e. in if (isEmpty())).
Once you have one element in the list, inserting more elements into a circular list is equivalent to inserting them into a linear list.