Circular Doubly-Linked Lists
Circular Doubly-Linked Lists
Doubly-linked list...
Doubly-linked list...
Now each element has its own previous pointer, so given any element, we can insert it into, or remove it rom, a list. !till has special case or inserting irst element, but can otherwise use generic doubly-linked-list insertion"removal Alligators and Ducks is a bit simpli ied
&he list head is typically an automatic, static or global variable ' not allocated rom the heap ' so it is always available List insertion and removal is now consistent( no special case or irst or last element) &o append a new element to the list, insert it be ore the list head. &o prepend a new element, insert it be ore the list head#s next element.
Nomenclature
*or elements +nodes,, we have re erred to the light blue arrows as next pointers, and the brown arrows as previous pointers. *or the list head, let#s re er to them instead, as first and last pointers.
A list head#s first list head C B B#s next B#s prev
Nomenclature +continued,
&o append an element to the list, insert it be ore the list head +as stated be ore,. &o prepend an element, insert it be ore the list head#s first element.
B#s prev
B#s next
-mpty List
&esting whether a list is empty is accomplished by checking whether either the list head#s first or last pointer points to the list head itsel .
list head
.n C, that re/uires a 0cast1 to convert the list element type to the list head type, to prevent the compiler rom complaining about comparison o pointers to di erent types)
if ((ListHead *) listHead.pFirst == &listHead) { // the list is empty }
!tack
A stack is 0last in, irst out1 also known as LIFO. An element is pushed onto the top o a stack, and popped rom the top o the stack. 2nly the top o the stack is manipulated. push is implemented by a prepend operation, i.e., insert new element be ore the element pointed to by list head#s first pointer, i.e., the new element becomes the new first element. pop is implemented by removing the element pointed to by the list head#s first pointer.
3ueue
A /ueue is a 0 irst in, irst out1 also known as FIFO. &hink about a cashier line at the grocery store. &he irst person to arrive is served irst. As other people arrive, they go to the end o the line) the tail o the /ueue. &he person at the head o the /ueue is the ne4t to be served. Adding to a /ueue is accomplished with an append operation, i.e., add the new element be ore the list head, i.e., it becomes the new last element. 5emoving rom a /ueue is accomplished by removing the element pointed to by the list head#s first pointer.