Linked Structures - Texas Southern Universitycs.tsu.edu/ghemri/CS246/ClassNotes/LinkedList.pdf ·...

Post on 24-Jun-2020

5 views 0 download

Transcript of Linked Structures - Texas Southern Universitycs.tsu.edu/ghemri/CS246/ClassNotes/LinkedList.pdf ·...

55Linked Structures

What is a List?

• A list is a homogeneous collection of• A list is a homogeneous collection of elements, with a linear relationshipbetween elementsbetween elements.

• That is each list element (except theThat is, each list element (except the first) has a unique predecessor, and each element (except the last) has aeach element (except the last) has a unique successor.

2

A Single Node

.info .next.info .next

‘D ’

The user’s data Pointer to the next node innext node inthe stack

3

Node terminology

4

ADT Unsorted List Operations

Transformers • MakeEmpty • InsertItem change state

• DeleteItemObservers

• IsFull• IsFull• LengthIs• RetrieveItem

observe state

RetrieveItem

Iterators • ResetListResetList • GetNextItem

process all

5

#include “ItemType.h” // unsorted.h. . .

template <class ItemType>class UnsortedTypeclass UnsortedType{public : // LINKED LIST IMPLEMENTATION

UnsortedType ( ) ;~UnsortedType ( ) ;void MakeEmpty ( ) ;bool IsFull ( ) const ; int LengthIs ( ) const ;int LengthIs ( ) const ; void RetrieveItem ( ItemType& item, bool& found ) ;void InsertItem ( ItemType item ) ; void DeleteItem ( ItemType item ) ; void ResetList ( );void GetNextItem ( ItemType& item ) ;

private :NodeType<ItemType>* listData;NodeType<ItemType> listData;int length;NodeType<ItemType>* currentPos;

} ;6

class UnsortedType<char>

UnsortedTypePrivate data:

MakeEmpty

~UnsortedType ‘X’ ‘C’ ‘L’

Private data:length 3

listData

InsertItem

RetrieveItem‘X’ ‘C’ ‘L’listData

currentPos ?

DeleteItem...

7

.

GetNextItem

// LINKED LIST IMPLEMENTATION ( unsorted.cpp )#include “itemtype.h”

template <class ItemType>UnsortedType<ItemType>::UnsortedType ( ) // constructor// Pre: None.// Post: List is empty.{

length = 0 ;listData = NULL;

}

template <class ItemType>template <class ItemType>int UnsortedType<ItemType>::LengthIs ( ) const// Post: Function value = number of items in the list.{{

return length;} 8

template <class ItemType>void UnsortedType<ItemType>::RetrieveItem( ItemType& item bool&void UnsortedType<ItemType>::RetrieveItem( ItemType& item, bool&

found ) // Pre: Key member of item is initialized.// Post: If found, item’s key matches an element’s key in the list // and a copy of that element has been stored in item; otherwise// and a copy of that element has been stored in item; otherwise,// item is unchanged.{ bool moreToSearch ;

NodeType<ItemType>* location ;location = listData ;found = false ;moreToSearch = ( location != NULL ) ;while ( moreToSearch && !found ) ( ){ if ( item == location->info ) // match here

{ found = true ;item = location->info ;

}}else // advance pointer { location = location->next ;

moreToSearch = ( location != NULL ) ;}

} }

9

template <class ItemType>void UnsortedType<ItemType>::InsertItem ( ItemType item ) // Pre: list is not full and item is not in list// Pre: list is not full and item is not in list.// Post: item is in the list; length has been incremented.{

d t * l tiNodeType<ItemType>* location ;// obtain and fill a nodelocation = new NodeType<ItemType> ;location->info = item ;location->next = listData ;listData = location ;listData location ;length++ ;

}

10

Inserting ‘B’ into an Unsorted List

Private data:length 3

‘X’ ‘C’ ‘L’listData

currentPos ?

11

location = new NodeType<ItemType>;item ‘B’

location

Private data:length 3

‘X’ ‘C’ ‘L’listData

currentPos ?

12

location->info = item ;item ‘B’

location ‘B’

Private data:length 3

‘X’ ‘C’ ‘L’listData

currentPos ?

13

location->next = listData ;item ‘B’

location ‘B’

Private data:length 3

‘X’ ‘C’ ‘L’listData

currentPos ?

14

listData = location ;item ‘B’

location ‘B’

Private data:length 3

‘X’ ‘C’ ‘L’listData

currentPos ?

15

length++ ;item ‘B’

location ‘B’

Private data:length 4

‘X’ ‘C’ ‘L’listData

currentPos ?

16

class SortedType<char>

SortedTypePrivate data:

MakeEmpty

~SortedType ‘C’ ‘L’ ‘X’

Private data:length 3

listData

InsertItem

RetrieveItem‘C’ ‘L’ ‘X’listData

currentPos ?

DeleteItem...

17

.

GetNextItem

InsertItem algorithm for Sorted Linked List

• Find proper position for the new element• Find proper position for the new element in the sorted list using two pointers predLoc and location, where predLoc p , ptrails behind location.

• Obtain a node for insertion and place item• Obtain a node for insertion and place item in it.

• Insert the node by adjusting pointers.

• Increment length.

18

g

Implementing SortedType member function InsertItem

// LINKED LIST IMPLEMENTATION (sorted.cpp)#include “ItemType.h”

template <class ItemType>p ypvoid SortedType<ItemType> :: InsertItem ( ItemType item )// Pre: List has been initialized. List is not full. // item is not in list.// List is sorted by key member.// Post: item is in the list. List is still sorted.{

.

.

.

19

}

The Inchworm Effect

20

Inserting ‘S’ into a Sorted List

predLoc location

Private data:length 3

‘C’ ‘L’ ‘X’listData

currentPos ?

T S h

21

moreToSearch

Finding proper position for ‘S’

predLoc locationNULL

Private data:length 3

‘C’ ‘L’ ‘X’listData

currentPos ?

T S h t

22

moreToSearch true

Finding proper position for ‘S’

predLoc location

Private data:length 3

‘C’ ‘L’ ‘X’listData

currentPos ?

T S h t

23

moreToSearch true

Finding Proper Position for ‘S’

predLoc location

Private data:length 3

‘C’ ‘L’ ‘X’listData

currentPos ?

T S h f l

24

moreToSearch false

Inserting ‘S’ into Proper Position

predLoc location

Private data:length 4

‘C’ ‘L’ ‘X’listData

currentPos

T S h f l‘S’

25

moreToSearch false