LLEssentials

LLEssentials - CS
112
:
Data
Structures
...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS
112
:
Data
Structures
 Fall
2011
 Linked
List
Essen8als:
Wednesday,
Oct
4,
2011
 Linked
Lists
Essen8als
 The
following
discussion
highlights
the
key
things
to
know/understand/remember
 about
linked
lists
and
nodes.
For
purposes
of
keeping
the
discussion
focused
on
the
 concepts,
nodes
are
not
generic
(you
can
assume
int data
in
the
nodes)
 CS112


10/04/11
 Sesh
Venugopal
 2
 Declara8on
of
Node
 Node ptr; Does
NOT
create
a
new
node.
This
is
just
a
“declara8on
of
intent”,
that
 the
variable
ptr is
intended
to
refer
(point)
to
a
Node object.
Immediately
 aSer
the
declara8on,
ptr holds
the
value
null ptr null Node
is
ONLY
created
when
you
do
a
new ptr = new Node(…) The
variable
ptr which
is
on
the
leS
hand
 
side
of
the
assignment
will
now
get
the

 ptr 
address
of
the
new
Node object
 CS112


10/04/11
 Sesh
Venugopal
 3
 Linked
List
Varia8ons The
basic
linked
list
(LL)
is
the
one
in
which
the
last
node
points
to
nothing,
 i.e.
the
next field
of
the
last
node
is
a
null pointer
 front The
circular
linked
list(CLL)
is
just
like
the
basic
LL
except
that
the
last
node
 points
back
to
the
first,
i.e.
the
next field
of
the
last
node
refers
to

the
first
 rear The
advantage
of
the
CLL
is
that
both
the
ends
of
the
linked
lists
can
be
 accessed
with
a
single
rear pointer,
without
stepping
through
the
other
 nodes
(e.g.
this
feature
is
used
to
advantage
in
the
queue
data
structure)

 CS112


10/04/11
 Sesh
Venugopal
 4
 Linked
List
Varia8ons The
doubly
linked
list
(DLL)
uses
nodes
that
point
to
the
next
as
well
as
the
 previous
nodes
 front The
ability
to
go
both
ways
makes
coding
easier
for
many
linked
list
 opera8ons
since
you
can
never
overshoot
in
a
way
that
you
can’t
backpedal
 (except
if
you
shoot
past
the
last
node!)
 rear And
of
course,
you
can
have
a
doubly
circular
linked
list
(DCLL)
–
the
last
 node’s
next
points
back
to
the
first,
and
the
first
node’s
previous
points
to
the
 last
 CS112


10/04/11
 Sesh
Venugopal
 5
 Fundamental
Linked
List
Opera8ons
 •  There
are
three
fundamental
things
you
need
to
 know
how
to
do
with
linked
lists:
  
Stepping
through
the
nodes
(managing
links)
  
Adding
a
node
(arranging/rearranging
links)
  
Dele8ng
a
node
(arranging/rearranging
links)
 •  Higher
level
opera8ons
are
built
off
of
varia8ons
and
 combina8ons
of
these
fundamental
opera8ons,
e.g.:
  
Searching
for
an
item
=
stepping
+
comparing
  
Dele8ng
an
item
=
searching
+
dele8ng
node
  
Inser8ng
an
item
into
a
sorted
list
=
searching
+
adding
node
 CS112


10/04/11
 Sesh
Venugopal
 6
 Stepping
Through
Nodes
 front while loop
 Node ptr=front; while ( ptr != null) { <do stuff> ptr = ptr.next; } for loop
 for (Node ptr=front;ptr != null;ptr=ptr.next) { <do stuff> } Verify
that
this
code
works
even
if
front is
null (list
is
empty)
 A
note
about
null
pointer
excep0ons
 A
pointer
can
have
null
in
it,
and
you
can
compare
it
with
null,
or
copy
it
to
 some
other
pointer
without
any
problem.
It
is
ONLY
when
you
say
 pointerDOTstuff

that
you
get
the
excep8on
if
the
pointer
is
null.
 CS112

10/04/11
 Sesh
Venugopal
 7
 Stepping
Through
Nodes:
CLL
 rear do-while loop
 Node ptr=rear; do { <do stuff> ptr = ptr.next; } while (ptr != rear); for loop
 There
isn’t
an
equivalent
for
loop
because
the
 termina8on
condi8on
is
checked
before
each
 itera8on
of
the
loop
 See
that
this
code
DOES
NOT
work
if
rear is
null (list
is
empty)
–
you
will
 need
to
check
for
the
rear == null condi8on
before
the
loop,
and
only
do
 the
loop
if
the
condi8on
is
false.
Also,
some8mes
you
will
need
to
start

 at
the
first
node
of
the
list,
in
which
case
this
loop
will
not
work.
 CS112


10/04/11
 Sesh
Venugopal
 8
 Stepping
Through
Nodes:
DLL/CDLL

 front Same
as
for
LL
 rear Same
as
for
CLL
 CS112


10/04/11
 Sesh
Venugopal
 9
 Stepping
Through
With
Intent
To
 Delete
Node
OR
Add
Before
Node
 KEY:
You
need
to
get
a
pointer
to
point
to
the
node
BEFORE
the
“target”
node
 TRICK:
Use
two
pointers
that
move
in
lock
step,
the
leading
pointer
is
one
node
 ahead
of
the
lagging
one,
and
stops
at
the
node
to
be
deleted
(or
before
which
 another
node
is
to
be
added)
 front Step
1
 Step
2
 null prev X
 ptr prev (No
need
for
two
 pointers
if
DLL!)
 ptr SPECIAL
CHECKS
aSer
ptr
stops
advancing
  
If
ptr is
null
  
If
ptr is
not
null but
prev is
null 
 ?
 WHAT
WOULD
YOU
NEED
TO
DO
DIFFERENTLY
FOR
A
CLL?

 CS112


10/04/11
 Sesh
Venugopal
 10
 LinkedList versus
Node There
is
NO
standard
LinkedList class
that
can
serve
the
need
of
all
 applica8ons
that
use
linked
lists.
OSen,
applica8ons
(and
other
data
 structures
that
are
built
using
linked
lists)
will
deal
directly
with
Node objects
and
maintain
their
own
linked
lists
as
a
sequence
of
Nodes.
 BE
CAREFUL
WHEN
YOU
WRITE
METHODS
THAT
MODIFY
THE
STRUCTURE
OF
A
 LINKED
LIST,
TAKING
THE
“HANDLE”
NODE
(FRONT/REAR)
AS
PARAMETER
 void addToFront(Node front, int item) { Node ptr=new Node(item,null); ptr.next = front; front = ptr; } !
 This
code
WILLNOT
work
 WHY?
HOW
TO
FIX?
 Parameter
values

 only
go
one
way
 Node addToFront(Node front, int item) { Node ptr=new Node(item,null); ptr.next = front; return ptr; } Doesn’t
work
because
the
change
to
front inside
the
method
is
not
carried
over
to
the
caller
 CS112


10/04/11
 Sesh
Venugopal
 RETURN
the
new
front
 11
 ...
View Full Document

This note was uploaded on 03/29/2012 for the course CS 112 taught by Professor Sesh during the Spring '07 term at Rutgers.

Ask a homework question - tutors are online