Function Pop for a static stack

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

The discussion focuses on the implementation of the Push operation for a static stack using a one-dimensional array in C. The participants clarify that the check for stack fullness is performed by evaluating if S->Length equals N. They confirm that the new element is placed at index S->Length before it is incremented, ensuring that the stack does not exceed its defined capacity. The conversation emphasizes the importance of understanding the relationship between the Length and the maximum size N of the stack.

PREREQUISITES
  • Understanding of static stack data structures
  • Familiarity with C programming language syntax
  • Knowledge of pointer manipulation in C
  • Concept of array indexing and bounds checking
NEXT STEPS
  • Review C programming documentation on pointers and arrays
  • Study the implementation of stack operations in C, focusing on error handling
  • Explore dynamic stack implementations and their advantages over static stacks
  • Learn about memory management in C, particularly regarding arrays and pointers
USEFUL FOR

Software developers, computer science students, and anyone interested in data structure implementations, particularly those working with C programming and stack operations.

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: 123
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
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
6
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
Replies
12
Views
2K