Function Pop for a static stack

In summary: Let's redo that for the more general case, with the initial assumption that S->Length <= N. (Thinking)
  • #1
evinda
Gold Member
MHB
3,836
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
  • #2
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.
 
  • #3
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: 56
  • #4
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)
 
  • #5
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)
 
  • #6
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.

StatementAssertion
[m]if (S->Length==N) then error;[/m]No error
[m]else {[/m]S->Length == N -1
[m]S->Length=S->Length+1;[/m]​
S->Length == N
[m](S->A)[S->Length-1]=x;[/m]​
S->A[N-1] == x
[m]}[/m]

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

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

StatementAssertion
[m]if (S->Length==N) then error;[/m]No error
[m]else {[/m]S->Length == N -1
[m]S->Length=S->Length+1;[/m]​
S->Length == N
[m](S->A)[S->Length-1]=x;[/m]​
S->A[N-1] == x
[m]}[/m]

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?
 
  • #8
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)

StatementAssertion
S->Length <= N
[m]if (S->Length==N) then[/m]
[m]error;[/m]​
S->Length == N and ERROR
[m]else {[/m]S->Length < N
[m]S->Length=S->Length+1;[/m]​
S->Length <= N
[m](S->A)[S->Length-1]=x;[/m]​
S->A == x where i <= N - 1


[tr]
[td][m]}[/m][/td]
[td]S->Length <= N[/td]
[/tr]


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)
 

Related to Function Pop for a static stack

1. What is a static stack?

A static stack is a data structure in computer science that stores and retrieves data in a last-in, first-out (LIFO) manner. It is called "static" because the size of the stack is fixed and does not change during program execution.

2. How does a static stack differ from a dynamic stack?

A dynamic stack, also known as a dynamic array, is a data structure that allows for the size of the stack to change dynamically during program execution. This means that items can be added or removed from the stack without any restrictions on the size of the stack.

3. What is the purpose of a function pop for a static stack?

A function pop for a static stack is used to remove the top item from the stack and return its value. This helps maintain the LIFO order of the stack and allows for retrieving data in the reverse order it was inserted.

4. What happens if a pop function is called on an empty static stack?

If a pop function is called on an empty static stack, an error will occur as there are no items in the stack to remove and return. It is important to check if the stack is empty before calling the pop function.

5. Can a pop function be used on a dynamic stack?

Yes, a pop function can be used on a dynamic stack. However, unlike a static stack, the size of the dynamic stack can change during program execution. Therefore, the pop function may need to resize the stack after removing an item to maintain efficiency.

Similar threads

  • Programming and Computer Science
Replies
2
Views
968
  • Programming and Computer Science
Replies
5
Views
915
  • Programming and Computer Science
Replies
11
Views
3K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
6
Views
962
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
9
Views
2K
Back
Top