A reference to an array what will be the type of the

This preview shows page 13 - 24 out of 101 pages.

A reference to an array. What will be the type of the references of this array? Object! Or some parameter if generics are used. What will be your strategy for adding elements to the data structure?
Image of page 13

Subscribe to view the full document.

Implementing a Stack using an array: ArrayStack elems s 0 1 2 3 4 5 6 7
Image of page 14
Implementing a Stack using an array: ArrayStack Elements can be stored in the low part or high part of the array. Let’s select the low part of the array, the solution for the high part will be symmetrical. How will the method push know where to insert the next element?
Image of page 15

Subscribe to view the full document.

Implementing a Stack using an array: ArrayStack elems s 0 1 2 3 4 5 6 7
Image of page 16
Implementing a Stack using an array: ArrayStack How will the method push know where to insert the next element? A new instance variable is needed. Is it top or bottom? Is the top position fixed or is it the bottom? In which direction will the stack be growing? Where will the first element, second element, third element, etc. be stored?
Image of page 17

Subscribe to view the full document.

Implementing a Stack using an array: ArrayStack elems bottom "charlie" "bravo" "alpha" 3 s
Image of page 18
Implementing a Stack using an array: ArrayStack elems bottom "charlie" "bravo" "alpha" 3 s
Image of page 19

Subscribe to view the full document.

Implementing a Stack using an array: ArrayStack elems bottom "charlie" "bravo" "alpha" 3 s "delta"
Image of page 20
Implementing a Stack using an array: ArrayStack Comment the following implementation: The elements of this stack are stored in the low part of the array, the top element is always located at position 0 (convention), an instance variable is used to indicate the position of the bottom element in the array. For each element pushed onto the stack, all the elements must be moved one position up in the array, so that the top element is always at position 0. For each element removed, all the elements must be moved one position down in the array.
Image of page 21

Subscribe to view the full document.

Implementing a Stack using an array: ArrayStack Comment the following implementation: The elements of this stack are stored in the low part of the array, the bottom element is always located at position 0 (convention), an instance variable is used to indicate the position of the top element in the array. Each time an element is pushed onto the stack, the value of the index top is incremented by one, the new element is added at that position. Each time an element is removed from the stack, the reference of the top element is stored in a temporary variable, the position of the array designated by top is set to null , the index is decremented by one, the element saved is returned. This is the preferred solution. It avoids copying the elements up or down for each insertion or removal. Copying elements would become progressively more expensive as the number of elements in the stack increases.
Image of page 22
Implementing a Stack using an array: ArrayStack Summary. this implementations requires a reference to an array, an array, as well as a top index.
Image of page 23

Subscribe to view the full document.

Image of page 24
You've reached the end of this preview.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern