1 chapter 16 linked structures dale/weems/headington

61
1 Chapter 16 Linked Structures Dale/Weems/Headington

Upload: andrew-cross

Post on 05-Jan-2016

227 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Chapter 16 Linked Structures Dale/Weems/Headington

1

Chapter 16

Linked Structures

Dale/Weems/Headington

Page 2: 1 Chapter 16 Linked Structures Dale/Weems/Headington

2

Chapter 16 Topics

Meaning of a Linked List Meaning of a Dynamic Linked List Traversal, Insertion and Deletion of Elements

in a Dynamic Linked List Specification of a Dynamic Linked Sorted List Insertion and Deletion of Elements in a

Dynamic Linked Sorted List

Page 3: 1 Chapter 16 Linked Structures Dale/Weems/Headington

3

What is a List?

A list is a varying-length, linear collection of homogeneous elements.

linear means each list element (except the first) has a unique predecessor, and each element (except the last) has a unique successor

Page 4: 1 Chapter 16 Linked Structures Dale/Weems/Headington

4

To implement the List ADT

The programmer must

1) choose a concrete data representation for the list, and

2) implement the list operations

Page 5: 1 Chapter 16 Linked Structures Dale/Weems/Headington

Array-Based Sorted List Operations

Transformers Insert Delete

Observers IsEmpty IsFull Length IsPresent Print

change state

observe state

5

Page 6: 1 Chapter 16 Linked Structures Dale/Weems/Headington

6

Array-based class SortedList

IsFull

Length

IsPresent

Delete

IsEmpty

Insert

Print

Private data:

length

data [ 0 ] [ 1 ] [ 2 ]

[MAX_LENGTH-1]

SortedList

Page 7: 1 Chapter 16 Linked Structures Dale/Weems/Headington

7

// SPECIFICATION FILE ARRAY-BASED SORTED LIST ( slist.h )const int MAX_LENGTH = 50 ;typedef int ItemType ;

class SortedList{public : // public member functions

SortedList ( ) ; // constructorbool IsEmpty ( ) const ;bool IsFull ( ) const ; int Length ( ) const ; // returns length of list void Insert ( ItemType item ) ; void Delete ( ItemType item ) ; bool IsPresent( ItemType item ) const ;void Print ( ) ;

private : // private data members

int length ; // number of values currently storedItemType data[MAX_LENGTH] ; void BinSearch ( ItemType item, bool& found, int& position ) const ;

} ; 7

Page 8: 1 Chapter 16 Linked Structures Dale/Weems/Headington

8

How to Implement a List

use a built-in array stored in contiguous memory locations, implementing operations Insert and Delete by moving list items around in the array, as needed

use a linked list (to avoid excessive data movement from insertions and deletions) not necessarily stored in contiguous memory locations

Page 9: 1 Chapter 16 Linked Structures Dale/Weems/Headington

9

Implementation Possibilities for a List ADT

List

Linked listBuilt-in array

Built-in dynamic data and pointers

Built-in arrayof structs

Page 10: 1 Chapter 16 Linked Structures Dale/Weems/Headington

10

A Linked List

a linked list is a list in which the order of the components is determined by an explicit link member in each node

the nodes are structs--each node contains a component member and also a link member that gives the location of the next node in the list

head ‘X’ ‘C’ ‘L’

Page 11: 1 Chapter 16 Linked Structures Dale/Weems/Headington

11

Dynamic Linked List

head “Ted” “Irv” “Lee”

in a dynamic linked list, nodes are linked together by pointers, and an external pointer (or head pointer) points to the first node in the list

Page 12: 1 Chapter 16 Linked Structures Dale/Weems/Headington

12

Nodes can be located anywhere in memory

the link member holds the memory address of the next node in the list

head 3000 “Ted” 5000 “Irv” 2000 “Lee” NULL

3000 5000 2000

Page 13: 1 Chapter 16 Linked Structures Dale/Weems/Headington

13

// Type DECLARATIONS

struct NodeType {

char info;

NodeType* link;

}

typedef NodeType* NodePtr;

// Variable DECLARATIONS

NodePtr head;

NodePtr ptr;

13

Declarations for a Dynamic Linked List

. info . link

‘A’ 6000

Page 14: 1 Chapter 16 Linked Structures Dale/Weems/Headington

1414

Pointer Dereferencing and Member Selection

. info . link

‘A’ 6000 ptr

ptr

ptr

. info . link

‘A’ 6000

*ptr

ptr

. info . link

(*ptr).info

ptr->info

‘A’ 6000

Page 15: 1 Chapter 16 Linked Structures Dale/Weems/Headington

1515

ptr is a pointer to a node

. info . link

‘A’ 6000 ptr

ptr

Page 16: 1 Chapter 16 Linked Structures Dale/Weems/Headington

1616

*ptr is the entire node pointed to by ptr

ptr

. info . link

‘A’ 6000

*ptr

Page 17: 1 Chapter 16 Linked Structures Dale/Weems/Headington

1717

ptr->info is a node member

ptr

. info . link

ptr->info

(*ptr).info // equivalent

‘A’ 6000

Page 18: 1 Chapter 16 Linked Structures Dale/Weems/Headington

1818

ptr->link is a node member

ptr

. info . link

ptr->link

(*ptr).link // equivalent

‘A’ 6000

Page 19: 1 Chapter 16 Linked Structures Dale/Weems/Headington

19

Traversing a Dynamic Linked List

//PRE: head points to a dynamic linked list

ptr = head ;

while (ptr != NULL) {

cout << ptr->info ;

// Or, do something else with node *ptr

ptr = ptr->link ;

}

ptr

3000 “Ted” 5000 “Irv” 2000 “Lee” NULL

3000 5000 2000

head

Page 20: 1 Chapter 16 Linked Structures Dale/Weems/Headington

20

//PRE: head points to a dynamic linked list

ptr = head ;

while (ptr != NULL) {

cout << ptr->info ;

// Or, do something else with node *ptr

ptr = ptr->link ;

}

ptr 3000

3000 “Ted” 5000 “Irv” 2000 “Lee” NULL

3000 5000 2000

head

Traversing a Dynamic Linked List

Page 21: 1 Chapter 16 Linked Structures Dale/Weems/Headington

21

//PRE: head points to a dynamic linked list

ptr = head ;

while (ptr != NULL) {

cout << ptr->info ;

// Or, do something else with node *ptr

ptr = ptr->link ;

}

ptr 3000

3000 “Ted” 5000 “Irv” 2000 “Lee” NULL

3000 5000 2000

head

Traversing a Dynamic Linked List

Page 22: 1 Chapter 16 Linked Structures Dale/Weems/Headington

22

//PRE: head points to a dynamic linked list

ptr = head ;

while (ptr != NULL) {

cout << ptr->info ;

// Or, do something else with node *ptr

ptr = ptr->link ;

}

ptr 3000

3000 “Ted” 5000 “Irv” 2000 “Lee” NULL

3000 5000 2000

head

Traversing a Dynamic Linked List

Page 23: 1 Chapter 16 Linked Structures Dale/Weems/Headington

23

//PRE: head points to a dynamic linked list

ptr = head ;

while (ptr != NULL) {

cout << ptr->info ;

// Or, do something else with node *ptr

ptr = ptr->link ;

}

ptr 5000

3000 “Ted” 5000 “Irv” 2000 “Lee” NULL

3000 5000 2000

head

Traversing a Dynamic Linked List

Page 24: 1 Chapter 16 Linked Structures Dale/Weems/Headington

24

//PRE: head points to a dynamic linked list

ptr = head ;

while (ptr != NULL) {

cout << ptr->info ;

// Or, do something else with node *ptr

ptr = ptr->link ;

}

ptr 5000

3000 “Ted” 5000 “Irv” 2000 “Lee” NULL

3000 5000 2000

head

Traversing a Dynamic Linked List

Page 25: 1 Chapter 16 Linked Structures Dale/Weems/Headington

25

//PRE: head points to a dynamic linked list

ptr = head ;

while (ptr != NULL) {

cout << ptr->info ;

// Or, do something else with node *ptr

ptr = ptr->link ;

}

ptr 5000

3000 “Ted” 5000 “Irv” 2000 “Lee” NULL

3000 5000 2000

head

Traversing a Dynamic Linked List

Page 26: 1 Chapter 16 Linked Structures Dale/Weems/Headington

26

//PRE: head points to a dynamic linked list

ptr = head ;

while (ptr != NULL) {

cout << ptr->info ;

// Or, do something else with node *ptr

ptr = ptr->link ;

}

ptr 2000

3000 “Ted” 5000 “Irv” 2000 “Lee” NULL

3000 5000 2000

head

Traversing a Dynamic Linked List

Page 27: 1 Chapter 16 Linked Structures Dale/Weems/Headington

27

//PRE: head points to a dynamic linked list

ptr = head ;

while (ptr != NULL) {

cout << ptr->info ;

// Or, do something else with node *ptr

ptr = ptr->link ;

}

ptr 2000

3000 “Ted” 5000 “Irv” 2000 “Lee” NULL

3000 5000 2000

head

Traversing a Dynamic Linked List

Page 28: 1 Chapter 16 Linked Structures Dale/Weems/Headington

28

//PRE: head points to a dynamic linked list

ptr = head ;

while (ptr != NULL) {

cout << ptr->info ;

// Or, do something else with node *ptr

ptr = ptr->link ;

}

ptr 2000

3000 “Ted” 5000 “Irv” 2000 “Lee” NULL

3000 5000 2000

head

Traversing a Dynamic Linked List

Page 29: 1 Chapter 16 Linked Structures Dale/Weems/Headington

29

//PRE: head points to a dynamic linked list

ptr = head ;

while (ptr != NULL) {

cout << ptr->info ;

// Or, do something else with node *ptr

ptr = ptr->link ;

}

ptr NULL

3000 “Ted” 5000 “Irv” 2000 “Lee” NULL

3000 5000 2000

head

Traversing a Dynamic Linked List

Page 30: 1 Chapter 16 Linked Structures Dale/Weems/Headington

30

//PRE: head points to a dynamic linked list

ptr = head ;

while (ptr != NULL) {

cout << ptr->info ;

// Or, do something else with node *ptr

ptr = ptr->link ;

}

ptr NULL

3000 “Ted” 5000 “Irv” 2000 “Lee” NULL

3000 5000 2000

head

Traversing a Dynamic Linked List

Page 31: 1 Chapter 16 Linked Structures Dale/Weems/Headington

Using Operator new

If memory is available in an area called the free store (or heap), operator new allocates the requested object, and returns a pointer to the memory allocated.

The dynamically allocated object exists until the delete operator destroys it.

31

Page 32: 1 Chapter 16 Linked Structures Dale/Weems/Headington

32

Inserting a Node at the Front of a List

char item = ‘B’;

NodePtr location;

location = new NodeType;

location->info = item;

location->link = head;

head = location;

head ‘X’ ‘C’ ‘L’

‘B’item

Page 33: 1 Chapter 16 Linked Structures Dale/Weems/Headington

33

Inserting a Node at the Front of a List

char item = ‘B’;

NodePtr location;

location = new NodeType;

location->info = item;

location->link = head;

head = location;

head ‘X’ ‘C’ ‘L’

‘B’item

location

Page 34: 1 Chapter 16 Linked Structures Dale/Weems/Headington

34

Inserting a Node at the Front of a List

char item = ‘B’;

NodePtr location;

location = new NodeType;

location->info = item;

location->link = head;

head = location;

head ‘X’ ‘C’ ‘L’

‘B’item

location

Page 35: 1 Chapter 16 Linked Structures Dale/Weems/Headington

35

Inserting a Node at the Front of a List

char item = ‘B’;

NodePtr location;

location = new NodeType;

location->info = item;

location->link = head;

head = location;

head ‘X’ ‘C’ ‘L’

‘B’item

location ‘B’

Page 36: 1 Chapter 16 Linked Structures Dale/Weems/Headington

36

Inserting a Node at the Front of a List

char item = ‘B’;

NodePtr location;

location = new NodeType;

location->info = item;

location->link = head;

head = location;

head ‘X’ ‘C’ ‘L’

‘B’item

location ‘B’

Page 37: 1 Chapter 16 Linked Structures Dale/Weems/Headington

37

Inserting a Node at the Front of a List

char item = ‘B’;

NodePtr location;

location = new NodeType;

location->info = item;

location->link = head;

head = location;

head ‘X’ ‘C’ ‘L’

‘B’item

location ‘B’

Page 38: 1 Chapter 16 Linked Structures Dale/Weems/Headington

The object currently pointed to by the pointer is deallocated, and the pointer is considered undefined. The object’s memory is returned to the free store.

Using Operator delete

38

Page 39: 1 Chapter 16 Linked Structures Dale/Weems/Headington

39

Deleting the First Node from the List

NodePtr tempPtr;

item = head->info;

tempPtr = head;

head = head->link;

delete tempPtr;

head

item

‘B’ ‘X’ ‘C’ ‘L’

tempPtr

Page 40: 1 Chapter 16 Linked Structures Dale/Weems/Headington

40

Deleting the First Node from the List

NodeType * tempPtr;

item = head->info;

tempPtr = head;

head = head->link;

delete tempPtr;

head

item

‘B’ ‘X’ ‘C’ ‘L’

tempPtr

‘B’

Page 41: 1 Chapter 16 Linked Structures Dale/Weems/Headington

41

Deleting the First Node from the List

NodeType * tempPtr;

item = head->info;

tempPtr = head;

head = head->link;

delete tempPtr;

head

item

‘B’ ‘X’ ‘C’ ‘L’

tempPtr

‘B’

Page 42: 1 Chapter 16 Linked Structures Dale/Weems/Headington

42

Deleting the First Node from the List

NodeType * tempPtr;

item = head->info;

tempPtr = head;

head = head->link;

delete tempPtr;

head

item

‘B’ ‘X’ ‘C’ ‘L’

tempPtr

‘B’

Page 43: 1 Chapter 16 Linked Structures Dale/Weems/Headington

43

Deleting the First Node from the List

NodeType * tempPtr;

item = head->info;

tempPtr = head;

head = head->link;

delete tempPtr;

head

item

‘X’ ‘C’ ‘L’

tempPtr

‘B’

Page 44: 1 Chapter 16 Linked Structures Dale/Weems/Headington

44

What is a List?

a list is a varying-length, linear collection of homogeneous elements

linear means each list element (except the first) has a unique predecessor, and each element (except the last) has a unique successor

Page 45: 1 Chapter 16 Linked Structures Dale/Weems/Headington

ADT SortedList2 Operations

Transformers InsertTop Insert DeleteTop Delete

Observers Print IsEmpty

change state

observe state

45

Page 46: 1 Chapter 16 Linked Structures Dale/Weems/Headington

46

// SPECIFICATION FILE DYNAMIC-LINKED SORTED LIST( slist2.h )

typedef int ItemType ; // Type of each component // is simple type or string type

struct NodeType{

ItemType item ; // Pointer to person’s nameNodeType* link ; // link to next node in list

} ;

typedef NodeType* NodePtr;

46

struct NodeType

Page 47: 1 Chapter 16 Linked Structures Dale/Weems/Headington

47

// SPECIFICATION FILE DYNAMIC-LINKED SORTED LIST( slist2.h )

class SortedList2{public :

bool IsEmpty ( ) const ;

void Print ( ) const ;

void InsertTop ( /* in */ ItemType item ) ;

void Insert ( /* in */ ItemType item ) ;

void DeleteTop ( /* out */ ItemType& item ) ;

void Delete ( /* in */ ItemType item );

SortedList2 ( ) ; // Constructor~SortedList2 ( ) ; // DestructorSortedList2 ( const SortedList2& otherList ) ; // Copy-constructor

private :

NodeType* head;} ;

47

Page 48: 1 Chapter 16 Linked Structures Dale/Weems/Headington

48

class SortedList2

Print

~SortedList2

Insert

InsertTop

SortedList2

IsEmpty

Delete

‘C’ ‘L’ ‘X’

Private data:

head

DeleteTop

Page 49: 1 Chapter 16 Linked Structures Dale/Weems/Headington

49

Insert Algorithm

what will be the algorithm to Insert an item into its proper place in a sorted linked list?

that is, for a linked list whose elements are maintained in ascending order?

Page 50: 1 Chapter 16 Linked Structures Dale/Weems/Headington

50

Insert algorithm for SortedList2

find proper position for the new element in the sorted list using two pointers prevPtr and currPtr, where prevPtr trails behind currPtr

obtain a node for insertion and place item in it

insert the node by adjusting pointers

Page 51: 1 Chapter 16 Linked Structures Dale/Weems/Headington

51

Implementing SortedList2Member Function Insert

// DYNAMIC LINKED LIST IMPLEMENTATION (slist2.cpp)

void SortedList2 :: Insert ( /* in */ ItemType item )

// PRE: item is assigned && List components in ascending order

// POST: item is in List && List components in ascending order{

.

.

.

}

Page 52: 1 Chapter 16 Linked Structures Dale/Weems/Headington

52

Inserting ‘S’ into a Sorted List

‘C’ ‘L’ ‘X’

Private data:

head

prevPtr currPtr

Page 53: 1 Chapter 16 Linked Structures Dale/Weems/Headington

53

Finding Proper Position for ‘S’

‘C’ ‘L’ ‘X’

Private data:

head

prevPtr currPtr

NULL

Page 54: 1 Chapter 16 Linked Structures Dale/Weems/Headington

54

‘C’ ‘L’ ‘X’

Private data:

head

prevPtr currPtr

Finding Proper Position for ‘S’

Page 55: 1 Chapter 16 Linked Structures Dale/Weems/Headington

55

‘C’ ‘L’ ‘X’

Private data:

head

prevPtr currPtr

Finding Proper Position for ‘S’

Page 56: 1 Chapter 16 Linked Structures Dale/Weems/Headington

56

‘C’ ‘L’ ‘X’

Private data:

head

prevPtr currPtr

Inserting ‘S’ into Proper Position

‘S’

Page 57: 1 Chapter 16 Linked Structures Dale/Weems/Headington

57

// IMPLEMENTATION DYNAMIC-LINKED SORTED LIST (slist2.cpp)

SortedList2 ::SortedList2 ( ) // Constructor

// Post: head == NULL

{

head = NULL ;

}

SortedList2 :: ~SortedList2 ( ) // Destructor

// Post: All linked nodes deallocated

{

ItemType temp ;

// keep deleting top node

while ( !IsEmpty )

DeleteTop ( temp );

}57

Page 58: 1 Chapter 16 Linked Structures Dale/Weems/Headington

58

void SortedList2 :: Insert( /* in */ ItemType item ) // Pre: item is assigned && list components in ascending order

// Post: new node containing item is in its proper place

// && list components in ascending order

{ NodePtr currPtr ;

NodePtr prevPtr ;

NodePtr location ;

location = new NodeType ;

newNodePtr->link = item ;

prevPtr = NULL ;

currPtr = head ;

while ( currPtr != NULL && item > currPtr->info )

{ prevPtr = currPtr ; // advance both pointers currPtr = currPtr->link ;

}

location->link = currPtr ; // insert new node here

if ( prevPtr == NULL )

head = location ;

else

prevPtr->link = location ;

}58

Page 59: 1 Chapter 16 Linked Structures Dale/Weems/Headington

59

void SortedList2 :: DeleteTop ( /* out */ ItemType& item )

// Pre: list is not empty && list elements in ascending order

// Post: item == element of first list node @ entry

// && node containing item is no longer in linked list

// && list elements in ascending order

{

NodePtr tempPtr = head ;

// obtain item and advance head

item = head->info ;

head = head->link ;

delete tempPtr ;

}

59

Page 60: 1 Chapter 16 Linked Structures Dale/Weems/Headington

60

void SortedList2 :: Delete ( /* in */ ItemType item ) // Pre: list is not empty && list elements in ascending order

// && item == component member of some list node

// Post: item == element of first list node @ entry

// && node containing first occurrence of item is no longer

// in linked list && list elements in ascending order

{ NodePtr delPtr ;

NodePtr currPtr ; // Is item in first node?

if ( item == head->info )

{ delPtr = head ; // If so, delete first node

head = head->link ;}

else { // search for item in rest of list

currPtr = head ;

while ( currPtr->link->info != item )

currPtr = currPtr->link ;

delPtr = currPtr->link ;

currPtr->link = currPtr->link->link ;}

delete delPtr ;

}60

Page 61: 1 Chapter 16 Linked Structures Dale/Weems/Headington

61

class SortedList2

Print

~SortedList2

Insert

InsertTop

SortedList2

IsEmpty

Delete

‘C’ ‘L’ ‘X’

Private data:

head

DeleteTop