0% found this document useful (0 votes)
50 views

Stack

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)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

Stack

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)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

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

You might also like