Function Pop for a static stack

  • Context:
  • Thread starter Thread starter evinda
  • Start date Start date
  • Tags Tags
    Function Static
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
7 replies · 2K views
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?
 
Physics news on Phys.org
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: 152
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)