简体   繁体   中英

CS, Java: How an array implemented stack data structure can grow (upwards, downwards)? analyze

Lets look at two real-world examples

Model 1: Consider a eat self-service shop where you grab a plate; the plates are stored below the surface of a table, imagine a horizontal line represent the counter top and a spring pushing the plates up. As plates are removed from the top of the stack, the spring below causes the next plate to move to the top and all the plates below move up one position

Model 2: Consider a stack of books on a desk; imagine a horizontal line represents the desktop. In this case when a book is removed from the stack, the rest of the stack does not move as it does in the plate example.

Recently i receive a question about analyzing the differences between the two aforementioned models in implementing trough Java array a stack data structure. I responded but wonder if missed something in my response

PS: I guess the question can be useful to others too

Best Regards

Harry GT Kar

Model 1: Imagine that i have two arrows top and bottom indicating the very top and bottom elements of the stack. So the variables top and bottom contain the array index of the top and bottom elements, respectively.

When plates are removed from the top of the stack note that the pointer to the top plate has not moved, but rather only the pointer to the bottom has moved.

Lets translate all that in a six-element array containing four integers

[0]     7       0   top

[1]     11

[2]     18      

[3]     23      3   bottom

[4]

[5]

[6]
  • Now if the top element was removed, the integer 11 would have to be copied from the first position to the zeroth position, then the integer 18 could be copied from the second position to the first position, and so on. Lastly, the variable bottom would need to change from 3 to 2.

  • Regard the last element in pile, in actuality a copy of the integer 23 would still be in position 3 of the array since it was merely copied from one array element position to another. However, it is the variable bottom now at 2 which indicates the bottom of the stack, not the number of items in the array.

  • Note that in removing all the items from the stack, bottom would be a −1 and top would remain at a 0.

CONS:

What if instead of just four items in the stack, here was a 100 element array with 100 items in the stack? To remove just one item in the stack, it would need to copy 99 items.

To extrapolate, if there were n items in the stack, then n − 1 items would need to be moved. This is a O(n) process . That mean clearly as the number of items increases, the efficiency of this design is lacking. Even though the above seems to work fairly well for a small number of items, one of the things that must be considered when designing a data structure is how it works with a large number of items.

Model 2: In this case when a book is removed from the stack, top moves down one place (as opposed to the bottom moving up as in Model 1) and bottom is not moved

Lets represented this model using an array:

[6]

[5]

[4]     

[3]     7       3   top     

[2]     11

[1]     18

[0]     23      0   bottom
  • First, note that typically an array would be drawn with the zeroth element on the top and subsequent elements following below as was done in the previous example, but the above array has the zeroth element at the bottom instead. The reason for this is that when using an array to represent a stack it is usually more convenient to have the zeroth element drawn at the bottom, so that when items are added, they are added to the next higher number index and it looks more like a physical stack.

  • Does this affect processing on the computer? No, since how it is drawn has no effect on the processing. Drawing the array this way is only more convenient for viewing by the programmer.

  • Also, notice that instead of having top at 0 and bottom at 3, these numbers are also reversed in this example.

  • When the top number, 7, is removed from the stack, the value of top will change from 3 to 2. Actually 7 has been popped off the stack, but the integer 7 is still in element 3. The item was not technically removed from the stack, but rather only top was decremented.

  • Since top is now at 2, that is what defines the top of the stack data structure, not what is in the array.

PROS

The advantage of this model is that whether there are four, one hundred, or one thousand items in the stack, none of the items needs to be copied. So, if there were n items in the stack, the process would not be of O(n), but rather a constant O(1).

PS: Here's is an important point concerning the distinction between an array and a stack as a data structure: The array merely implements the data structure, but it is a combination of all the variables and the algorithm that makes up a data structure.

Source: mainly from

JT Streib and T. Soma, Guide to Data Structures

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM