Presentation is loading. Please wait.

Presentation is loading. Please wait.

Array Based Collections

Similar presentations


Presentation on theme: "Array Based Collections"— Presentation transcript:

1 Array Based Collections
Stack and ArrayList Array Based Collections

2 Collections Arrays Collections Primitive structured storage
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 pushing 5, 10, 20 capacity = 10
size = 3 elements = □ 1 2 3 4 5 6 7 8 9 10 20 ??

6 Constructor Default capacity = 10 Array of uninitialized values
size = 0 elements = □ 1 2 3 4 5 6 7 8 9 ??

7 Push Pushing 15 Size indicates next available slot capacity = 10
size = 3 elements = □ 1 2 3 4 5 6 7 8 9 10 20 ??

8 Push Pushing 15 Size indicates next available slot capacity = 10
size = 3 elements = □ 1 2 3 4 5 6 7 8 9 10 20 15 ??

9 Push Pushing 15 Size indicates next available slot capacity = 10
size = 4 elements = □ 1 2 3 4 5 6 7 8 9 10 20 15 ??

10 Growing When run out of room allocate new array, copy delete old
capacity = 5 size = 5 elements = □ 1 2 3 4 5

11 Growing When run out of room allocate new array, copy delete old
capacity = 5 size = 5 elements = □ old = □ 1 2 3 4 5

12 Growing When run out of room allocate new array, copy delete old
capacity = 10 size = 5 elements = □ old = □ 1 2 3 4 5 1 2 3 4 5 6 7 8 9 ??

13 Growing When run out of room allocate new array, copy delete old
capacity = 10 size = 5 elements = □ old = □ 1 2 3 4 5 1 2 3 4 5 6 7 8 9 ??

14 Growing When run out of room allocate new array, copy delete old
capacity = 10 size = 5 elements = □ old = □ 1 2 3 4 5 6 7 8 9 ??

15 Growing When run out of room allocate new array, copy delete old
capacity = 10 size = 5 elements = □ 1 2 3 4 5 6 7 8 9 ??

16 Peek Peek Size – 1 indicates "top" element capacity = 10
size = 4 elements = □ 1 2 3 4 5 6 7 8 9 10 20 15 ??

17 Pop Pop Predecrement happens before access
Old item not part of logical stack capacity = 10 size = 4 elements = □ 1 2 3 4 5 6 7 8 9 10 20 15 ??

18 Pop Pop Predecrement happens before access
Old item not part of logical stack capacity = 10 size = 3 elements = □ 1 2 3 4 5 6 7 8 9 10 20 15 ??

19 Usual Suspects Manage memory so… Copy Constructor Destructor

20 Split Split Return new Stack with top half capacity = 10
size = 4 elements = □ 1 2 3 4 5 6 7 8 9 10 20 15 ??

21 Split Split Return new Stack with top half top this capacity = 10
size = 0 elements = □ capacity = 10 size = 4 elements = □ 1 2 3 4 5 6 7 8 9 10 20 15 ?? 1 2 3 4 5 6 7 8 9 ??

22 Split Split Return new Stack with top half top this capacity = 10
size = 0 elements = □ capacity = 10 size = 4 elements = □ myNewSize = 2 1 2 3 4 5 6 7 8 9 10 20 15 ?? 1 2 3 4 5 6 7 8 9 ??

23 Split Split Return new Stack with top half top this capacity = 10
size = 1 elements = □ capacity = 10 size = 4 elements = □ myNewSize = 2 1 2 3 4 5 6 7 8 9 10 20 15 ?? 1 2 3 4 5 6 7 8 9 20 ??

24 Split Split Return new Stack with top half top this capacity = 10
size = 2 elements = □ capacity = 10 size = 4 elements = □ myNewSize = 2 1 2 3 4 5 6 7 8 9 10 20 15 ?? 1 2 3 4 5 6 7 8 9 20 15 ??

25 Split Split Return new Stack with top half top this capacity = 10
size = 2 elements = □ capacity = 10 size = 2 elements = □ myNewSize = 2 1 2 3 4 5 6 7 8 9 10 20 15 ?? 1 2 3 4 5 6 7 8 9 20 15 ??

26 Split Split Return new Stack with top half topPart this capacity = 10
size = 0 elements = □ capacity = 10 size = 2 elements = □ 1 2 3 4 5 6 7 8 9 10 20 15 ?? 1 2 3 4 5 6 7 8 9 20 15 ??

27 Clear Clear Change logical size to 0 capacity = 10
size = 4 elements = □ 1 2 3 4 5 6 7 8 9 10 20 15 ??

28 Clear Clear Change logical size to 0 capacity = 10
size = 0 elements = □ 1 2 3 4 5 6 7 8 9 10 20 15 ??

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 = 0 list = □ 1 2 3 4 5
1 2 3 4 5 6 7 8 9 ??

32 Code Tour Append 'U' capacity = 10 currentSize = 1 list = □ 1 2 3 4 5
1 2 3 4 5 6 7 8 9 U ??

33 Code Tour Insert 'Q' at 2 capacity = 10 currentSize = 4 list = □ 1 2 3
1 2 3 4 5 6 7 8 9 U C L A ??

34 Code Tour Insert 'Q' at 2 capacity = 10 currentSize = 4 list = □ i = 4
1 2 3 4 5 6 7 8 9 U C L A ??

35 Code Tour Insert 'Q' at 2 capacity = 10 currentSize = 4 list = □ i = 4
1 2 3 4 5 6 7 8 9 U C L A ??

36 Code Tour Insert 'Q' at 2 capacity = 10 currentSize = 4 list = □ i = 3
1 2 3 4 5 6 7 8 9 U C L A ??

37 Code Tour Insert 'Q' at 2 capacity = 10 currentSize = 4 list = □ 1 2 3
1 2 3 4 5 6 7 8 9 U C Q L A ??

38 Code Tour Insert 'Q' at 2 capacity = 10 currentSize = 5 list = □ 1 2 3
1 2 3 4 5 6 7 8 9 U C Q L A ??

39 Code Tour Remove at 2 capacity = 10 currentSize = 5 list = □ 1 2 3 4 5
1 2 3 4 5 6 7 8 9 U C Q L A ??

40 Code Tour Remove at 2 capacity = 10 currentSize = 5 list = □ i = 2 1 2
1 2 3 4 5 6 7 8 9 U C Q L A ??

41 Code Tour Remove at 2 capacity = 10 currentSize = 5 list = □ i = 2 1 2
1 2 3 4 5 6 7 8 9 U C L A ??

42 Code Tour Remove at 2 capacity = 10 currentSize = 5 list = □ i = 3 1 2
1 2 3 4 5 6 7 8 9 U C L A ??

43 Code Tour Remove at 2 capacity = 10 currentSize = 4 list = □ 1 2 3 4 5
1 2 3 4 5 6 7 8 9 U C L A ??

44 Search Search for 'L' Linear search capacity = 10
currentSize = 4 list = □ 1 2 3 4 5 6 7 8 9 U C L A ??

45 Sort Selection Sort capacity = 10 currentSize = 4 list = □ i = 0 1 2 3
1 2 3 4 5 6 7 8 9 U C A L ?? currentMin = U currentMinIndex = 0

46 Sort Selection Sort capacity = 10 currentSize = 4 list = □ i = 0 j = 1
1 2 3 4 5 6 7 8 9 U C A L ?? currentMin = U currentMinIndex = 0

47 Sort Selection Sort capacity = 10 currentSize = 4 list = □ i = 0 j = 1
1 2 3 4 5 6 7 8 9 U C A L ?? currentMin = C currentMinIndex = 1

48 Sort Selection Sort capacity = 10 currentSize = 4 list = □ i = 0 j = 2
1 2 3 4 5 6 7 8 9 U C A L ?? currentMin = A currentMinIndex = 2

49 Sort Selection Sort capacity = 10 currentSize = 4 list = □ i = 0 j = 3
1 2 3 4 5 6 7 8 9 U C A L ?? currentMin = A currentMinIndex = 2

50 Sort Selection Sort capacity = 10 currentSize = 4 list = □ i = 0 1 2 3
1 2 3 4 5 6 7 8 9 U C L ?? currentMin = A currentMinIndex = 3

51 Sort Selection Sort capacity = 10 currentSize = 4 list = □ i = 0 1 2 3
1 2 3 4 5 6 7 8 9 A C U L ?? currentMin = A currentMinIndex = 3

52 Sort Selection Sort capacity = 10 currentSize = 4 list = □ i = 1 1 2 3
1 2 3 4 5 6 7 8 9 A C U L ?? currentMin = C currentMinIndex = 1

53 Sort Selection Sort capacity = 10 currentSize = 4 list = □ i = 1 j = 2
1 2 3 4 5 6 7 8 9 A C U L ?? currentMin = C currentMinIndex = 1

54 Sort Selection Sort capacity = 10 currentSize = 4 list = □ i = 1 j = 3
1 2 3 4 5 6 7 8 9 A C U L ?? currentMin = C currentMinIndex = 1

55 Sort Selection Sort capacity = 10 currentSize = 4 list = □ i = 1 1 2 3
1 2 3 4 5 6 7 8 9 A C U L ?? currentMin = C currentMinIndex = 1

56 Sort Selection Sort capacity = 10 currentSize = 4 list = □ i = 2 1 2 3
1 2 3 4 5 6 7 8 9 A C U L ?? currentMin = U currentMinIndex = 2

57 Sort Selection Sort capacity = 10 currentSize = 4 list = □ i = 2 j = 3
1 2 3 4 5 6 7 8 9 A C U L ?? currentMin = L currentMinIndex = 3

58 Sort Selection Sort capacity = 10 currentSize = 4 list = □ i = 2 1 2 3
1 2 3 4 5 6 7 8 9 A C L U ?? currentMin = L currentMinIndex = 2

59 Sort Selection Sort capacity = 10 currentSize = 4 list = □ i = 2 1 2 3
1 2 3 4 5 6 7 8 9 A C L U ?? currentMin = L currentMinIndex = 2


Download ppt "Array Based Collections"

Similar presentations


Ads by Google