Lists and Trees - Object-Oriented Programming and Data Structures - Lecture Sl, Lecture notes of Object Oriented Programming

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

2012/2013

Uploaded on 08/20/2013

yumni
yumni 🇮🇳

5

(2)

25 documents

1 / 27

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Lists&Trees
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b

Partial preview of the text

Download Lists and Trees - Object-Oriented Programming and Data Structures - Lecture Sl and more Lecture notes Object Oriented Programming in PDF only on Docsity!

Lists

Trees

List

Overview

•^

Purpose^ –

Maintain

an

ordered

collection

of elements

(with

possible

duplication)

•^

Common

operations

-^

Create

a^ list

-^

Access

elements

of a^ list

sequentially

-^

Insert

elements

into

a^ list

-^

Delete

elements

from

a^ list

•^

Arrays^ –

Random

access

-^

Fixed

size:

cannot

grow

or shrink

after

creation

(Sometimes

simulated

using

copying)

•^

Linked

Lists

-^

No

random

access

(Sometimes

random

‐access

is^ “simulated”

but

cost

is^ linear)

-^

Can

grow

and

shrink

dynamically

List

Data

Structures

•^

Array^ –

Must

specify

array

size

at

creation

-^

Insert,

delete

require

moving

elements

-^

Must

copy

array

to

a^

larger

array

when

it^

gets

full

^

Linked list

^

uses a sequence of linked cells ^

we will define a class ListCell fromwhich we build lists

empty

24

87

78 •

List

Terminology

•^

Head

first

element

of

the

list

•^

Tail

rest

of

the

list

tail

head

Ways

of

building

a

Linked

List

ListCell

c

=

new

ListCell(new

Integer(24),null);

Integer

t

=

new

Integer(24);

Integer

s

=

new

Integer(-7);

Integer

e

=

new

Integer(87);

ListCell

p

=

new

ListCell(t,new

ListCell(s,new

ListCell(e,

null))); c ListCell: p ListCell:

Building

a

Linked

List

(cont’d)

Integer

t

=

new

Integer(24);

Integer

s

=

new

Integer(-7);

Integer

e

=

new

Integer(87);

//Can

also

use

"autoboxing"

ListCell

p

=

new

ListCell(e,

null);

p^

=^

new

ListCell(s,

p);

p^

=^

new

ListCell(t,

p);

p ListCell:

Another way: Note:

p = new ListCell(s,p);

does

not

create a circular list!

Access

Example:

Linear

Search

//

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;

}

Recursion

on

Lists

•^

Recursion

can

be

done

on

lists

-^

Similar

to

recursion

on

integers

•^

Almost

always

-^

Base

case:

empty

list

-^

Recursive

case:

Assume

you

can

solve

problem

on

the

tail,

use

that

in

the

solution

for

the

whole

list

•^

Many

list

operations

can

be

implemented

very

simply

by

using

this

idea

-^

Although

some

are

easier

to

implement

using

iteration

Recursive

Search:

Static

method

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()));

Recursive

Search:

Instance

method

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));

Reversing

a

list:

Animation

•^

Approach:

One

by

one,

remove

the

first

element

of

the

given

list

and

make

it

the

first

element

of

“rev”

•^

By

the

time

we

are

done,

the

last

element

from

the

given

list

will

be

the

first

element

of

the

finished

“rev”

24

^7

11

24

^7

11

Recursive

Reverse

•^

Exercise:

Turn

this

into

an

instance

method

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));

List

with

Header

-^

Sometimes

it

is

preferable

to

have

a^

List

class

distinct

from

the

ListCell

class

-^

The

List

object

is

like

a^

head

element

that

always

exists

even

if^

list

itself

is

empty^ class List {

protected ListCell head;public List(ListCell c) {

head = c; } public ListCell getHead()………public void setHead(ListCell c)……… }

head Heap

List

Variations

on

List

with

Header

•^

Header

can

also

keep

other

info

–^

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