One array but two distinct instance variables for the

This preview shows page 87 - 98 out of 101 pages.

One array but two distinct instance variables for the two tops:
Image of page 87

Subscribe to view the full document.

6 7 0 1 2 3 4 5 8 9 B D E C A B C topHi = 1 topLo = 5 elems Why? Memory management works like that. Such implementation may reduce the amount of memory that is wasted.
Image of page 88
Primitive vs reference 6 7 0 1 2 3 4 5 8 9 A B C D E top = 4 elems elems 6 7 0 1 2 3 4 5 8 9 exemple de pile un top = 3
Image of page 89

Subscribe to view the full document.

Earlier we said that pop consists of: 1. saving the current top value, 2. reset stack[top], 3. decrement top, 4. return the saved value. Is it necessary to set the top value to null?
Image of page 90
6 7 0 1 2 3 4 5 8 9 A B C D E top = 3 elems elems 6 7 0 1 2 3 4 5 8 9 exemple de pile un top = 2 Since a reference from the “stack” to the object exists, this prevents the garbage collector from doing its job.
Image of page 91

Subscribe to view the full document.

6 7 0 1 2 3 4 5 8 9 A B C D top = 3 elems elems 6 7 0 1 2 3 4 5 8 9 exemple de pile un top = 2 No reference to the object, therefore gc() can do its job.
Image of page 92
Note: error condition Pre- and post-conditions should be checked and the appropriate Exceptions should be thrown. if (! s.empty()) v = s.pop(); ... if (! s.isFull()) s.push(v);
Image of page 93

Subscribe to view the full document.

Properties of the arrays Arrays are accessed by index position, e.g. a[3] designates the fourth position of the array. Access to a position is very fast. We say that indexing is a random access operation in the case of the arrays, which means that the access to any element of an array always takes a constant number of steps, we say the operation necessitates constant time , i.e. the time to access an element is independent of: The size of the array; The number of elements that are in the array; The position of the element that we wish to access (first, last, middle).
Image of page 94
Access to any element of an array is fast because the elements of an array are stored contiguously in memory. The array starts at some address of the memory, let’s call this the base address, then the first element is stored at this address and the location of the next element depends on the size of an element. All the elements of an array occupy the same amount of space and therefore the address of any element is simply, base address + offset where the offset is index * size of an element The first element ( index = 0 ) is found at the base address, the second at the base address plus the size of one element, and so on. No search involved.
Image of page 95

Subscribe to view the full document.

Fixed size arrays What if the size of an array is not known? Suppose, you were asked to read positive integers from the input until a special value is read (sentinel), say -9, and the values should be stored in a array. How large should you declare this array? 1 ? 1 Certain programming languages, such as Fortran and Pascal, require you to specify the size of the array at compile time.
Image of page 96
Solution 1: make it large enough A possible solution would be to create an array that would be suitable for even the largest application.
Image of page 97

Subscribe to view the full document.

Image of page 98
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