0% found this document useful (0 votes)
274 views

Circular Doubly-Linked Lists

Circular doubly-linked lists use a head node to link elements in a circle, allowing easy insertion and removal from any point in the list. A stack implements LIFO using a circular doubly-linked list by prepending new elements, while a queue implements FIFO by appending new elements and removing the head element. Checking if a circular doubly-linked list is empty is done by checking if the head node's first and last pointers point to itself.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
274 views

Circular Doubly-Linked Lists

Circular doubly-linked lists use a head node to link elements in a circle, allowing easy insertion and removal from any point in the list. A stack implements LIFO using a circular doubly-linked list by prepending new elements, while a queue implements FIFO by appending new elements and removing the head element. Checking if a circular doubly-linked list is empty is done by checking if the head node's first and last pointers point to itself.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Circular Doubly-linked Lists

Doubly-linked list...

... with head and tail pointers

Doubly-linked list...

Consider linking the elements in a circle


A B C

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

Let#s add a List $ead%


A list head C B

&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

list head#s last

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.

A list head#s first list head C B

B#s prev

list head#s last

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.

You might also like