MHB Function Pop for a static stack

  • Thread starter Thread starter evinda
  • Start date Start date
  • Tags Tags
    Function Static
AI Thread Summary
The discussion revolves around the implementation of the Push operation for a static stack using a one-dimensional array. The key point is the handling of the stack's length and the conditions for adding a new element. The initial check ensures that the stack is not full by verifying if the current length equals the maximum size, N. If the stack is not full, the length is incremented, and the new element is added at the index corresponding to the current length minus one. Participants clarify that the new element is placed at the position indicated by the current length before it is incremented. They emphasize that this logic holds true as long as the stack's length is less than N, ensuring that the new element can be added without exceeding the array's bounds. The discussion concludes that if the stack starts empty, any new element will be placed at an index less than or equal to N-1, maintaining the integrity of the stack's structure.
evinda
Gold Member
MHB
Messages
3,741
Reaction score
0
Hello! (Smile)

A static stack is implemented with the use of a one-dimensional array $A$.
Suppose that we have a pointer $S$ to a struct that has two fields, the array $A$ and the integer Length, and represents a stack.

According to my notes, the operation [m] Push [/m] for a static stack is the following:

Code:
void Push(Pointer S, Type x)
  if (S->Length==N) then error;
  else {
         S->Length=S->Length+1;
         (S->A)[S->Length-1]=x;
}
But don't we have to check also if [m]S->Length+1==N[/m] ? Or am I wrong?
 
Technology news on Phys.org
Hey! (Wave)

What is the reason that we would have to check [m]S->Length+1==N[/m] ? (Wondering)

The first check is to see if the stack is full. If it isn't, we can add the new element.
 
I like Serena said:
Hey! (Wave)

What is the reason that we would have to check [m]S->Length+1==N[/m] ? (Wondering)

The first check is to see if the stack is full. If it isn't, we can add the new element.

I thought so because we check if the stack is full for a given [m] Length [/m] and then we increment the [m] Length [/m] and after that we put a new element.

So it will be for example like that:

View attachment 3865

Right? So we check if [m] Length==N [/m] because that will be the position at which we will place the new element, right? (Thinking)
 

Attachments

  • examp.png
    examp.png
    3 KB · Views: 108
I believe the new element is effectively placed at N-1. (Thinking)

When yet another element is pushed, we find that Length == N, and no other element will fit. (Wasntme)
 
I like Serena said:
I believe the new element is effectively placed at N-1. (Thinking)

Will the new element not be placed at the postion [m]S->length[/m] before we change it? Or am I wrong? (Worried)
 
evinda said:
Will the new element not be placed at the postion [m]S->length[/m] before we change it? Or am I wrong? (Worried)

Let's take a close look at your example. (Thinking)

Initially we have N=6 and S->Length=5.

[table="width: 500"]
[tr]
[td]Statement[/td]
[td]Assertion[/td]
[/tr]
[tr]
[td][m]if (S->Length==N) then error;[/m][/td]
[td]No error[/td]
[/tr]
[tr]
[td][m]else {[/m][/td]
[td]S->Length == N -1[/td]
[/tr]
[tr]
[td]
[m]S->Length=S->Length+1;[/m]​
[/td]
[td]S->Length == N[/td]
[/tr]
[tr]
[td]
[m](S->A)[S->Length-1]=x;[/m]​
[/td]
[td]S->A[N-1] == x[/td]
[/tr]
[tr]
[td][m]}[/m][/td]
[td][/td]
[/tr]
[/table]

In other words: the new element is placed at index N - 1, which is indeed 5. (Wasntme)
 
I like Serena said:
Let's take a close look at your example. (Thinking)

Initially we have N=6 and S->Length=5.

[table="width: 500"]
[tr]
[td]Statement[/td]
[td]Assertion[/td]
[/tr]
[tr]
[td][m]if (S->Length==N) then error;[/m][/td]
[td]No error[/td]
[/tr]
[tr]
[td][m]else {[/m][/td]
[td]S->Length == N -1[/td]
[/tr]
[tr]
[td]
[m]S->Length=S->Length+1;[/m]​
[/td]
[td]S->Length == N[/td]
[/tr]
[tr]
[td]
[m](S->A)[S->Length-1]=x;[/m]​
[/td]
[td]S->A[N-1] == x[/td]
[/tr]
[tr]
[td][m]}[/m][/td]
[td][/td]
[/tr]
[/table]

In other words: the new element is placed at index N - 1, which is indeed 5. (Wasntme)

I see... (Nod) But this happens only when at the beginning it holds that [m] S->Length=N-1[/m]. In the general case, we put the new element at the position [m] S->Length [/m] before incrementing it, right?
 
evinda said:
I see... (Nod) But this happens only when at the beginning it holds that [m] S->Length=N-1[/m]. In the general case, we put the new element at the position [m] S->Length [/m] before incrementing it, right?

Let's redo that for the more general case, with the initial assumption that S->Length <= N. (Thinking)

[table="width: 500"]
[tr]
[td]Statement[/td]
[td]Assertion[/td]
[/tr]
[tr]
[td][/td]
[td]S->Length <= N[/td]
[/tr]
[tr]
[td][m]if (S->Length==N) then[/m][/td]
[td][/td]
[/tr]
[tr]
[td]
[m]error;[/m]​
[/td]
[td]S->Length == N and ERROR[/td]
[/tr]
[tr]
[td][m]else {[/m][/td]
[td]S->Length < N[/td]
[/tr]
[tr]
[td]
[m]S->Length=S->Length+1;[/m]​
[/td]
[td]S->Length <= N[/td]
[/tr]
[tr]
[td]
[m](S->A)[S->Length-1]=x;[/m]​
[/td]
[td]S->A == x where i <= N - 1[/td]
[/tr]
[tr]
[td][m]}[/m][/td]
[td]S->Length <= N[/td]
[/tr]
[/table]

In other words, if the stack was initially empty, any element will placed at an index <= N - 1.
And S->Length will always be <= N. (Mmm)
 
Back
Top