Circular Single linked list
I was wondering if anybody could explain how to insert and remove from a circular linked list.
I have a book that only provides a picture of nodes with the last one pointing to the first. From that I came up with this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
template <typename T>
void List<T>::insert(int index, const T &newData)
{
Node<T> *newPtr = new Node<T>;
newPtr->data = newData;
if(index == 1)
{
newPtr->next = head;
head = newPtr;
tail = head;
}
else
{
size++;
Node<T> *prev = find(index-1);
newPtr->next = prev->next;
prev->next = newPtr;
tail = newPtr;
tail->next = head;
list_sort();
}
}
|
For some reason, that just seems wrong.
Thanks. :-)
I've not wriiten a circular list - but I've given some thought about how one could be written.
I think I'll do one as an exercise.
Topic archived. No new replies allowed.