CS 261 Fall 2006 - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

CS 261 Fall 2006

Description:

Since we don't care where it is added (unlike stack or queue), we can ... Struct dlink * lnk; For (lnk = lst- firstLink; lnk != lst- Sentinel; lnk = lnk- next) ... – PowerPoint PPT presentation

Number of Views:46
Avg rating:3.0/5.0
Slides: 14
Provided by: osue6
Category:
Tags: dlink | fall

less

Transcript and Presenter's Notes

Title: CS 261 Fall 2006


1
CS 261 - Fall 2006
  • Intro to ADT Bag

2
What is a Bag?
  • A simple collection, allowing add, removal, and
    searching
  • Add (newElement)
  • Contains (testElement)
  • Remove (testElement)
  • Size ()
  • Iterator () / more on this on Friday /

3
Adding is easy
  • Add is an easy operation
  • Since we dont care where it is added (unlike
    stack or queue), we can simply use whatever code
    we have previously defined
  • That is, we have already solved this problem

4
Contains test
  • Testing to see if an element is contained is also
    pretty easy
  • Simply loop over the collection, testing each
    element.
  • What is the complexity of this operation?
  • (Later, we will see better solutions)

5
What does equality mean?
  • Just as we used EleType to represent an unknown
    element type - we need a general way to represent
    testing equality.
  • ifndef EQ
  • define EQ(a, b) (a b)
  • endif

6
What does a loop look like?
  • What does a loop look like for a dynamic array?
  • What does a loop look like for a linked list?
    (in part, depends upon whether we have sentinels
    or not)

7
Dynamic Array Loop
  • dyArray d
  • Int i
  • For (i 0 i lt d-gtsize i)
  • if (EQ(testElement, d-gtdatai))

8
What does a list loop look like?
  • Struct list lst
  • Struct dlink lnk
  • For (lnk lst-gtfirstLink
  • lnk ! lst-gtSentinel lnk lnk-gtnext)
  • if (EQ(testElement, lnk-gtvalue))

9
Remove
  • Remove requires removing elements from middle of
    collection
  • For dynamic array, need to move values down
  • For linked list, need to remove link

10
Dynamic Array Remove
11
Remove
  • Need to write a loop to move elements down.
  • What is the complexity of this operation?

12
Linked List Remove
  • Already solved this problem - wrote removeLink
  • What is the complexity of removing the link?
  • What is the complexity of finding the element you
    want to remove?

13
Your turn
  • Questions?
  • Complete worksheets that show how to make a bag
    out of a dynamic array
  • And how to make a bag out of a linked list.
Write a Comment
User Comments (0)
About PowerShow.com