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

13
l¥AD

Upload: others

Post on 25-Mar-2021

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: l¥AD - Simon Fraser Universitymitchell/cmpt-225/2020-Spring/slides/2 - Lists.pdflinkedin.tt# Insert at front: LD-ota.to/D-oeDo¥F Removefrom front: t.tt#I&f1D-oeDo Find it: Traverse

l¥AD

Page 2: l¥AD - Simon Fraser Universitymitchell/cmpt-225/2020-Spring/slides/2 - Lists.pdflinkedin.tt# Insert at front: LD-ota.to/D-oeDo¥F Removefrom front: t.tt#I&f1D-oeDo Find it: Traverse

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

Page 3: l¥AD - Simon Fraser Universitymitchell/cmpt-225/2020-Spring/slides/2 - Lists.pdflinkedin.tt# Insert at front: LD-ota.to/D-oeDo¥F Removefrom front: t.tt#I&f1D-oeDo Find it: Traverse

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

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

- , -]

Linked Lists :

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

Page 4: l¥AD - Simon Fraser Universitymitchell/cmpt-225/2020-Spring/slides/2 - Lists.pdflinkedin.tt# Insert at front: LD-ota.to/D-oeDo¥F Removefrom front: t.tt#I&f1D-oeDo Find it: Traverse

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 .

Page 5: l¥AD - Simon Fraser Universitymitchell/cmpt-225/2020-Spring/slides/2 - Lists.pdflinkedin.tt# Insert at front: LD-ota.to/D-oeDo¥F Removefrom front: t.tt#I&f1D-oeDo Find it: Traverse

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

Page 6: l¥AD - Simon Fraser Universitymitchell/cmpt-225/2020-Spring/slides/2 - Lists.pdflinkedin.tt# Insert at front: LD-ota.to/D-oeDo¥F Removefrom front: t.tt#I&f1D-oeDo Find it: Traverse

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

.

Page 7: l¥AD - Simon Fraser Universitymitchell/cmpt-225/2020-Spring/slides/2 - Lists.pdflinkedin.tt# Insert at front: LD-ota.to/D-oeDo¥F Removefrom front: t.tt#I&f1D-oeDo Find it: Traverse

Operation Array Vectorhihkedhtstgetfirst -1 not n1

add first • n un not

get rest un un not

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

Page 8: l¥AD - Simon Fraser Universitymitchell/cmpt-225/2020-Spring/slides/2 - Lists.pdflinkedin.tt# Insert at front: LD-ota.to/D-oeDo¥F Removefrom front: t.tt#I&f1D-oeDo Find it: Traverse

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.

Page 9: l¥AD - Simon Fraser Universitymitchell/cmpt-225/2020-Spring/slides/2 - Lists.pdflinkedin.tt# Insert at front: LD-ota.to/D-oeDo¥F Removefrom front: t.tt#I&f1D-oeDo Find it: Traverse

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

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

Page 10: l¥AD - Simon Fraser Universitymitchell/cmpt-225/2020-Spring/slides/2 - Lists.pdflinkedin.tt# Insert at front: LD-ota.to/D-oeDo¥F Removefrom front: t.tt#I&f1D-oeDo Find it: Traverse

Operation"

Partidhytiedtrray"

hkked¥A ttoaD→D→l§

+t.EE/idDoJgetitIit nn

find X - nun

insert ati nn an

TF.lt#Jo----±

→*gCITE

-

Page 11: l¥AD - Simon Fraser Universitymitchell/cmpt-225/2020-Spring/slides/2 - Lists.pdflinkedin.tt# Insert at front: LD-ota.to/D-oeDo¥F Removefrom front: t.tt#I&f1D-oeDo Find it: Traverse

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

Page 12: l¥AD - Simon Fraser Universitymitchell/cmpt-225/2020-Spring/slides/2 - Lists.pdflinkedin.tt# Insert at front: LD-ota.to/D-oeDo¥F Removefrom front: t.tt#I&f1D-oeDo Find it: Traverse

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⇒

Page 13: l¥AD - Simon Fraser Universitymitchell/cmpt-225/2020-Spring/slides/2 - Lists.pdflinkedin.tt# Insert at front: LD-ota.to/D-oeDo¥F Removefrom front: t.tt#I&f1D-oeDo Find it: Traverse

F-not