cs 1031 linked lists definition of linked lists examples of linked lists operations on linked lists...

17
CS 103 1 Linked Lists • Definition of Linked Lists • Examples of Linked Lists • Operations on Linked Lists • Linked List as a Class • Linked Lists as Implementations of Stacks, Sets, etc.

Upload: spencer-welch

Post on 03-Jan-2016

215 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: CS 1031 Linked Lists Definition of Linked Lists Examples of Linked Lists Operations on Linked Lists Linked List as a Class Linked Lists as Implementations

CS 103 1

Linked Lists

• Definition of Linked Lists• Examples of Linked Lists• Operations on Linked Lists• Linked List as a Class• Linked Lists as Implementations of Stacks,

Sets, etc.

Page 2: CS 1031 Linked Lists Definition of Linked Lists Examples of Linked Lists Operations on Linked Lists Linked List as a Class Linked Lists as Implementations

CS 103 2

Definition of Linked Lists

• A linked list is a sequence of items (objects) where every item is linked to the next.

• Graphically:

data data data data

head_ptr tail_ptr

Page 3: CS 1031 Linked Lists Definition of Linked Lists Examples of Linked Lists Operations on Linked Lists Linked List as a Class Linked Lists as Implementations

CS 103 3

Definition Details

• Each item has a data part (one or more data members), and a link that points to the next item

• One natural way to implement the link is as a pointer; that is, the link is the address of the next item in the list

• It makes good sense to view each item as an object, that is, as an instance of a class.

• We call that class: Node• The last item does not point to anything. We set

its link member to NULL. This is denoted graphically by a self-loop

Page 4: CS 1031 Linked Lists Definition of Linked Lists Examples of Linked Lists Operations on Linked Lists Linked List as a Class Linked Lists as Implementations

CS 103 4

Examples of Linked Lists(A Waiting Line)

• A waiting line of customers: John, Mary, Dan, Sue (from the head to the tail of the line)

• A linked list of strings can represent this line:

John Mary Dan Sue

head_ptrtail_ptr

Page 5: CS 1031 Linked Lists Definition of Linked Lists Examples of Linked Lists Operations on Linked Lists Linked List as a Class Linked Lists as Implementations

CS 103 5

Examples of Linked Lists(A Stack of Numbers)

• A stack of numbers (from top to bottom): 10, 8, 6, 8, 2

• A linked list of ints can represent this stack:

10 8 6 2

head_ptr tail_ptr

8

Page 6: CS 1031 Linked Lists Definition of Linked Lists Examples of Linked Lists Operations on Linked Lists Linked List as a Class Linked Lists as Implementations

CS 103 6

Examples of Linked Lists(A Set of Non-redundant Elements)

• A set of characters: a, b, d, f, c• A linked list of chars can represent this set:

a b d c

head_ptr tail_ptr

f

Page 7: CS 1031 Linked Lists Definition of Linked Lists Examples of Linked Lists Operations on Linked Lists Linked List as a Class Linked Lists as Implementations

CS 103 7

Examples of Linked Lists(A Sorted Set of Non-redundant Elements)

• A set of characters: a, b, d, f, c• The elements must be arranged in sorted

order: a, b, c, d, f• A linked list of chars can represent this set:

a b c f

head_ptr tail_ptr

d

Page 8: CS 1031 Linked Lists Definition of Linked Lists Examples of Linked Lists Operations on Linked Lists Linked List as a Class Linked Lists as Implementations

CS 103 8

Examples of Linked Lists(A Polynomial)

• A polynomial of degree n is the function Pn(x)=a0+a1x+a2x2+…+anxn. The ai’s are called the coefficients of the polynomial

• The polynomial can be represented by a linked list (2 data members and a link per item):

a0,0 a1,1 a2,2 an,n

head_ptr tail_ptr

Page 9: CS 1031 Linked Lists Definition of Linked Lists Examples of Linked Lists Operations on Linked Lists Linked List as a Class Linked Lists as Implementations

CS 103 9

Operations on Linked Lists• Insert a new item– At the head of the list, or– At the tail of the list, or– Inside the list, in some designated position

• Search for an item in the list– The item can be specified by position, or by some value

• Delete an item from the list– Search for and locate the item, then remove the item,

and finally adjust the surrounding pointers• size( );• isEmpty( )

Page 10: CS 1031 Linked Lists Definition of Linked Lists Examples of Linked Lists Operations on Linked Lists Linked List as a Class Linked Lists as Implementations

CS 103 10

Insert– At the Head• Insert a new data A. Call new: newPtr List before insertion:

• After insertion to head:

data data data data

head_ptr tail_ptr

A

data data data data

head_ptrtail_ptr

A

•The link value in the new item = old head_ptr•The new value of head_ptr = newPtr

Page 11: CS 1031 Linked Lists Definition of Linked Lists Examples of Linked Lists Operations on Linked Lists Linked List as a Class Linked Lists as Implementations

CS 103 11

Insert – at the Tail• Insert a new data A. Call new: newPtr

List before insertion

• After insertion to tail:

data data data data

head_ptr tail_ptr

A

data data data data

head_ptr tail_ptr

A

•The link value in the new item = NULL•The link value of the old last item = newPtr

Page 12: CS 1031 Linked Lists Definition of Linked Lists Examples of Linked Lists Operations on Linked Lists Linked List as a Class Linked Lists as Implementations

CS 103 12

Insert – inside the List• Insert a new data A. Call new: newPtr

List before insertion:

• After insertion in 3rd position:

data data data data

head_ptr tail_ptr

data

data A data data

head_ptrtail_ptr

data

•The link-value in the new item = link-value of 2nd item•The new link-value of 2nd item = newPtr

Page 13: CS 1031 Linked Lists Definition of Linked Lists Examples of Linked Lists Operations on Linked Lists Linked List as a Class Linked Lists as Implementations

CS 103 13

Delete – the Head Item• List before deletion:

• List after deletion of the head item:

data data data data

head_ptr tail_ptr

data data data data

head_ptr tail_ptr

data

•The new value of head_ptr = link-value of the old head item•The old head item is deleted and its memory returned

data

Page 14: CS 1031 Linked Lists Definition of Linked Lists Examples of Linked Lists Operations on Linked Lists Linked List as a Class Linked Lists as Implementations

CS 103 14

Delete – the Tail Item• List before deletion:

• List after deletion of the tail item:

data data data data

head_ptr tail_ptr

data data data

head_ptrtail_ptr

•New value of tail_ptr = link-value of the 3rd from last item•New link-value of new last item = NULL.

data

datadata

Page 15: CS 1031 Linked Lists Definition of Linked Lists Examples of Linked Lists Operations on Linked Lists Linked List as a Class Linked Lists as Implementations

CS 103 15

Delete – an inside Item• List before deletion:

• List after deletion of the 2nd item:

data data data data

head_ptr tail_ptr

data data

head_ptr tail_ptr

•New link-value of the item located before the deleted one = the link-value of the deleted item

data

data datadata

Page 16: CS 1031 Linked Lists Definition of Linked Lists Examples of Linked Lists Operations on Linked Lists Linked List as a Class Linked Lists as Implementations

CS 103 16

size() and isEmpty()

• We need to scan the items in the list from the head_ptr to the last item marked by its link-value being NULL

• Count the number of items in the scan, and return the count. This is the size().

• Alternatively, keep a counter of the number of item, which gets updated after each insert/delete. The function size( ) returns that counter

• If head_ptr is NULL, isEmpty() returns true; else, it returns false.

Page 17: CS 1031 Linked Lists Definition of Linked Lists Examples of Linked Lists Operations on Linked Lists Linked List as a Class Linked Lists as Implementations

CS 103 17

Searching for an Item

• Suppose you want to find the item whose data value is A

• You have to search sequentially starting from the head item rightward until the first item whose data member is equal to A is found.

• At each item searched, a comparison between the data member and A is performed.