list data structure that uses only the amount of memory needed for the number of elements in use at a given point in time Compared to Arrays Arrays easily allocateddeallocated direct access accessing element ID: 760796
Download Presentation The PPT/PDF document "Linked Lists Motivation A" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Linked Lists
Slide2Motivation
A
list
data structure that uses only the amount of memory needed for the number of elements in use
at a given point in time.
Slide3Compared to Arrays
Arrays:
- easily allocated/deallocated
- direct access (accessing element
n
is done simply by indexing: [n])
Linked Lists:
- no maximum number of elements
- no waste of memory for unused
elements.
Slide4Node
A
Node
is a dynamically allocated li
st
element
, implemented as a structure that has at least two members:
- key:
a (usually) unique identifier
for an element.
- next:
a pointer to a node.
Slide5Definition
A
Linked List
is a linear, dynamicall
y-
allocated data structure. It consists of a
head
which is a pointer to a node.
- The
head
points to the first element of the list; or is
NULL
when the list is empty.
- The
next
pointer of each node points to the next node in the list; except the last node, where
next
is
NULL
.
Slide6Examples:
Slide7Class Definitions
class node {
friend class
LList
;
private:
int
key;
node * next;
// circular reference OK
};
class
LList
{
private: node * head;
};
Slide8Algorithms
When designing a function that
operates on a Linked List, consider
if the solution works for:
- an empty list
- a list of one node
- a list of two or more nodes
Slide9Algorithms
"The Loop that solves all problems!"
(well, most anyway)
//
point p to the first/head node
node * p = head;
while (p != NULL) {
// process this node
...
p = p->next;
// go to next node
}
// or NULL
Slide10Constructors
#include <
iostream
>
n
ode::node() {
key = 0;
next = NULL;
}
LList
::
LList
() {
head = NULL;
}
Slide11Print
void
LList
::print() {
node * p = head;
while (p != NULL) {
cout
<< p->key << " ";
p = p-> next;
}
}
Slide12Search
// return
ptr
to found node or
// NULL = not found
node *
LList
::search(
int
srchKey
) {
node * p = head;
while (p != NULL) {
if (p->key ==
srchKey
)
return p;
p = p->next;
}
return NULL;
//looked at all, not found
}
Slide13Insert
A new node should first be created and populated:
void
LList
::insert(
int
newkey
) {
node * n = new node;
//allocate new node
n->key =
newkey
;
n->next = NULL;
insert
(n);
// some insertion method
}
// given pointer to new node
Slide14Insert (After)
Given:
-
after:
a pointer to a node in the List,
after which the new node will be
inserted. NULL to insert new head.
-
n:
a pointer to the new node, already
allocated and populated.
Slide15Insert
General Case: Blue arrows/null showvalues before insertion.Red arrows show links after insertion (changes)
Slide16Insert
New Head Case: red arrows show links after changes.Empty List Case:the same code willwork when head is NULL
Slide17Insert
void
LList
::
insertAfter
(node*
after,
node*
n
) {
if (head
== NULL || after == NULL) {
n-
>next =
head;
// insert as new
head
= n
;
// head of list
}
else {
// insert middle/tail of list
n-
>next
= after-
>next;
after-
>next
= n
;
}
// note: ORDER of statements is IMPORTANT!
}
Slide18Insert
Checks:
- Does it work for Empty List? [
head
is NULL]
- Does it work for a List of 1 element?
- as the new head? [
after
is NULL]
- after the current head? [
after
==
head
]
- Does it work for a list of 2 or more elements?
- as the new head? [
after
is NULL]
- as the new tail? (last in list) [
after
points to
tail
]
- in the middle of the list?
Slide19Remove
Given:
r
- a
pointer to a node in the
List to be
removed
Removes the node from the list, but does not
deallocate it.
Alternative:
Removes and deallocates the node.
Slide20Remove
Remove Head case:
Slide21Remove
General Case:Use b4 tofind apointer to the nodebeforethe one toremove
Slide22Remove
void
LList::remove(node *
r) {
if
(r ==
head)
head
=
head-
>next;
else {
// must find node BEFORE one being removed
node * b4
=
head
;
while
(b4->next != r)
{ b4
= b4->next
; }
b4-
>next = r->next;
}
r->next = NULL
; // detach following node
}
Slide23Remove
Checks:
- Assumption: never invoked on Empty List
- Assumption: r points to a node
actually
in the List.
- Does it work for a List of 1 element?
- Does it work for a list of 2 or more elements?
- removing the head?
- removing the tail?
- removing a node in the middle of the list?
Slide24Remove - Defensive Version
void LList::
remove(node *
r)
{
if (head == NULL) return;
// list empty, can't remove
if (r == NULL) return;
// nothing to remove!?
if
(r ==
x.head
)
x.head
=
x.head
->next;
else { // must find node BEFORE one being removed
node * b4
=
x.head
;
while
(b4->next !=
r
&& b4 != NULL
)
{ b4
= b4->next
; }
if (b4 == NULL) return;
// node not in list!!
b4-
>next = r->next;
}
r->next = NULL
; }
Slide25Remove
Note:
r
still points to the removed node
after remove() is finished.
The function invoking
remove()
may choose
to deallocate it or not.
Slide26Remove - invocation
void
LList::fun() {
...
node * p;
p =
ptr to node to remove
;
remove(p
);
cout <<
p-
>key
<<
" was removed";
delete p
;
// deallocate removed node
...
}
Slide27Length
// return: count of the nodes in the
list
void
LList
::length()
{
int num = 0;
node * p = head;
while (p != NULL) {
num
++;
p = p->next;
}
return num;
}
Slide28Vocabulary
Term
Definition
Node
A dynamically-allocated li
st
element
that has at least two members:
- key:
a (usually) unique identifier
for an element.
- next:
a pointer to a node.
Linked List
A linear, dynamically-allocated list data structure.
It consists of a
head
which is a pointer to a node,
and the list of nodes.
- The
head
points to the first element of the list;
or is NULL when the list is empty.
- The
next
pointer of each node points to the next node in
the list; except the last node, where
next
is NULL.