Stack and Array List Array Based Collections Collections
Stack and Array. List 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 pushing 5, 10, 20 capacity = 10 size = 3 elements = □ 0 1 2 3 4 5 6 7 8 9 5 10 20 ? ? ? ?
Constructor • Default capacity = 10 • Array of uninitialized values capacity = 10 size = 0 elements = □ 0 1 2 3 4 5 6 7 8 9 ? ? ? ? ? ?
Push • Pushing 15 – Size indicates next available slot capacity = 10 size = 3 elements = □ 0 1 2 3 4 5 6 7 8 9 5 10 20 ? ? ? ?
Push • Pushing 15 – Size indicates next available slot capacity = 10 size = 3 elements = □ 0 1 2 3 4 5 6 7 8 9 5 10 20 15 ? ? ? ?
Push • Pushing 15 – Size indicates next available slot capacity = 10 size = 4 elements = □ 0 1 2 3 4 5 6 7 8 9 5 10 20 15 ? ? ? ?
Growing • When run out of room allocate new array, copy delete old capacity = 5 size = 5 elements = □ 0 1 2 3 4 5
Growing • When run out of room allocate new array, copy delete old capacity = 5 size = 5 elements = □ old = □ 0 1 2 3 4 5
Growing • When run out of room allocate new array, copy delete old capacity = 10 size = 5 elements = □ old = □ 0 1 2 3 4 5 6 7 8 9 ? ? ? ? ? ?
Growing • When run out of room allocate new array, copy delete old capacity = 10 size = 5 elements = □ old = □ 0 1 2 3 4 5 6 7 8 9 1 2 3 4 5 ? ? ? ?
Growing • When run out of room allocate new array, copy delete old capacity = 10 size = 5 elements = □ old = □ 0 1 2 3 4 5 6 7 8 9 1 2 3 4 5 ? ? ? ?
Growing • When run out of room allocate new array, copy delete old capacity = 10 size = 5 elements = □ 0 1 2 3 4 5 6 7 8 9 1 2 3 4 5 ? ? ? ?
Peek • Peek – Size – 1 indicates "top" element capacity = 10 size = 4 elements = □ What can go wrong here? 0 1 2 3 4 5 6 7 8 9 5 10 20 15 ? ? ? ?
Pop • Pop – Predecrement happens before access – Old item not part of logical stack capacity = 10 size = 4 elements = □ 0 1 2 3 4 5 6 7 8 9 5 10 20 15 ? ? ? ?
Pop • Pop – Predecrement happens before access – Old item not part of logical stack capacity = 10 size = 3 elements = □ 0 1 2 3 4 5 6 7 8 9 5 10 20 15 ? ? ? ?
Usual Suspects • Manage memory so… – Copy Constructor – Destructor
Split • Split – Return new Stack with top half capacity = 10 size = 4 elements = □ 0 1 2 3 4 5 6 7 8 9 5 10 20 15 ? ? ? ?
Split • Split – Return new Stack with top half top capacity = 10 size = 0 elements = □ this capacity = 10 size = 4 elements = □ 0 1 2 3 4 5 6 7 8 9 5 10 20 15 ? ? ? ? 0 1 2 3 4 5 6 7 8 9 ? ? ? ? ? ?
Split • Split – Return new Stack with top half top capacity = 10 size = 0 elements = □ this capacity = 10 size = 4 elements = □ my. New. Size = 2 0 1 2 3 4 5 6 7 8 9 5 10 20 15 ? ? ? ? 0 1 2 3 4 5 6 7 8 9 ? ? ? ? ? ?
Split • Split – Return new Stack with top half top capacity = 10 size = 1 elements = □ this capacity = 10 size = 4 elements = □ my. New. Size = 2 0 1 2 3 4 5 6 7 8 9 5 10 20 15 ? ? ? ? 0 1 2 3 4 5 6 7 8 9 20 ? ? ? ? ? ?
Split • Split – Return new Stack with top half top capacity = 10 size = 2 elements = □ this capacity = 10 size = 4 elements = □ my. New. Size = 2 0 1 2 3 4 5 6 7 8 9 5 10 20 15 ? ? ? ? 0 1 2 3 4 5 6 7 8 9 20 15 ? ? ? ?
Split • Split – Return new Stack with top half top capacity = 10 size = 2 elements = □ this capacity = 10 size = 2 elements = □ my. New. Size = 2 0 1 2 3 4 5 6 7 8 9 5 10 20 15 ? ? ? ? 0 1 2 3 4 5 6 7 8 9 20 15 ? ? ? ?
Split • Split – Return new Stack with top half top. Part capacity = 10 size =2 elements = □ this capacity = 10 size = 2 elements = □ 0 1 2 3 4 5 6 7 8 9 5 10 20 15 ? ? ? ? 0 1 2 3 4 5 6 7 8 9 20 15 ? ? ? ?
Clear • Clear – Change logical size to 0 capacity = 10 size = 4 elements = □ 0 1 2 3 4 5 6 7 8 9 5 10 20 15 ? ? ? ?
Clear • Clear – Change logical size to 0 capacity = 10 size = 0 elements = □ 0 1 2 3 4 5 6 7 8 9 5 10 20 15 ? ? ? ?
Array. List • Array. List – 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 current. Size = 0 list = □ 0 1 2 3 4 5 6 7 8 9 ? ? ? ? ? ?
Code Tour • Append 'U' capacity = 10 current. Size = 1 list = □ 0 1 2 3 4 5 6 7 8 9 U ? ? ? ? ? ?
Code Tour • Insert 'Q' at 2 capacity = 10 current. Size = 4 list = □ 0 1 2 3 4 5 6 7 8 9 U C L A ? ? ? ?
Code Tour • Insert 'Q' at 2 capacity = 10 current. Size = 4 list = □ i=4 0 1 2 3 4 5 6 7 8 9 U C L A ? ? ? ?
Code Tour • Insert 'Q' at 2 capacity = 10 current. Size = 4 list = □ i=4 0 1 2 3 4 5 6 7 8 9 U C L A A ? ? ? ?
Code Tour • Insert 'Q' at 2 capacity = 10 current. Size = 4 list = □ i=3 0 1 2 3 4 5 6 7 8 9 U C L L A ? ? ? ?
Code Tour • Insert 'Q' at 2 capacity = 10 current. Size = 4 list = □ 0 1 2 3 4 5 6 7 8 9 U C Q L A ? ? ? ?
Code Tour • Insert 'Q' at 2 capacity = 10 current. Size = 5 list = □ 0 1 2 3 4 5 6 7 8 9 U C Q L A ? ? ? ?
Code Tour • Remove at 2 capacity = 10 current. Size = 5 list = □ 0 1 2 3 4 5 6 7 8 9 U C Q L A ? ? ? ?
Code Tour • Remove at 2 capacity = 10 current. Size = 5 list = □ i=2 0 1 2 3 4 5 6 7 8 9 U C Q L A ? ? ? ?
Code Tour • Remove at 2 capacity = 10 current. Size = 5 list = □ i=2 0 1 2 3 4 5 6 7 8 9 U C L L A ? ? ? ?
Code Tour • Remove at 2 capacity = 10 current. Size = 5 list = □ i=3 0 1 2 3 4 5 6 7 8 9 U C L A A ? ? ? ?
Code Tour • Remove at 2 capacity = 10 current. Size = 4 list = □ 0 1 2 3 4 5 6 7 8 9 U C L A A ? ? ? ?
Search • Search for 'L' – Linear search capacity = 10 current. Size = 4 list = □ How to rewrite this to have just one return statement? 0 1 2 3 4 5 6 7 8 9 U C L A ? ? ? ?
Sort • Selection Sort capacity = 10 current. Size = 4 list = □ i=0 0 1 2 3 4 5 6 7 8 9 U C A L ? ? ? ? current. Min = U current. Min. Index = 0
Sort • Selection Sort capacity = 10 current. Size = 4 list = □ i=0 j=1 0 1 2 3 4 5 6 7 8 9 U C A L ? ? ? ? current. Min = U current. Min. Index = 0
Sort • Selection Sort capacity = 10 current. Size = 4 list = □ i=0 j=1 0 1 2 3 4 5 6 7 8 9 U C A L ? ? ? ? current. Min = C current. Min. Index = 1
Sort • Selection Sort capacity = 10 current. Size = 4 list = □ i=0 j=2 0 1 2 3 4 5 6 7 8 9 U C A L ? ? ? ? current. Min = A current. Min. Index = 2
Sort • Selection Sort capacity = 10 current. Size = 4 list = □ i=0 j=3 0 1 2 3 4 5 6 7 8 9 U C A L ? ? ? ? current. Min = A current. Min. Index = 2
Sort • Selection Sort capacity = 10 current. Size = 4 list = □ i=0 0 1 2 3 4 5 6 7 8 9 U C U L ? ? ? ? current. Min = A current. Min. Index = 3
Sort • Selection Sort capacity = 10 current. Size = 4 list = □ i=0 0 1 2 3 4 5 6 7 8 9 A C U L ? ? ? ? current. Min = A current. Min. Index = 3
Sort • Selection Sort capacity = 10 current. Size = 4 list = □ i=1 0 1 2 3 4 5 6 7 8 9 A C U L ? ? ? ? current. Min = C current. Min. Index = 1
Sort • Selection Sort capacity = 10 current. Size = 4 list = □ i=1 j=2 0 1 2 3 4 5 6 7 8 9 A C U L ? ? ? ? current. Min = C current. Min. Index = 1
Sort • Selection Sort capacity = 10 current. Size = 4 list = □ i=1 j=3 0 1 2 3 4 5 6 7 8 9 A C U L ? ? ? ? current. Min = C current. Min. Index = 1
Sort • Selection Sort capacity = 10 current. Size = 4 list = □ i=1 0 1 2 3 4 5 6 7 8 9 A C U L ? ? ? ? current. Min = C current. Min. Index = 1
Sort • Selection Sort capacity = 10 current. Size = 4 list = □ i=2 0 1 2 3 4 5 6 7 8 9 A C U L ? ? ? ? current. Min = U current. Min. Index = 2
Sort • Selection Sort capacity = 10 current. Size = 4 list = □ i=2 j=3 0 1 2 3 4 5 6 7 8 9 A C U L ? ? ? ? current. Min = L current. Min. Index = 3
Sort • Selection Sort capacity = 10 current. Size = 4 list = □ i=2 0 1 2 3 4 5 6 7 8 9 A C L U ? ? ? ? current. Min = L current. Min. Index = 2
Sort • Selection Sort capacity = 10 current. Size = 4 list = □ i=2 0 1 2 3 4 5 6 7 8 9 A C L U ? ? ? ? current. Min = L current. Min. Index = 2
- Slides: 59