590 likes | 610 Views
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
E N D
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 • Pop() : remove value on top • Push(value) : put new value on top
Stack • LIFO : Last in First Out
Internal Structure • Stack after pushing5, 10, 20 capacity = 10 size = 3elements = □
Constructor • Default capacity = 10 • Array of uninitialized values capacity = 10 size = 0elements = □
Push • Pushing 15 • Size indicates next available slot capacity = 10 size = 3elements = □
Push • Pushing 15 • Size indicates next available slot capacity = 10 size = 3elements = □
Push • Pushing 15 • Size indicates next available slot capacity = 10 size = 4elements = □
Growing • When run out of roomallocate new array, copydelete old capacity = 5 size = 5elements = □
Growing • When run out of roomallocate new array, copydelete old capacity = 5 size = 5elements = □ old = □
Growing • When run out of roomallocate new array, copydelete old capacity = 10 size = 5elements = □ old = □
Growing • When run out of roomallocate new array, copydelete old capacity = 10 size = 5elements = □ old = □
Growing • When run out of roomallocate new array, copydelete old capacity = 10 size = 5elements = □ old = □
Growing • When run out of roomallocate new array, copydelete old capacity = 10 size = 5elements = □
Peek • Peek • Size – 1 indicates "top" element capacity = 10 size = 4elements = □ What can go wrong here?
Pop • Pop • Predecrement happens before access • Old item not part of logical stack capacity = 10 size = 4elements = □
Pop • Pop • Predecrement happens before access • Old item not part of logical stack capacity = 10 size = 3elements = □
Usual Suspects • Manage memory so… • Copy Constructor • Destructor
Split • Split • Return new Stackwith top half capacity = 10 size = 4elements = □
Split • Split • Return new Stackwith top half top capacity = 10 size = 0elements = □ this capacity = 10 size = 4elements = □
Split • Split • Return new Stackwith top half top capacity = 10 size = 0elements = □ this capacity = 10 size = 4elements = □ myNewSize = 2
Split • Split • Return new Stackwith top half top capacity = 10 size = 1elements = □ this capacity = 10 size = 4elements = □ myNewSize = 2
Split • Split • Return new Stackwith top half top capacity = 10 size = 2elements = □ this capacity = 10 size = 4elements = □ myNewSize = 2
Split • Split • Return new Stackwith top half top capacity = 10 size = 2elements = □ this capacity = 10 size = 2elements = □ myNewSize = 2
Split • Split • Return new Stackwith top half topPart capacity = 10 size =2elements = □ this capacity = 10 size = 2elements = □
Clear • Clear • Change logical sizeto 0 capacity = 10 size = 4elements = □
Clear • Clear • Change logical sizeto 0 capacity = 10 size = 0elements = □
ArrayList • ArrayList • Acts like a list • Implemented with array
Code Tour • Same structure as Stack • Pointer to dynamic array • Maintain logical/maximum size
Code Tour • Constructor capacity = 10 currentSize = 0list = □
Code Tour • Append 'U' capacity = 10 currentSize = 1list = □
Code Tour • Insert 'Q' at 2 capacity = 10 currentSize = 4list = □
Code Tour • Insert 'Q' at 2 capacity = 10 currentSize = 4list = □ i = 4
Code Tour • Insert 'Q' at 2 capacity = 10 currentSize = 4list = □ i = 4
Code Tour • Insert 'Q' at 2 capacity = 10 currentSize = 4list = □ i = 3
Code Tour • Insert 'Q' at 2 capacity = 10 currentSize = 4list = □
Code Tour • Insert 'Q' at 2 capacity = 10 currentSize = 5list = □
Code Tour • Remove at 2 capacity = 10 currentSize = 5list = □
Code Tour • Remove at 2 capacity = 10 currentSize = 5list = □ i = 2
Code Tour • Remove at 2 capacity = 10 currentSize = 5list = □ i = 2
Code Tour • Remove at 2 capacity = 10 currentSize = 5list = □ i = 3
Code Tour • Remove at 2 capacity = 10 currentSize = 4list = □
Search • Search for 'L' • Linear search capacity = 10 currentSize = 4list = □ How to rewrite this to have just one return statement?
Sort • Selection Sort capacity = 10 currentSize = 4list = □ i = 0 currentMin = U currentMinIndex = 0
Sort • Selection Sort capacity = 10 currentSize = 4list = □ i = 0 j = 1 currentMin = U currentMinIndex = 0
Sort • Selection Sort capacity = 10 currentSize = 4list = □ i = 0 j = 1 currentMin = C currentMinIndex = 1
Sort • Selection Sort capacity = 10 currentSize = 4list = □ i = 0 j = 2 currentMin = A currentMinIndex = 2
Sort • Selection Sort capacity = 10 currentSize = 4list = □ i = 0 j = 3 currentMin = A currentMinIndex = 2
Sort • Selection Sort capacity = 10 currentSize = 4list = □ i = 0 currentMin = A currentMinIndex = 3