Function Pop for a static stack

  • Context: MHB 
  • Thread starter Thread starter evinda
  • Start date Start date
  • Tags Tags
    Function Static
Click For Summary

Discussion Overview

The discussion revolves around the implementation of the Push operation for a static stack using a one-dimensional array. Participants explore the necessary checks for stack overflow and the correct placement of new elements in the stack.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant questions whether an additional check for S->Length+1==N is necessary when pushing a new element onto the stack.
  • Another participant clarifies that the first check is to determine if the stack is full, implying that if it isn't, a new element can be added.
  • There is a discussion about the placement of the new element, with some participants asserting that it will be placed at index N-1 when the stack is full.
  • Concerns are raised about whether the new element is placed at S->Length before it is incremented, leading to further clarification on the indexing logic.
  • Participants analyze the conditions under which the new element is added, emphasizing that if the stack is initially empty, any element will be placed at an index less than or equal to N-1.

Areas of Agreement / Disagreement

Participants express differing views on the necessity of the additional check and the exact placement of the new element, indicating that multiple competing views remain unresolved.

Contextual Notes

There are unresolved assumptions regarding the initial state of the stack and the implications of the Length variable in relation to the maximum capacity N.

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: 126
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)
 

Similar threads

Replies
11
Views
4K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
6
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
Replies
12
Views
2K