1 / 59

Stack and ArrayList

Stack and ArrayList. Array Based Collections. Collections. Arrays Primitive structured storage Collections Higher level storage structures Lists Stacks/Queues Sets Trees Graphs…. Stack. Collection where we can only work with "top" Peek() : get value on top

inunez
Download Presentation

Stack and ArrayList

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Stack and ArrayList Array Based Collections

  2. Collections • Arrays • Primitive structured storage • Collections • Higher level storage structures • Lists • Stacks/Queues • Sets • Trees • Graphs…

  3. Stack • Collection where we can only work with "top" • Peek() : get value on top • Pop() : remove value on top • Push(value) : put new value on top

  4. Stack • LIFO : Last in First Out

  5. Internal Structure • Stack after pushing5, 10, 20 capacity = 10 size = 3elements = □

  6. Constructor • Default capacity = 10 • Array of uninitialized values capacity = 10 size = 0elements = □

  7. Push • Pushing 15 • Size indicates next available slot capacity = 10 size = 3elements = □

  8. Push • Pushing 15 • Size indicates next available slot capacity = 10 size = 3elements = □

  9. Push • Pushing 15 • Size indicates next available slot capacity = 10 size = 4elements = □

  10. Growing • When run out of roomallocate new array, copydelete old capacity = 5 size = 5elements = □

  11. Growing • When run out of roomallocate new array, copydelete old capacity = 5 size = 5elements = □ old = □

  12. Growing • When run out of roomallocate new array, copydelete old capacity = 10 size = 5elements = □ old = □

  13. Growing • When run out of roomallocate new array, copydelete old capacity = 10 size = 5elements = □ old = □

  14. Growing • When run out of roomallocate new array, copydelete old capacity = 10 size = 5elements = □ old = □

  15. Growing • When run out of roomallocate new array, copydelete old capacity = 10 size = 5elements = □

  16. Peek • Peek • Size – 1 indicates "top" element capacity = 10 size = 4elements = □ What can go wrong here?

  17. Pop • Pop • Predecrement happens before access • Old item not part of logical stack capacity = 10 size = 4elements = □

  18. Pop • Pop • Predecrement happens before access • Old item not part of logical stack capacity = 10 size = 3elements = □

  19. Usual Suspects • Manage memory so… • Copy Constructor • Destructor

  20. Split • Split • Return new Stackwith top half capacity = 10 size = 4elements = □

  21. Split • Split • Return new Stackwith top half top capacity = 10 size = 0elements = □ this capacity = 10 size = 4elements = □

  22. Split • Split • Return new Stackwith top half top capacity = 10 size = 0elements = □ this capacity = 10 size = 4elements = □ myNewSize = 2

  23. Split • Split • Return new Stackwith top half top capacity = 10 size = 1elements = □ this capacity = 10 size = 4elements = □ myNewSize = 2

  24. Split • Split • Return new Stackwith top half top capacity = 10 size = 2elements = □ this capacity = 10 size = 4elements = □ myNewSize = 2

  25. Split • Split • Return new Stackwith top half top capacity = 10 size = 2elements = □ this capacity = 10 size = 2elements = □ myNewSize = 2

  26. Split • Split • Return new Stackwith top half topPart capacity = 10 size =2elements = □ this capacity = 10 size = 2elements = □

  27. Clear • Clear • Change logical sizeto 0 capacity = 10 size = 4elements = □

  28. Clear • Clear • Change logical sizeto 0 capacity = 10 size = 0elements = □

  29. ArrayList • ArrayList • Acts like a list • Implemented with array

  30. Code Tour • Same structure as Stack • Pointer to dynamic array • Maintain logical/maximum size

  31. Code Tour • Constructor capacity = 10 currentSize = 0list = □

  32. Code Tour • Append 'U' capacity = 10 currentSize = 1list = □

  33. Code Tour • Insert 'Q' at 2 capacity = 10 currentSize = 4list = □

  34. Code Tour • Insert 'Q' at 2 capacity = 10 currentSize = 4list = □ i = 4

  35. Code Tour • Insert 'Q' at 2 capacity = 10 currentSize = 4list = □ i = 4

  36. Code Tour • Insert 'Q' at 2 capacity = 10 currentSize = 4list = □ i = 3

  37. Code Tour • Insert 'Q' at 2 capacity = 10 currentSize = 4list = □

  38. Code Tour • Insert 'Q' at 2 capacity = 10 currentSize = 5list = □

  39. Code Tour • Remove at 2 capacity = 10 currentSize = 5list = □

  40. Code Tour • Remove at 2 capacity = 10 currentSize = 5list = □ i = 2

  41. Code Tour • Remove at 2 capacity = 10 currentSize = 5list = □ i = 2

  42. Code Tour • Remove at 2 capacity = 10 currentSize = 5list = □ i = 3

  43. Code Tour • Remove at 2 capacity = 10 currentSize = 4list = □

  44. Search • Search for 'L' • Linear search capacity = 10 currentSize = 4list = □ How to rewrite this to have just one return statement?

  45. Sort • Selection Sort capacity = 10 currentSize = 4list = □ i = 0 currentMin = U currentMinIndex = 0

  46. Sort • Selection Sort capacity = 10 currentSize = 4list = □ i = 0 j = 1 currentMin = U currentMinIndex = 0

  47. Sort • Selection Sort capacity = 10 currentSize = 4list = □ i = 0 j = 1 currentMin = C currentMinIndex = 1

  48. Sort • Selection Sort capacity = 10 currentSize = 4list = □ i = 0 j = 2 currentMin = A currentMinIndex = 2

  49. Sort • Selection Sort capacity = 10 currentSize = 4list = □ i = 0 j = 3 currentMin = A currentMinIndex = 2

  50. Sort • Selection Sort capacity = 10 currentSize = 4list = □ i = 0 currentMin = A currentMinIndex = 3

More Related