CMSC 341 - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

CMSC 341

Description:

CMSC 341 Lecture 2 ... cmsc 341 – PowerPoint PPT presentation

Number of Views:97
Avg rating:3.0/5.0
Slides: 15
Provided by: umb95
Category:

less

Transcript and Presenter's Notes

Title: CMSC 341


1
CMSC 341
2
List.H
  • template ltclass Objectgt
  • class List
  • public
  • List()
  • List(const List rhs)
  • List()
  • const List operator(const List rhs)
  • Bool isEmpty() const
  • void makeEmpty()
  • void remove (const Object x)
  • void insert (const Object x,
  • const ListIterltObjectgt p)

3
List.H (cont)
  • ListIterltObjectgt first()const
  • ListIterltObjectgt zeroth()const
  • ListIterltObjectgt find(const Object x)
  • ListIterltObjectgt findPrevious(const Object x)
  • private
  • ListNodeltObjectgt header

4
ListNode.H
  • template ltclass Objectgt
  • class ListNode
  • ListNode(const Object theElementObject(),
  • ListNode n NULL)
  • element(theElement), next(n)
  • Object element
  • ListNode next
  • friend class ListltObjectgt
  • friend class ListItrltObjectgt

5
ListItr.H
  • template ltclass Objectgt
  • class ListItr
  • public
  • ListItr()current(NULL)
  • bool isPastEnd() const return current NULL
  • void advance()
  • if (!isPastEnd()) current current-gtnext
  • const Object retrieve() const
  • if (isPastEnd()) throw BadIterator()
  • return current-gtelement
  • private
  • ListNodeltObjectgt current
  • ListItr(ListNodeltObjectgt theNode)current(theNod
    e)
  • friend class ListltObjectgt

6
Linked List Implementation
  • template ltclass Objectgt
  • ListltObjectgtList()
  • header new ListNodeltObjectgt
  • template ltclass Objectgt
  • ListltObjectgtisEmpty()
  • return !(header-gtnext)

7
Linked List Implementation (cont)
  • template ltclass Objectgt
  • ListItrltObjectgt ListltObjectgtzeroth()
  • return ListItrltObjectgt(header)
  • template ltclass Objectgt
  • ListItrltObjectgt ListltObjectgtfirst()
  • return ListItrltObjectgt(header-gtnext)

8
Linked List Implementation (cont)
  • template ltclass Objectgt
  • void ListltObjectgt
  • insert(const Object x, const ListItrltObjectgt p)
  • if (!p.isPastEnd()) //text uses p.current!NULL
  • p.current-gtnext
  • new ListNodeltObjectgt(x,p.current-gtnext)

9
Linked List Implementation (cont)
  • template ltclass Objectgt
  • ListItrltObjectgt ListltObjectgt
  • find(const Object x const)
  • ListNodeltObjectgt p header-gtnext
  • while (p!NULL p-gtelement !x)
  • p p-gtnext
  • return ListItrltObjectgt(p)

10
Linked List Implementation (cont)
  • template ltclass Objectgt
  • ListItrltObjectgt ListltObjectgt
  • findPrevious(const Object x const)
  • ListNodeltObjectgt p header
  • while (p-gtnext!NULL p-gtnext-gtelement !x)
  • p p-gtnext
  • return ListItrltObjectgt(p)

11
Linked List Implementation (cont)
  • template ltclass Objectgt
  • void ListltObjectgt
  • remove(const Object x)
  • ListItrltObjectgt pfindPrevious(x)
  • if (p.current-gtnext ! NULL)
  • ListNodeltObjectgt oldnode
  • p.current-gtnext
  • p.current-gtnext p.current-gtnext-gtnext
  • delete oldnode

12
Linked List Implementation (cont)
  • template ltclass Objectgt
  • void ListltObjectgtmakeEmpty()
  • while (!isEmpty()) remove (first().retrieve())

13
Linked List Implementation (cont)
  • template ltclass Objectgt
  • void ListltObjectgtList()
  • makeEmpty() //deletes all nodes except header
  • delete header

14
Linked List Implementation (cont)
  • template ltclass Objectgt
  • const ListltObjectgt ListltObjectgt
  • operator (const ListltObjectgt rhs)
  • if (this ! rhs)
  • makeEmpty()
  • ListItrltObjectgt ritr rhs.first()
  • ListItrltObjectgt itr zeroth()
  • while (!ritr.isPastEnd())
  • insert(ritr.retrieve(), itr)
  • ritr.advance()
  • itr.advance()
  • return this
Write a Comment
User Comments (0)
About PowerShow.com