CMSC 341 - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

CMSC 341

Description:

CMSC 341 Lists 3 – PowerPoint PPT presentation

Number of Views:120
Avg rating:3.0/5.0
Slides: 16
Provided by: umb60
Category:
Tags: cmsc | circle | class

less

Transcript and Presenter's Notes

Title: CMSC 341


1
CMSC 341
  • Lists 3

2
Doubly-Linked Lists
  • Option add pointer to previous node
  • Issues
  • doubles number of pointers
  • allows immediate (O(1)) access to previous node

3
Circular Linked Lists
  • Configurations
  • header in circle
  • header before circle
  • no header

4
Doubly-Linked Circular Linked List
  • May or may not have a header
  • Useful for applications requiring fast access to
    head and tail

5
Vector Implementation
  • template ltclass Objectgt
  • void ListltObjectgt
  • insert(const Object x, const ListItrltObjectgt p)
  • if (!p.isPastEnd())
  • // shift all elements down
  • for (i this.last i gt p.current1 i--)
  • nodesi 1 nodesi
  • // put new element in hole
  • nodesp.current 1 x

6
Cursor Implementation
  • Linked list look feel
  • data stored as collection of nodes data and next
  • nodes allocated and deallocated
  • without dynamic memory allocation
  • Basic concepts
  • have node pool of all nodes, keep list of free
    ones
  • use pool indices in place of pointers 0 NULL

7
Cursor List Class
  • template ltclass Objectgt
  • class List
  • List()
  • // same old list public interface
  • public
  • struct CursorNode
  • CursorNode() next(0)
  • private
  • CursorNode(const Object theElement, int n)
  • element(theElement), next(n)
  • Object element
  • int next
  • friend class ListltObjectgt
  • friend class ListItrltObjectgt

8
Cursor List Class (cont.)
  • private
  • int header
  • static vectorltCursorNodegt cursorSpace
  • static void initializeCursorSpace( )
  • static int alloc( )
  • static void free (int p)
  • friend class ListItrltObjectgt

9
Cursor Initialization
  • template ltclass Objectgt
  • void ListltObjectgtinitializeCursorSpace()
  • static int cursorSpaceIsInitialized false
  • if (!cursorSpaceIsInitialized)
  • cursorSpace.resize(100)
  • for(int i 0 i lt cursorSpace.size() i)
  • cursorSpacei.next i 1
  • cursorSpacecursorSpace.size()-1.next 0
  • cursorSpaceIsInitialized true

10
cursorSpace
11
Cursor Allocation
  • template ltclass Objectgt
  • void ListltObjectgtalloc()
  • int p cursorSpace0.next
  • cursorSpace0.next cursorSpacep.next
  • return p
  • template ltclass Objectgt
  • void ListltObjectgtfree(int p)
  • cursorSpacep.next cursorSpace0.next
  • cursorSpace0.next p

12
Cursor Implementation (cont.)
  • template ltclass Objectgt
  • ListItrltObjectgt ListltObjectgt
  • find(const Object x) const
  • int itr cursorSpaceheader.nextwhile
    (itr!0 cursorSpaceitr.element ! x)
  • itr cursorSpaceitr.next
  • return ListItrltObjectgt(itr)

13
Cursor Implementation (cont.)
  • template ltclass Objectgt
  • void ListltObjectgt
  • insert(const Object x, const ListItrltObjectgt p)
  • if (!p.isPastEnd())
  • int pos p.current
  • int tmp alloc()
  • cursorSpacetmp
  • CursorNode(x, cursorSpacepos.next)
  • cursorSpacepos.next tmp

14
Cursor Implementation (cont.)
  • template ltclass Objectgt
  • void ListltObjectgt
  • remove(const Object x)
  • ListItrltObjectgt p findPrevious(x)
  • int pos p.current
  • if (cursorSpacepos.next ! 0)
  • int tmp cursorSpacepos.next
  • cursorSpacepos.next cursorSpacetmp.n
    ext
  • free (tmp)

15
Comparing Performance
Write a Comment
User Comments (0)
About PowerShow.com