0% found this document useful (0 votes)
26 views3 pages

Understanding Stack Data Structure

This document defines and explains the stack data structure. It notes that a stack is a limited version of an array that can only have elements added or removed from one end in LIFO (Last-In First-Out) order. Common stack operations like push, pop, isEmpty and isFull are described. An example is given showing the state of a stack after various push and pop operations, illustrating the LIFO behavior. Finally, some exercises are provided to demonstrate working with stacks through code snippets.

Uploaded by

25tt
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views3 pages

Understanding Stack Data Structure

This document defines and explains the stack data structure. It notes that a stack is a limited version of an array that can only have elements added or removed from one end in LIFO (Last-In First-Out) order. Common stack operations like push, pop, isEmpty and isFull are described. An example is given showing the state of a stack after various push and pop operations, illustrating the LIFO behavior. Finally, some exercises are provided to demonstrate working with stacks through code snippets.

Uploaded by

25tt
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Stack Data Structure

By : Imam M Shofi

What is stack?
 A stack is a limited version of an
array.
 New elements, or nodes as they are
often called, can be added to a stack
and removed from a stack only from
one end.
 Access system a stack is referred to
as a LIFO structure (Last-
(Last-In First-
First-
Out)
 Some illustrations:

stack of satay stack of CDs

1
Stacks operations
 Push : adds a new node
Push(X,S)
Push(X,S)  add the value X to the TOP of stack
 Pop : removes a node
Pop(S)
Pop(S)  removes the TOP node and returns its
value
 IsEmpty : reports whether the stack is
empty
IsEmpty(S)
IsEmpty(S)  report whether the stack S is empty
 IsFull : reports whether the stack is full
IsFull(S)
IsFull(S)  report whether the stack S is full
 Initialize : creates/initializes the stack
Initialize(S)
Initialize(S)  create a new empty stack named S
 Destroy : deletes the contents of the
stack (may be implemented by re-
re-
initializing the stack)
Destroy(S)
Destroy(S)  deletes the contents of the stack S

Illustration/example
Operation Stack’
Stack’s contents TOP value

1. Initialiaze(S)
Initialiaze(S) <empty> 0
2. Push(‘
Push(‘a’,S)
,S) a 1
3. Push(‘
Push(‘b’,S)
,S) ab 2
4. Push(‘
Push(‘c’,S)
,S) abc 3
5. Pop(S)
Pop(S) ab 2
6. Push(‘
Push(‘d’,S)
,S) abd 3
7. Push(‘
Push(‘e’,S)
,S) abde 4
8. Pop(S)
Pop(S) abd 3
9. Pop(S)
Pop(S) ab 2
10. Pop(S)
Pop(S) a 1

2
Exercise
 What would the state of the stack be after the
following operations:
create stack
push A onto stack
push F onto stack
pop item from stack
push B onto stack
pop item from stack
pop item from stack
 Show the state of the stack and the value of each
variable after execution of each of the following
statements:
A=5 B=3 C=7
(a) (b)
create stack create stack
push A onto stack push B onto stack
push C*C onto stack push C onto stack
pop item from stack and store in B push A onto stack
push B+A onto stack A=B*C
pop item from stack and store in A push A+C onto stack
pop item from stack and store in B pop item from stack and store in A
pop item from stack and store in B
pop item from stack and store in C

Common questions

Powered by AI

The fundamental characteristic of the stack data structure that differentiates it from an array is its Last-In First-Out (LIFO) property. This means that new elements, or nodes, can only be added and removed from one end of the structure, the 'top' of the stack .

The IsFull operation checks whether a stack has reached its capacity and cannot accommodate additional elements. If IsFull returns true, attempting a Push operation would result in an error or undefined behavior, as the stack cannot expand to accommodate more nodes .

Starting from an empty stack, after Push 'X', the stack becomes [X]. After Push 'Y', the stack becomes [X, Y]. The subsequent Pop operation removes 'Y', leaving the stack as [X]. Finally, Push 'Z' results in the stack becoming [X, Z].

The Push operation adds a new node to the top of the stack, increasing its size by one . Conversely, the Pop operation removes the top node from the stack, returns its value, and decreases the stack's size by one . Together, these operations modify the state of the stack by altering its contents and the position of its top element.

The IsEmpty operation checks whether a stack is empty, thereby informing whether a Pop operation can proceed without error. If a stack is empty, attempts to Pop will return errors or null results, guiding the programmer to avoid such operations until a Push operation populates the stack .

Initializing a stack creates an empty stack, making it available for subsequent operations such as Push and Pop . Destroying a stack deletes its contents, usually through re-initialization, and effectively returns it to an empty state where it can be used anew, but any data previously stored is lost .

An initially empty stack will once again be empty after the following sequence of operations: Push A (stack becomes [A]), Push B (stack becomes [A, B]), Push C (stack becomes [A, B, C]), Pop (removes C, stack becomes [A, B]), Pop (removes B, stack becomes [A]), Pop (removes A, stack becomes empty).

Initially, B = 3, C = 7, after pushing these and A = 5, stack is [3, 7, 5]. Calculating A = B*C gives A = 21, and pushing A+C (28) results in stack [3, 7, 5, 28]. Popping 28 stores it in A, A = 28, popping 5 stores it in B, B = 5, and popping 7 stores it in C, C = 7, leaving stack [3].

When Push(X) is executed on a stack, X becomes the new top value. If this is immediately followed by a Pop(), X is removed and returned, revealing the value beneath it as the new top. If the stack was not previously empty, the previous top value before X was pushed becomes the top again. If the stack was empty, it returns to being empty .

Starting with A = 5, B = 3, C = 7: When C*C (49) is pushed, stack becomes [5, 49]. Pop removes 49; B now equals 49. Push 54 (B + A, i.e., 49 + 5) results in stack [5, 54]. Popping stores 54 in A, leading to A = 54 .

You might also like