Computer Science II for Majors Lecture 12 Linked Lists Dr. - - PowerPoint PPT Presentation

computer science ii for majors
SMART_READER_LITE
LIVE PREVIEW

Computer Science II for Majors Lecture 12 Linked Lists Dr. - - PowerPoint PPT Presentation

CMSC202 Computer Science II for Majors Lecture 12 Linked Lists Dr. Katherine Gibson www.umbc.edu Last Class We Covered Inheritance Object relationships is-a (Inheritance) has-a (Composition and Aggregation) 2 www.umbc.edu


slide-1
SLIDE 1

www.umbc.edu

CMSC202 Computer Science II for Majors

Lecture 12 –

Linked Lists

  • Dr. Katherine Gibson
slide-2
SLIDE 2

www.umbc.edu

Last Class We Covered

  • Inheritance
  • Object relationships

– is-a (Inheritance) – has-a (Composition and Aggregation)

2

slide-3
SLIDE 3

www.umbc.edu

Any Questions from Last Time?

slide-4
SLIDE 4

www.umbc.edu

Today’s Objectives

  • To cover linked lists in detail

– Traversal – Creation – Insertion – Deletion

4

slide-5
SLIDE 5

www.umbc.edu

Linked Lists vs Vectors

slide-6
SLIDE 6

www.umbc.edu

What is a Linked List?

  • Data structure

– Dynamic – Allow easy insertion and deletion

  • Uses nodes that contain

– Data – Pointer to next node in the list

6

slide-7
SLIDE 7

www.umbc.edu

Example Linked List

7

data link head data link data link data link

NULL In these diagrams, a doubly-

  • utlined box indicates a pointer.

tail

slide-8
SLIDE 8

www.umbc.edu

Why Use Linked Lists?

  • We already have vectors!
  • What are some disadvantages of an vectors?

– Inserting in the middle of an array takes time – Deletion as well – Sorting – Requires a contiguous block of memory

8

slide-9
SLIDE 9

www.umbc.edu

Representation in Memory

9

Array location in memory NULL First node of Linked List Each cell is a block of memory

slide-10
SLIDE 10

www.umbc.edu

(Dis)Advantages of Linked Lists

  • Advantages:

– Change size easily and constantly – Insertion and deletion can easily happen anywhere in the Linked List – Only one node needs to be contiguously stored

  • Disadvantages:

– Can’t access by index value – Requires management of memory – Pointer to next node takes up more memory

10

slide-11
SLIDE 11

www.umbc.edu

Nodes

slide-12
SLIDE 12

www.umbc.edu

Nodes

  • A node is one element of a Linked List
  • Nodes consist of two main parts:

– Data stored in the node – Pointer to next node in list

  • Often represented as classes

12

data link

slide-13
SLIDE 13

www.umbc.edu

Code for Node Class

class Node { String name; int testGrade; Node *link; // constructor // accessors // mutators };

13

link testGrade name

link can point to other nodes two options:

  • 1. another Node
  • 2. NULL

NULL

slide-14
SLIDE 14

www.umbc.edu

Linked List Overview

slide-15
SLIDE 15

www.umbc.edu

15

slide-16
SLIDE 16

www.umbc.edu

Example Linked List

16

NULL

link DUMMY DUMMY link testGrade name link testGrade name link testGrade name link

m_head

slide-17
SLIDE 17

www.umbc.edu

Important Points to Remember

  • Last node in the Linked List points to NULL
  • Each node points to either another node in

the Linked List, or to NULL

– Only one link per node

17

slide-18
SLIDE 18

www.umbc.edu

Managing Memory with LLs

  • Hard part of using Linked Lists is ensuring that

none of the nodes go “missing”

  • Think of Linked List as a train

– (Or as a conga line of Kindergarteners)

  • Must keep track of where links point to
  • If you’re not careful, nodes can get lost in

memory (and you have no way to find them)

18

slide-19
SLIDE 19

www.umbc.edu

Linked List Functions

  • What functions does a Linked List

class implementation require?

  • Linked_List constructor
  • insert()
  • remove()
  • printList()
  • isEmpty()

19

slide-20
SLIDE 20

www.umbc.edu

Linked Lists’ “Special” Cases

  • Linked Lists often need to be handled

differently under specific circumstances

– Linked List is empty – Linked List has only one element – Linked List has multiple elements – Changing something with the first or last node

  • Keep this in mind when you are coding

– Dummy nodes alleviate some of these concerns

20

slide-21
SLIDE 21

www.umbc.edu

Creating a Linked List

21

slide-22
SLIDE 22

www.umbc.edu

Traversing the List

  • To control our traversal, we’ll use a loop

– Initialization, Termination Condition, Modification

  • 1. Set CURR to the first node in the list
  • 2. Continue until we hit the end of the list (NULL)
  • 3. Move from one node to another

(using m_next)

slide-23
SLIDE 23

www.umbc.edu

link DUMMY DUMMY

Demonstration of Traversal

FRONT CURR

NULL

link 91 Bob link 94 Eve

NULL

for (CURR = FRONT; CURR != NULL; CURR = CURR->link) {

slide-24
SLIDE 24

www.umbc.edu

Demonstration of Traversal

link DUMMY DUMMY FRONT link 91 Bob link 94 Eve

NULL

CURR

for (CURR = FRONT; CURR != NULL; CURR = CURR->link) {

slide-25
SLIDE 25

www.umbc.edu

link DUMMY DUMMY

Demonstration of Traversal

FRONT link 91 Bob link 94 Eve

NULL

CURR

for (CURR = FRONT; CURR != NULL; CURR = CURR->link) { // ignore, dummy node

slide-26
SLIDE 26

www.umbc.edu

link DUMMY DUMMY

Demonstration of Traversal

FRONT link 91 Bob link 94 Eve

NULL

CURR

for (CURR = FRONT; CURR != NULL; CURR = CURR->link) {

slide-27
SLIDE 27

www.umbc.edu

link DUMMY DUMMY

Demonstration of Traversal

FRONT link 91 Bob link 94 Eve

NULL

CURR

for (CURR = FRONT; CURR != NULL; CURR = CURR->link) {

slide-28
SLIDE 28

www.umbc.edu

link DUMMY DUMMY

Demonstration of Traversal

FRONT link 91 Bob link 94 Eve

NULL

CURR

for (CURR = FRONT; CURR != NULL; CURR = CURR->link) { // print information (Bob)

slide-29
SLIDE 29

www.umbc.edu

link DUMMY DUMMY

Demonstration of Traversal

FRONT link 91 Bob link 94 Eve

NULL

CURR

for (CURR = FRONT; CURR != NULL; CURR = CURR->link) {

slide-30
SLIDE 30

www.umbc.edu

link DUMMY DUMMY

Demonstration of Traversal

FRONT link 91 Bob link 94 Eve

NULL

CURR

for (CURR = FRONT; CURR != NULL; CURR = CURR->link) {

slide-31
SLIDE 31

www.umbc.edu

link DUMMY DUMMY

Demonstration of Traversal

FRONT link 91 Bob link 94 Eve

NULL

CURR

for (CURR = FRONT; CURR != NULL; CURR = CURR->link) { // print information (Eve)

slide-32
SLIDE 32

www.umbc.edu

link DUMMY DUMMY

Demonstration of Traversal

FRONT link 91 Bob link 94 Eve

NULL

CURR

for (CURR = FRONT; CURR != NULL; CURR = CURR->link) {

slide-33
SLIDE 33

www.umbc.edu

link DUMMY DUMMY

Demonstration of Traversal

FRONT link 91 Bob link 94 Eve

NULL

CURR

for (CURR = FRONT; CURR != NULL; CURR = CURR->link) {

NULL

slide-34
SLIDE 34

www.umbc.edu

link DUMMY DUMMY

Demonstration of Traversal

FRONT link 91 Bob link 94 Eve

NULL

CURR

for (CURR = FRONT; CURR != NULL; CURR = CURR->link) {

NULL

} // exit the loop

slide-35
SLIDE 35

www.umbc.edu

Insertion and Deletion

35

slide-36
SLIDE 36

www.umbc.edu

Announcements

  • Project 3 is out – get started now!

–It is due Thursday, March 31st

36