ECE 2574: Data Structures and Algorithms - Linked-Based - - PowerPoint PPT Presentation

ece 2574 data structures and algorithms linked based
SMART_READER_LITE
LIVE PREVIEW

ECE 2574: Data Structures and Algorithms - Linked-Based - - PowerPoint PPT Presentation

ECE 2574: Data Structures and Algorithms - Linked-Based Implementations C. L. Wyatt Fall 2017 Today we will look at an alternative to using arrays (statically or dynamically allocated) as a data structure. Warmup Nodes Linking nodes


slide-1
SLIDE 1

ECE 2574: Data Structures and Algorithms - Linked-Based Implementations

  • C. L. Wyatt

Fall 2017

slide-2
SLIDE 2

Today we will look at an alternative to using arrays (statically or dynamically allocated) as a data structure.

◮ Warmup ◮ Nodes ◮ Linking nodes into chains ◮ Operations ◮ Implementing the ADT Bag with a Linked-List

slide-3
SLIDE 3

Warmup #1

Suppose you had a list of integers represented as an array. int a[5]; = {12,42,56,90}; a[0] = 12; a[1] = 42; a[2] = 56; a[3] = 90; and you want to insert the value 15 in the list so that the values remained in order. Would you:

◮ replace 12 by 15 ◮ replace 42 by 15 ◮ shift 42,56, and 90 over, then insert 15 at index 1 (93%

correct)

◮ insert 15 at index 1

slide-4
SLIDE 4

Warmup #2

Considering your answer to the warmup question 1, insertion at what location requires the least operations?

◮ at beginning ◮ at the end (80% correct) ◮ in the middle ◮ there is no preference

slide-5
SLIDE 5

Warmup #3

Considering your answer to the warmup question 1, insertion at what location requires the most operations?

◮ at beginning (73% correct) ◮ at the end ◮ in the middle ◮ there is no preference

slide-6
SLIDE 6

A partial Ordered List ADT

List with operations:

◮ insert(size_t index, TYPE value), expanding the list ◮ remove(size_t index), remove value from list, compress the list

slide-7
SLIDE 7

Insertion cases using arrays

What happens on the fjrst insertion? (size = 0)

? ? ? ? ? ? ? ?

What happens if the insertion is always at the end?

1 2 3 4 5 ? ? ?

What happens if the insertion is always at the beginning?

5 4 3 2 1 ? ? ?

slide-8
SLIDE 8

Deletion cases using arrays

What happens on the last deletion?

X ? ? ? ? ? ? ?

What happens if the deletion is always at the end?

1 2 3 4 X ? ? ?

What happens if the deletion is always at the beginning?

X 4 3 2 1 ? ? ?

slide-9
SLIDE 9

The implementation of the ADT Bag and Ordered List using arrays can be replaced with another data structure.

A linked list is a data structure that has several advantages over the array data structure:

  • 1. It can grow and shrink as needed without wasting space
  • 2. Insertions and deletions can be done with no copying

The disadvantage compared to an array-based implementation is the retrieve operations are no longer constant in the general case. It is constant to retrieve the beginning and end item though.

slide-10
SLIDE 10

The linked list data structure is a set of nodes linked by pointers.

slide-11
SLIDE 11

There are three main variations on the linked list.

slide-12
SLIDE 12

Overview of the insert operation

slide-13
SLIDE 13

Overview of the insert operation

slide-14
SLIDE 14

Overview of the insert operation

slide-15
SLIDE 15

Overview of the insert operation

Notice only the amount of memory needed to hold the item was allocated and no copying was required.

slide-16
SLIDE 16

Overview of the delete operation

slide-17
SLIDE 17

Overview of the delete operation

slide-18
SLIDE 18

Overview of the delete operation

Again, notice, no copying was required

slide-19
SLIDE 19

Implementing the single linked list

Need to represent nodes inside the class. Example: (see in-class code, no exercise today)

◮ Defjne a single-linked Bag template class using a structure to

represent the nodes.

◮ Implement the basic operations.

slide-20
SLIDE 20

Next Actions and Reminders

◮ Read CH pp. 150-154 ◮ No warmup for next time. ◮ P1 is due 9/27