



















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Major points from this lecture are: Lists and Trees, List Overview, Simple List Interface, List Data Structures, List Terminology, Ways of Building a Linked List, Building a Linked List, Accessing List Elements, Recursion On Lists, Static Method . Object-Oriented Programming and Data Structures course includes program structure and organization, object oriented programming, graphical user interfaces, algorithm analysis, recursion, data structures (lists, trees, stacks, queues, heaps, search tree
Typology: Lecture notes
1 / 27
This page cannot be seen from the preview
Don't miss anything!




















Maintain
an
ordered
collection
of elements
(with
possible
duplication)
-^
Create
a^ list
-^
Access
elements
of a^ list
sequentially
-^
Insert
elements
into
a^ list
-^
Delete
elements
from
a^ list
Random
access
-^
Fixed
size:
cannot
grow
or shrink
after
creation
(Sometimes
simulated
using
copying)
-^
No
random
access
(Sometimes
random
‐access
is^ “simulated”
but
cost
is^ linear)
-^
Can
grow
and
shrink
dynamically
Must
specify
array
size
at
creation
-^
Insert,
delete
require
moving
elements
-^
Must
copy
array
to
a^
larger
array
when
it^
gets
full
uses a sequence of linked cells ^
we will define a class ListCell fromwhich we build lists
empty
24
87
78 •
tail
head
ListCell
c
=
new
ListCell
Integer(24),null);
Integer
t
=
new
Integer(24);
Integer
s
=
new
Integer(-7);
Integer
e
=
new
Integer(87);
ListCell
p
=
new
ListCell
ListCell
ListCell
null))); c ListCell: p ListCell:
Integer
t
=
new
Integer(24);
Integer
s
=
new
Integer(-7);
Integer
e
=
new
Integer(87);
//Can
also
use
"autoboxing"
ListCell
p
=
new
ListCell
null);
p^
=^
new
ListCell
p);
p^
=^
new
ListCell
p);
p ListCell:
Another way: Note:
p = new ListCell
does
not
create a circular list!
//
Here
is
another
version.
Why
does
this
work?
public
static boolean
search(T
x,
ListCell
c)
{
while(c
!= null) { if
(c.getDatum().equals(x))
return
true;
c^
=^
c.getNext();
} return
false;
}
//
Scan
list
looking
for
x,
return
true
if
found
public
static boolean
search(T
x,
ListCell
c)
{
for
(ListCell
lc
=
c;
lc
!=
null;
lc
=
lc.getNext())
{
if
(lc.getDatum().equals(x))
return
true;
} return
false;
}
-^
Similar
to
recursion
on
integers
-^
Base
case:
empty
list
-^
Recursive
case:
Assume
you
can
solve
problem
on
the
tail,
use
that
in
the
solution
for
the
whole
list
-^
Although
some
are
easier
to
implement
using
iteration
public static boolean search(T x, ListCell c) {
if (c == null) return false;if (c.getDatum().equals(x)) return true;return search(x, c.getNext()); } public static boolean search(T x, ListCell c) {
return c != null &&
(c.getDatum().equals(x) || search(x, c.getNext()));
public boolean search(T x) {
if (datum.equals(x)) return true;if (next == null) return falsereturn next.search(x); } public boolean search(T x) {
return datum.equals(x) ||
(next!= null && next.search(x));
24
‐^7
11
24
‐^7
11
public static ListCell reverse(ListCell c) {
return reverse(c, null); } private static ListCell reverse(ListCell c, ListCell r) {
if (c == null) return r;return reverse(c.getNext(),
new ListCell(c.getDatum(), r));
-^
-^
protected ListCell head;public List(ListCell c) {
head = c; } public ListCell getHead()………public void setHead(ListCell c)……… }
head Heap
List
Reference
to
last
cell
of
list
-^
Number
of
elements
in
list
Search/insertion/
deletion
as instance
methods
Heap
head
List List headtail head
List tailsize