Linked Structures Songs, Games, Movies II Fall 2013 Carola Wenk - - PowerPoint PPT Presentation

linked structures songs games movies ii
SMART_READER_LITE
LIVE PREVIEW

Linked Structures Songs, Games, Movies II Fall 2013 Carola Wenk - - PowerPoint PPT Presentation

Linked Structures Songs, Games, Movies II Fall 2013 Carola Wenk Linked Lists x: y: hello world! The simplest dynamic structures is a linear ordering of data, called a linked list. We saw that is was easy to modify a


slide-1
SLIDE 1

Linked Structures Songs, Games, Movies II

Fall 2013 Carola Wenk

slide-2
SLIDE 2

Linked Lists

x: y: “hello” “world!”

We saw that is was easy to modify a linked structure without “touching” all of the data. The simplest dynamic structures is a linear ordering of data, called a “linked list”. How do we implement additions and deletions? Is finding an item efficient?

slide-3
SLIDE 3

Adding Items

L:

. . .

How do we add to the beginning of the list? Actually, what does it mean to add to a linked list?

slide-4
SLIDE 4

Adding Items

. . .

How do we add to the beginning of the list? L: x: Actually, what does it mean to add to a linked list?

slide-5
SLIDE 5

Adding Items

. . .

How do we add to the beginning of the list? L: x: Actually, what does it mean to add to a linked list?

slide-6
SLIDE 6

Adding Items

. . .

How do we add to the beginning of the list? L,x: Adding to the front of a list is easy and requires constant work. Actually, what does it mean to add to a linked list?

slide-7
SLIDE 7

Adding Items

. . .

How do we add to the end of the list? L: x: p: Actually, what does it mean to add to a linked list?

slide-8
SLIDE 8

Adding Items

. . .

How do we add to the end of the list? L: p: x: Actually, what does it mean to add to a linked list?

slide-9
SLIDE 9

Adding Items

. . .

How do we add to the end of the list? L: p: x: Actually, what does it mean to add to a linked list?

slide-10
SLIDE 10

Adding Items

. . .

How do we add to the end of the list? L: p: x: Actually, what does it mean to add to a linked list?

slide-11
SLIDE 11

Adding Items

  • Actually, what does it mean to add to a linked list?

. . .

How do we add to the end of the list? L: p: x: Sadly, we must traverse the entire structure just to add to the end!

slide-12
SLIDE 12

Adding Items

  • Actually, what does it mean to add to a linked list?

. . .

How do we add to the end of the list? L_head: L_tail: x: If we kept track of the end of the list, then we could add an item with just a few operations.

slide-13
SLIDE 13

Adding Items

  • Actually, what does it mean to add to a linked list?

. . .

How do we add to the end of the list? L_head: L_tail: x: But, if we wanted to insert anywhere else, we’re still out of luck.

slide-14
SLIDE 14

Finding Items

  • To find anything in a linked list, we again need to

(potentially) traverse the entire structure.

. . .

L: Finding an element is no better than in an array; it takes time that is linear in the size of the list. The implementation is easy, but in the worst case it is no better than a linear search, regardless of whether the linked list is ordered or not!

slide-15
SLIDE 15

Removing Items

. . .

L:

  • We must also keep track of the preceding element, so

that we can reassign its neighbor. The implementation is easy, but in the worst case it is no better than a linear search, regardless of whether the linked list is ordered or not! p: x

slide-16
SLIDE 16

Removing Items

. . .

L:

  • We must also keep track of the preceding element, so

that we can reassign its neighbor. The implementation is easy, but in the worst case it is no better than a linear search, regardless of whether the linked list is ordered or not! p: x

slide-17
SLIDE 17

Removing Items

. . .

L:

  • We must also keep track of the preceding element, so

that we can reassign its neighbor. The implementation is easy, but in the worst case it is no better than a linear search, regardless of whether the linked list is ordered or not. p: x