Difference between revisions of "W2295 Doubly-Linked Lists"

From Coder Merlin
m (Editorial review and minor corrections)
Line 1: Line 1:
[[File:Doubly-linked-list.svg|thumb|link=|Doubly-Linked List]]
[[File:Doubly-linked-list.svg|thumb|link=|Doubly-Linked List]]
== Prerequisites ==
== Prerequisites ==
{{MerlinNoteFromEditor|Need the prereqs here, or delete the heading.}}
== Introduction ==
== Introduction ==
A '''Linked Data Structure''' consists of a series of records, called '''nodes''', linked to one another through a series of '''pointers'''.  This arrangement contrasts with some other data structures, such as arrays, where data are found through address calculations.
A '''Linked Data Structure''' consists of a series of records, called '''nodes''', linked to one another through a series of '''pointers'''.  This arrangement contrasts with some other data structures, such as Arrays, where data are found through address calculations.
 
== Linked List ==
== Linked List ==
[[File:Singly-linked-list.svg|thumb|right|link=|Singly-linked-list]]
[[File:Singly-linked-list.svg|thumb|right|link=|Singly-linked-list]]
A linked list is built by creating '''nodes''', where each node is a structure consisting of the data (or some reference to the data) and an address field pointing to the next structure.  This pointer is often referred to as ''next''.  A special pointer, called '''head''', points to the first node of the list. If a node doesn't have a subsequent node, its next pointer will be point to a special value called '''nil'''.  If a list is empty, the ''head'' itself will point to nil.
A Linked List is built by creating '''Nodes''', where each node is a structure consisting of the data (or some reference to the data) and an address field pointing to the Next structure.  This pointer is often referred to as ''Next''.  A special pointer, called '''Head''', points to the first node of the list. If a node doesn't have a subsequent node, its next pointer points to a special value called '''nil'''.  If a list is empty, the ''head'' itself will point to nil.


{{Caution|
{{Caution|
It's vitally important that we always maintain a pointer to every node in our list so as not to lose it forever.
It is vitally important that you always maintain a pointer to every node in your list so as not to lose the node forever.
}}
}}


== Doubly-Linked List ==
== Doubly Linked List ==
A doubly-linked-list is similar to a singly-linked-list but in addition to having a ''next'' pointer each node also has a second pointer, often referred to as '''prev''' (for previous) which points to the prior node.
A Doubly Linked List is similar to a Singly Linked List but, in addition to having a ''next'' pointer, each node also has a second pointer, often referred to as '''prev''' (for previous) that points to the prior node.


== Common Functions ==
== Common Functions ==
Common functions on a linked list include:
Linked Lists have these common functions:
* '''isEmpty()''' - returns true iff the list is empty
{{MerlinNoteFromEditor|Review: is the "iff" correct in the first bullet, or should it be "if"?}}
* '''count()''' - counts the nodes in the list
* '''isEmpty()''' returns true iff the list is empty
* '''traverse()''' - visits each node in the list, in order
* '''count()''' counts the nodes in the list
* '''append(newNode)''' - appends a node to the end of the list
* '''traverse()''' visits each node in the list, in order
* '''insert(newNode, afterNode)''' - inserts a node immediately after the specified node; nil may be used to indicate the first node
* '''append(newNode)''' appends a node to the end of the list
* '''delete(node)''' - deletes the specified node
* '''insert(newNode, afterNode)''' inserts a node immediately after the specified node; nil can be used to indicate the first node
* '''delete(node)''' deletes the specified node


== Linked Lists vs Arrays ==
== Linked Lists vs Arrays ==
Line 35: Line 39:
| Memory Size
| Memory Size
| Fixed, often too large or too small
| Fixed, often too large or too small
| May grow or shrink as needed
| Can grow or shrink as needed
|-
|-
| Memory Allocation Method
| Memory Allocation Method
| Must be contiguous
| Must be contiguous
| May be freely located and relocated
| Can be freely located and relocated
|-
|-
| Speed of Accessing Particular Data
| Speed of Accessing Data
| Nearly immediate, simple arithmetic. Strong locality of reference.
| Nearly immediate, simple arithmetic. Strong locality of reference.
| Requires traversal of each node until target node is reached. Poor locality of reference.
| Requires traversing each node until target node is reached. Poor locality of reference.
|-  
|-  
| Memory Consumption
| Memory Consumption
| Minimal overhead
| Minimal overhead
| May have substantial overhead
| Can have substantial overhead
|}
|}


{{MerlinNoteFromEditor|Need the contents for the three topics below; or delete the headings.}}
== Key Concepts ==
== Key Concepts ==
== Exercises ==
== Exercises ==
== References ==
== References ==

Revision as of 21:02, 13 January 2022

Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder
Doubly-Linked List

Prerequisites[edit]

 This article can be improved by:  Need the prereqs here, or delete the heading.

Introduction[edit]

A Linked Data Structure consists of a series of records, called nodes, linked to one another through a series of pointers. This arrangement contrasts with some other data structures, such as Arrays, where data are found through address calculations.

Linked List[edit]

Singly-linked-list

A Linked List is built by creating Nodes, where each node is a structure consisting of the data (or some reference to the data) and an address field pointing to the Next structure. This pointer is often referred to as Next. A special pointer, called Head, points to the first node of the list. If a node doesn't have a subsequent node, its next pointer points to a special value called nil. If a list is empty, the head itself will point to nil.

CautionWarnIcon.png

It is vitally important that you always maintain a pointer to every node in your list so as not to lose the node forever.

Doubly Linked List[edit]

A Doubly Linked List is similar to a Singly Linked List but, in addition to having a next pointer, each node also has a second pointer, often referred to as prev (for previous) that points to the prior node.

Common Functions[edit]

Linked Lists have these common functions:

 This article can be improved by:  Review: is the "iff" correct in the first bullet, or should it be "if"?

  • isEmpty() returns true iff the list is empty
  • count() counts the nodes in the list
  • traverse() visits each node in the list, in order
  • append(newNode) appends a node to the end of the list
  • insert(newNode, afterNode) inserts a node immediately after the specified node; nil can be used to indicate the first node
  • delete(node) deletes the specified node

Linked Lists vs Arrays[edit]

Category Arrays Linked Lists
Memory Allocation When initially created As the program executes
Memory Size Fixed, often too large or too small Can grow or shrink as needed
Memory Allocation Method Must be contiguous Can be freely located and relocated
Speed of Accessing Data Nearly immediate, simple arithmetic. Strong locality of reference. Requires traversing each node until target node is reached. Poor locality of reference.
Memory Consumption Minimal overhead Can have substantial overhead

 This article can be improved by:  Need the contents for the three topics below; or delete the headings.

Key Concepts[edit]

Exercises[edit]

References[edit]