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

25
5 5 Linked Structures

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

Page 1: Linked Structures - Texas Southern Universitycs.tsu.edu/ghemri/CS246/ClassNotes/LinkedList.pdf · Sorted Linked List • Find proper position for the new elementFind proper position

55Linked Structures

Page 2: Linked Structures - Texas Southern Universitycs.tsu.edu/ghemri/CS246/ClassNotes/LinkedList.pdf · Sorted Linked List • Find proper position for the new elementFind proper position

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

Page 3: Linked Structures - Texas Southern Universitycs.tsu.edu/ghemri/CS246/ClassNotes/LinkedList.pdf · Sorted Linked List • Find proper position for the new elementFind proper position

A Single Node

.info .next.info .next

‘D ’

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

3

Page 4: Linked Structures - Texas Southern Universitycs.tsu.edu/ghemri/CS246/ClassNotes/LinkedList.pdf · Sorted Linked List • Find proper position for the new elementFind proper position

Node terminology

4

Page 5: Linked Structures - Texas Southern Universitycs.tsu.edu/ghemri/CS246/ClassNotes/LinkedList.pdf · Sorted Linked List • Find proper position for the new elementFind proper position

ADT Unsorted List Operations

Transformers • MakeEmpty • InsertItem change state

• DeleteItemObservers

• IsFull• IsFull• LengthIs• RetrieveItem

observe state

RetrieveItem

Iterators • ResetListResetList • GetNextItem

process all

5

Page 6: Linked Structures - Texas Southern Universitycs.tsu.edu/ghemri/CS246/ClassNotes/LinkedList.pdf · Sorted Linked List • Find proper position for the new elementFind proper position

#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

Page 7: Linked Structures - Texas Southern Universitycs.tsu.edu/ghemri/CS246/ClassNotes/LinkedList.pdf · Sorted Linked List • Find proper position for the new elementFind proper position

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

Page 8: Linked Structures - Texas Southern Universitycs.tsu.edu/ghemri/CS246/ClassNotes/LinkedList.pdf · Sorted Linked List • Find proper position for the new elementFind proper position

// 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

Page 9: Linked Structures - Texas Southern Universitycs.tsu.edu/ghemri/CS246/ClassNotes/LinkedList.pdf · Sorted Linked List • Find proper position for the new elementFind proper position

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

Page 10: Linked Structures - Texas Southern Universitycs.tsu.edu/ghemri/CS246/ClassNotes/LinkedList.pdf · Sorted Linked List • Find proper position for the new elementFind proper position

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

Page 11: Linked Structures - Texas Southern Universitycs.tsu.edu/ghemri/CS246/ClassNotes/LinkedList.pdf · Sorted Linked List • Find proper position for the new elementFind proper position

Inserting ‘B’ into an Unsorted List

Private data:length 3

‘X’ ‘C’ ‘L’listData

currentPos ?

11

Page 12: Linked Structures - Texas Southern Universitycs.tsu.edu/ghemri/CS246/ClassNotes/LinkedList.pdf · Sorted Linked List • Find proper position for the new elementFind proper position

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

location

Private data:length 3

‘X’ ‘C’ ‘L’listData

currentPos ?

12

Page 13: Linked Structures - Texas Southern Universitycs.tsu.edu/ghemri/CS246/ClassNotes/LinkedList.pdf · Sorted Linked List • Find proper position for the new elementFind proper position

location->info = item ;item ‘B’

location ‘B’

Private data:length 3

‘X’ ‘C’ ‘L’listData

currentPos ?

13

Page 14: Linked Structures - Texas Southern Universitycs.tsu.edu/ghemri/CS246/ClassNotes/LinkedList.pdf · Sorted Linked List • Find proper position for the new elementFind proper position

location->next = listData ;item ‘B’

location ‘B’

Private data:length 3

‘X’ ‘C’ ‘L’listData

currentPos ?

14

Page 15: Linked Structures - Texas Southern Universitycs.tsu.edu/ghemri/CS246/ClassNotes/LinkedList.pdf · Sorted Linked List • Find proper position for the new elementFind proper position

listData = location ;item ‘B’

location ‘B’

Private data:length 3

‘X’ ‘C’ ‘L’listData

currentPos ?

15

Page 16: Linked Structures - Texas Southern Universitycs.tsu.edu/ghemri/CS246/ClassNotes/LinkedList.pdf · Sorted Linked List • Find proper position for the new elementFind proper position

length++ ;item ‘B’

location ‘B’

Private data:length 4

‘X’ ‘C’ ‘L’listData

currentPos ?

16

Page 17: Linked Structures - Texas Southern Universitycs.tsu.edu/ghemri/CS246/ClassNotes/LinkedList.pdf · Sorted Linked List • Find proper position for the new elementFind proper position

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

Page 18: Linked Structures - Texas Southern Universitycs.tsu.edu/ghemri/CS246/ClassNotes/LinkedList.pdf · Sorted Linked List • Find proper position for the new elementFind proper position

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

Page 19: Linked Structures - Texas Southern Universitycs.tsu.edu/ghemri/CS246/ClassNotes/LinkedList.pdf · Sorted Linked List • Find proper position for the new elementFind proper position

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

}

Page 20: Linked Structures - Texas Southern Universitycs.tsu.edu/ghemri/CS246/ClassNotes/LinkedList.pdf · Sorted Linked List • Find proper position for the new elementFind proper position

The Inchworm Effect

20

Page 21: Linked Structures - Texas Southern Universitycs.tsu.edu/ghemri/CS246/ClassNotes/LinkedList.pdf · Sorted Linked List • Find proper position for the new elementFind proper position

Inserting ‘S’ into a Sorted List

predLoc location

Private data:length 3

‘C’ ‘L’ ‘X’listData

currentPos ?

T S h

21

moreToSearch

Page 22: Linked Structures - Texas Southern Universitycs.tsu.edu/ghemri/CS246/ClassNotes/LinkedList.pdf · Sorted Linked List • Find proper position for the new elementFind proper position

Finding proper position for ‘S’

predLoc locationNULL

Private data:length 3

‘C’ ‘L’ ‘X’listData

currentPos ?

T S h t

22

moreToSearch true

Page 23: Linked Structures - Texas Southern Universitycs.tsu.edu/ghemri/CS246/ClassNotes/LinkedList.pdf · Sorted Linked List • Find proper position for the new elementFind proper position

Finding proper position for ‘S’

predLoc location

Private data:length 3

‘C’ ‘L’ ‘X’listData

currentPos ?

T S h t

23

moreToSearch true

Page 24: Linked Structures - Texas Southern Universitycs.tsu.edu/ghemri/CS246/ClassNotes/LinkedList.pdf · Sorted Linked List • Find proper position for the new elementFind proper position

Finding Proper Position for ‘S’

predLoc location

Private data:length 3

‘C’ ‘L’ ‘X’listData

currentPos ?

T S h f l

24

moreToSearch false

Page 25: Linked Structures - Texas Southern Universitycs.tsu.edu/ghemri/CS246/ClassNotes/LinkedList.pdf · Sorted Linked List • Find proper position for the new elementFind proper position

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