l¥ad - simon fraser universitymitchell/cmpt-225/2020-spring/slides/2 - lists.pdflinkedin.tt# insert...

Post on 25-Mar-2021

3 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

l¥AD

III.I = { do , hi , lz, . . .

En-i )I is of length n .

Operations : get first elementadd to frontget

" rest" of elements tremor firstget ithget next element ( from a

"current"

insert at location i.one)

delete at location i.find itappend two lists

histhtmplemewtationsh-rra.gs: A= [1 , 213 , 4 , . . .µ

✓= [1,2 , 3) . . . h, - , - ,

- , -]

Linked Lists :

↳ t.fi#t*D-olsTta---tntI

A-rraysareaspecialkindoffonctiowlonsidei.tt-d array E of site n and types

mathematically :a is a function a:[n]→

S-

-

p

Implementation :lit 90,4 . . . a-is

N 1 is a fixed size contiguoussequence of memory locations .

Dynamic Data structures-

- Change size (memory used) as amountof data we are storing increases/ decreases.

- We construct them from collectionsof fixed - sized objects(with no names)linked together by references/pointers/. . .

- Simplest : Linked List:

LE1-4IH

linkedin.tt#Insert at front :

LD-ota.to/D-oeDo¥F

Remove from front :

t.tt#I&f1D-oeDoFind it :

Traverse the list, starting at

the front,

I check each node for x.

L D-HD1-4DH

et

.

Operation Array Vectorhihkedhtstgetfirst -1 not n1

add first • n un not

get rest un un not

T.IE#*....tT④¥④%D→"

traversing a L.tn .

-

C# L

while( e is not o ){4 do what you need to at

4 the node pointed to by C

C ← c. next

}where : Hodes are ①

p tdata next.

insertatithpositiojplxDYD-ob-otg-st.tt#Dinheremove the ith element : It"- +

D-ott-otg-TD-xsb-D.esDL5W

Operation"

Partidhytiedtrray"

hkked¥A ttoaD→D→l§

+t.EE/idDoJgetitIit nn

find X - nun

insert ati nn an

TF.lt#Jo----±

→*gCITE

-

Operationsonhnhipartiallyrfilledarrayttrrayheh .

get first 04) ok)

add to front an 04)-

add to back 04) an

get rest an od)

get ith Oli) an

find X an an

insert uxati anan

applied

linkedl time proportional to length of first list .→→→→

t.to?-D-oID-X3J an

D→*D→ttoD

any: time proportional to total lengthan

÷¥s⇒

F-not

top related