강의자료7

20
1 What is a List? A list is a homogeneous collection of elements, with a linear relationship between elements. That is, each list element (except the first) has a unique predecessor, and each element (except the last) has a unique successor.

Upload: young-wook-kim

Post on 23-Jun-2015

242 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 강의자료7

1

What is a List?

A list is a homogeneous collection of elements, with a linear relationship between elements.

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

Page 2: 강의자료7

ADT Unsorted List Operations

Transformers MakeEmpty InsertItem DeleteItem

Observers IsFull LengthIs RetrieveItem

Iterators ResetList GetNextItem

change state

observe state

process all

2

Page 3: 강의자료7

// SPECIFICATION FILE ( unsorted.h )

class UnsortedType // declares a class data type{public : // 8 public member functions ……

private : // 3 private data members// 리스트를 배열로 표현

int length ; ItemType info[MAX_ITEMS] ; // MAX_ITEMS 은 상수임 int currentPos ;

// Linked List 구현private :

NodeType* listData;int length;NodeType* currentPos;

} ;

struct ItemType{ …. KeyType key; …};

ADT Unsorted List 의 Linked 리스트 구현

Page 4: 강의자료7

4

class UnsortedType

MakeEmpty

~UnsortedType

DeleteItem . . .

InsertItem

UnsortedType

RetrieveItem

GetNextItem

‘X’ ‘C’ ‘L’

Private data:

length 3

listData

currentPos ?

Page 5: 강의자료7

Linked Implementation

5

다음 세 가지의 차이점 ?

location,*location, 과location->info

Page 6: 강의자료7

struct NodeType {

ItemType info;NodeType* next;

};

class UnsortedType{public : // LINKED LIST IMPLEMENTATION

UnsortedType ( ) ;~UnsortedType ( ) ;void MakeEmpty ( ) ;bool IsFull ( ) 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* listData;int length;NodeType* currentPos;

} ;

Page 7: 강의자료7

// 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>int UnsortedType<ItemType>::LengthIs ( ) const// Post: Function value = number of items in the list.{

return length;}

7

Page 8: 강의자료7

Linked Implementation: RetrieveItem

8

Page 9: 강의자료7

template <class ItemType> 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,// item is unchanged.{ bool moreToSearch ;

NodeType<ItemType>* location ; location = listData ;

found = false ;moreToSearch = ( location != NULL ) ;while ( moreToSearch && !found ) { if ( item.key == location->info.key ) // match here

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

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

moreToSearch = ( location != NULL ) ; }

} } 9

Page 10: 강의자료7

template <class ItemType>

void UnsortedType<ItemType>::InsertItem ( ItemType item )

// Pre: list is not full and item is not in list.

// Post: item is in the list; length has been incremented.

{

NodeType<ItemType>* location ;

// obtain and fill a node

location = new NodeType<ItemType> ;

location->info = item ;

location->next = listData ;

listData = location ;

length++ ;

}

10

Page 11: 강의자료7

11

Inserting ‘B’ into an Unsorted List

‘X’ ‘C’ ‘L’

Private data:

length 3

listData

currentPos ?

Page 12: 강의자료7

12

location = new NodeType<ItemType>;

‘X’ ‘C’ ‘L’

Private data:

length 3

listData

currentPos ?

item

location

‘B’

Page 13: 강의자료7

13

location->info = item ;

‘X’ ‘C’ ‘L’

Private data:

length 3

listData

currentPos ?

item

location

‘B’

‘B’

Page 14: 강의자료7

14

location->next = listData ;

‘X’ ‘C’ ‘L’

Private data:

length 3

listData

currentPos ?

item

location

‘B’

‘B’

Page 15: 강의자료7

15

listData = location ;

‘X’ ‘C’ ‘L’

Private data:

length 3

listData

currentPos ?

item

location

‘B’

‘B’

Page 16: 강의자료7

16

length++ ;

‘X’ ‘C’ ‘L’

Private data:

length 4

listData

currentPos ?

item

location

‘B’

‘B’

Page 17: 강의자료7

Linked Implementation: DeleteItem

17

How do you delete an item from the list?

1. Find the item

2. Remove the item

Page 18: 강의자료7

template <class ItemType>

void UnsortedType<ItemType>::DeleteItem ( ItemType item )

// Pre: An element in the list has a key that matches item’s.

// Post: No element in the list has a key that matches item’s.

{ NodeType<ItemType>* location = listData ;

NodeType<ItemType>* tempLocation;

// Locate node to be deleted.

if (item.key == listData->info.key)

{ tempLocation = location;

listData = listData->next;

}

else

{ while(!(item.key == (location->next)->info.key))

location = location->next;

// Delete node at location->next.

tempLocation = location->next;

location->next = location->next->next;

}

delete tempLocation;

length--;

}

18

Page 19: 강의자료7

template <class ItemType>

void UnsortedType<ItemType>::ResetList()

{

currentPos = NULL;

}

template <class ItemType>

void UnsortedType<ItemType>::GetNextItem(ItemType& item)

{

if (currentPos == NULL)

currentPos = listData;

else

currentPos = currentPos->next;

item = currentPos->info;

}

19

Page 20: 강의자료7

template <class ItemType>

UnsortedType<ItemType>::~UnsortedType()

{

NodeType<ItemType>* tempPtr;

while(listData != NULL)

{

tempPtr = listData;

listData = listData->next;

delete tempPtr;

}

}

20