Está en la página 1de 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

También podría gustarte