The 2 fundamental data structures in computer science
Review: the
array
- Array =
a
series of
variables
that:
- Are of the same
data type
- Are stored in
consecutive memory locations
|
- The memory used to
store and
array must be
allocated
"up front"
|
Review: the
array
- Strength of
an array:
|
Review: the
array
- Weaknesses of
an array ---
cannot
increase the
array size
|
Review: the
array
- Weaknesses of
an array --- it takes
a long time to:
|
The linked list data structure
- The linked list
and array are
complementary
to each other.
-
Characteristics of
a
linked list:
- Each list element is
allocated
individually
(and then linked
into the list)
(Memory for
all array elements are
allocated
up front)
- It's
easy to
insert/delete elements
from a linked list)
(It's not easy to
insert/delete elements
from an array)
- It is
slow to
look up
elements in
a linked list by
its index)
(It is fast to
look up
elements in
an array by
its index)
|
- Comment:
- Because each
list element is
allocated indvidually:
- There is
no need
to "shift"
list elements
|
|
|
What does a linked list look like
- A linked list consists of
a
chain of
list objects:
- A list object of
often called:
a
"node"
- Every node consists of
2 parts:
- One of more
data fields (contain the
information stored in the
linked list
- A
link (reference) variable
(contains the
reference (= address) of
the next
node/list element)
The link in the
last node is
null
(=
end of the
list)
|
|
Programmer perspective
of a linked list
- A Java program will have a
reference variable
(commonly named as
head
or
first)
that contains
the reference to
the
first node
(= list element):
-
Consequently:
only the
data stored in
the first node is
immediately accessible
- Accessing the
data stored in the
other nodes:
- Data in the
2nd node is
accessed through the
link in the
1st node
- Data in the
3rd node is
accessed through the
link in the
2d node
- And so on
|
|
Defining a Node
class for a linked list
- Suppose we want to
create a
linked list where:
- Each node stores
a
int (integer)
|
- We can define the
following
Node class
for this purpose:
public class Node
{
int item; // int data stored in Node
Node next; // Link that reference to the next node
// in the linked list
}
|
- Note:
- The class Node contains
a reference variable
next that
references to
an
Node (same class) object
- The next variable
is used to
create a
chain of
Nodes
- You can define
other
data field depending
on what you want to store in
a Node
|
|
How is a
linked list stored
in the computer memory ?
How is a
linked list stored
in the computer memory ?
- Internally, this
linked list is
stored in
the memory as
follows:
Notice:
-
head contains
the
address
(reference)
of the first
Node object
- Each
next field
contains
the
address
(reference)
of the subsequent Node object
|
|
What can
Linked Lists do for us ?
It's easy to
extend
(= add a node to) a
linked list:
What can
Linked Lists do for us ?
It's easy to
insert a node
at any position in
linked list:
Array vs Linked List
Array |
Linked list |
- Array elements are
stored
contiguously in
memory
- All
array elements are
allocated
at once
- Once allocated,
the
number
of elements in
the array
is
fixed
-
Only store
data fields,
do not need to store
non-data
fields
-
Accessing the
kth element
in an array is
fast
- Inserting a
value in the
middle of
an array is
difficult
(need to shift elements over)
|
- List elements (= "nodes")
do
not need to be
stored
contiguously
in memory
- Nodes can be
allocated
piece meal
when
needed
- We can increase the
number of elements in
a list
easily by
increasing the
length of the
chain
-
Requires the
use of
a
linking
field (next)
to create a chain
- Need to
traverse
the chain to
reach the
kth element
---
slow
- Inserting a
Node in the
middle of
a linked list is
easy
(splice it in...)
|
Array and
linked list
complement
each other in the
strengths/weaknesses...
❮
❯