Sorted - PowerPoint PPT Presentation

1 / 35
About This Presentation
Title:

Sorted

Description:

A Trailer Node is a node at the end of a list that contains a key larger ... Both header and trailer are placeholding nodes used to simplify list processing. ... – PowerPoint PPT presentation

Number of Views:68
Avg rating:3.0/5.0
Slides: 36
Provided by: anne277
Category:
Tags: sorted | trailer

less

Transcript and Presenter's Notes

Title: Sorted


1
Sorted Unsorted List as Linked Structures
  • CSC 2110 - Data Structures Abstraction
  • Spring/Summer 2001
  • Wayne State University
  • Instructor Anne-Marie Bosneag

2
Content
  • Definition of unsorted list
  • Specification of an unsorted list as a linked
    structure
  • Implementation of an unsorted list as a linked
    structure
  • Definition of the sorted list
  • Specification of a sorted list as a linked
    structure
  • Implementation of a sorted list as a linked
    structure

3
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.

4
ADT Unsorted List Operations
  • Transformers
  • MakeEmpty
  • InsertItem
  • DeleteItem
  • Observers
  • IsFull
  • LengthIs
  • RetrieveItem
  • Iterators
  • ResetList
  • GetNextItem

4
5
  • include ItemType.h // unsorted.h
  • . . .
  • template ltclass ItemTypegt
  • 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
  • NodeTypeltItemTypegt listData
  • int length

5
6

class UnsortedTypeltchargt
UnsortedType
Private data length
3 listData currentPos
MakeEmpty
UnsortedType
RetrieveItem
InsertItem
DeleteItem . .
GetNextItem
7
  • // LINKED LIST IMPLEMENTATION ( unsorted.cpp )
  • include itemtype.h
  • template ltclass ItemTypegt
  • UnsortedTypeltItemTypegtUnsortedType ( ) //
    constructor
  • // Pre None.
  • // Post List is empty.
  • length 0
  • listData NULL
  • template ltclass ItemTypegt
  • int UnsortedTypeltItemTypegtLengthIs ( ) const
  • // Post Function value number of items in the
    list.
  • return length

7
8
  • template ltclass ItemTypegt
  • void UnsortedTypeltItemTypegtRetrieveItem(
    ItemType item, bool found )
  • // Pre Key member of item is initialized.
  • // Post If found, items key matches an
    elements key in the list and a copy
  • // of that element has been stored in item
    otherwise, item is unchanged.
  • bool moreToSearch
  • NodeTypeltItemTypegt location
  • location listData
  • found false
  • moreToSearch ( location ! NULL )
  • while ( moreToSearch !found )
  • if ( item location-gtinfo )
    // match here
  • found true
  • item location-gtinfo
  • else // advance pointer
  • location location-gtnext
  • moreToSearch ( location ! NULL )

8
9
  • template ltclass ItemTypegt
  • void UnsortedTypeltItemTypegtInsertItem (
    ItemType item )
  • // Pre list is not full and item is not in
    list.
  • // Post item is in the list length has been
    incremented.
  • NodeTypeltItemTypegt location
  • // obtain and fill a node
  • location new NodeTypeltItemTypegt
  • location-gtinfo item
  • location-gtnext listData
  • listData location
  • length

9
10

Inserting B into an Unsorted List
11

Inserting B into an Unsorted List
location new NodeTypeltItemTypegt
item location
B
12

Inserting B into an Unsorted List
location-gtinfo item
item location
B
B
13
Inserting B into an Unsorted List
location-gtnext listData
B
item location
B
14
Inserting B into an Unsorted List
listData location
item location
B
B
Private data length
3 listData currentPos
15

length
item location
B
B
Private data length
4 listData currentPos
16

class SortedTypeltchargt
SortedType
Private data length 3 listData curren
tPos
MakeEmpty
SortedType
RetrieveItem
InsertItem
DeleteItem . .
GetNextItem
17
InsertItem algorithm for Sorted Linked List
  • Find proper position for the new element in the
    sorted list using two pointers predLoc and
    location, where predLoc trails behind location.
  • Obtain a node for insertion and place item in it.
  • Insert the node by adjusting pointers.
  • Increment length.

18
Implementing SortedType member function
InsertItem

// LINKED LIST IMPLEMENTATION
(sorted.cpp) include ItemType.h template
ltclass ItemTypegt void SortedTypeltItemTypegt
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

Inserting S into a Sorted List
predLoc location
Private data length
3 listData currentPos
20

Finding proper position for S
predLoc location
NULL
Private data length 3 listData current
Pos
21

Finding proper position for S
predLoc location
Private data length 3 listData curren
tPos
22

Finding proper position for S
predLoc location
Private data length
3 listData currentPos
23

Inserting S into proper position
predLoc location
Private data length
4 listData currentPos
C L X
24
Inserting an element into SortedType
  • template ltclass ItemTypegt
  • void SortedTypeltItemTypegtInsertItem(ItemType
    item)
  • NodeTypeltItemTypegt newNode, predLoc,
    location
  • bool moreToSearch true
  • location listData predLoc NULL
  • while (moreToSearch)
  • if (location-gtinfo lt item)
  • predLoc location
  • location location-gtnext
  • moreToSearch (location ! NULL)
  • else
  • moreToSearch false

25
Inserting an element into SortedType
  • newNode new NodeTypeltItemTypegt
  • newNode-gtinfo item
  • if (predLoc NULL)
  • newNode-gtnext listData
  • listData newNode
  • else
  • newNode-gtnext location
  • predLoc-gtnext newNode
  • length

26
What is a Circular Linked List?
  • A circular linked list is a list in which every
    node has a successor the last element is
    succeeded by the first element.

B C L
listData
27
Finding a List Item into a Circular Sorted List
  • template ltclass ItemTypegt
  • void FindItem (NodeTypeltItemType listData,
    ItemType item,
  • NodeTypeltItemTypegt location,
  • NodeTypeltItemTypegt predLoc, bool found)
  • //Pre List not empty
  • //Post If there is an element in the list, whose
    key matches the items key, then found true,
    location contains the address of the found
    element and predLoc contains the address of the
    successor. If not found, found false and
    predLoc and location point to predecessor and
    succesor, respectively.

28
Finding a List Item - cont.
  • bool moreToSearch
  • location listData-gtnext
  • predLoc listData
  • found false
  • while (moreToSearch !found)
  • if (item lt location-gtinfo)
  • moreToSearch false
  • else if (item location-gtinfo)
  • found true
  • else predLoc location
  • location location-gtnext
  • moreToSeach (location !
    listData-gtnext)

29
Inserting an Item into a Circular Sorted List
  • template ltclass ItemTypegt
  • void SortedTypeltItemTypegtInsertItem (ItemType
    item)
  • NodeTypeltItemTypegt newNode, predLoc,
    location
  • bool found
  • newNode new NodeTypeltItemTypegt
  • newNode-gtinfo item
  • if (listData ! NULL)
  • FindItem(listData, item, location, predLoc,
    found)
  • newNode-gtnext predLoc-gtnext
  • predLoc-gtnext newNode
  • //if last node, reassign listData
  • if (listData-gtinfo lt item)
  • listData newNode

30
Inserting an Item - cont
  • else
  • listData newNode
  • newNode-gtnext newNode
  • length

31
Deleting an Item from a Circular Sorted List
  • template ltclass ItemTypegt
  • void SortedTypeltItemTypegtDeleteItem (ItemType
    item)
  • NodeTypeltItemTypegt location, predLoc
  • bool found
  • FindItem(listData, item, location, predLoc,
    found)
  • if (predLoc location) //the only node in the
    list
  • listData NULL
  • else predLoc-gtnext location-gtnext
  • if (location listData)
  • listData predLoc
  • delete location
  • length--

32
What is a Doubly Linked List?
  • A doubly linked list is a list in which each node
    is linked to both its successor and its
    predecessor.

A C F
T Z
listData
33
Each node contains two pointers
templatelt class ItemType gt struct NodeType
ItemType info //Data member
NodeTypeltItemTypegt back //Pointer to
predecessor NodeTypeltItemTypegt next
//Pointer to successor
3000 A NULL
. back . info . next
34
Finding an Item into a Doubly-linked Sorted List
  • template ltclass ItemTypegt
  • void FindItem (NodeTypeltItemTypegt listData,
    ItemType item,
  • NodeTypeltItemTypegt location, bool found)
  • bool moreToSearch true
  • location listData found false
  • while (moreToSearch !found)
  • if (item lt location-gtinfo)
  • moreToSearch false
  • else if (item location-gtinfo)
  • found true
  • else location location-gtnext
  • moreToSearch (location ! NULL)

35
What are Header and Trailer Nodes?
  • A Header Node is a node at the beginning of a
    list that contains a key value smaller than any
    possible key.
  • A Trailer Node is a node at the end of a list
    that contains a key larger than any possible key.
  • Both header and trailer are placeholding nodes
    used to simplify list processing.

INT_MIN 5 8
13 INT_MAX
listData
Write a Comment
User Comments (0)
About PowerShow.com