Sie sind auf Seite 1von 10

IIT Bombay

Data Structures and Algorithms


Prof. Ajit A. Diwan
Prof. Ganesh Ramakrishnan
Prof. Deepak B. Phatak
Department of Computer Science and Engineering
IIT Bombay

Session: Stacks

Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 1


Sequence Data Types
IIT Bombay

• Many data types in computer science have their values as


sequence of elements of some type.
• Differ in the operations that can be performed on these values.
• Restricting the allowed operations prevents improper use.
• Clarifies the purpose for which the variables of that type are
being used.
• Improves the readability of programs.

Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 2


Stack
IIT Bombay

• A stack of type T is also a sequence of type T.


• Certain operations are defined for a stack.
• Only those operations can be used on a stack.
• Allows efficient implementation of the restricted set of
operations.
• Allows clearer descriptions of other functions.
• Simplest data type whose value is a sequence.

Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 3


Stack
IIT Bombay

• Allowed operations for a stack:


• empty(S)
• empty(Ф) = true
• empty(push(x,S)) = false for all x
• push(x,S) ( Note that this is different from the push
operation on a sequence, although it does the same thing)
• push(x,Ф) = push(x,Ф)
• push(x,push(y,S)) = push(x,push(y,S))
• The push on the right hand side is the operation on
sequences
Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 4
Stack
IIT Bombay

• pop(S)
• pop(Ф) = undefined (will give an error if executed)
• pop(push(x,S)) = S
• top(S)
• top(Ф) = undefined
• top(push(x,S)) = x
• Only these operations are allowed for a variable of stack
type

Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 5


Programming with Stacks
IIT Bombay

• Given a sequence of opening and closing parenthesis, we


want to write a function that returns true if the sequence is
balanced, and false otherwise.
• The sequence is balanced if every opening parenthesis has a
corresponding closing parenthesis, and these are properly
nested.
• “(()())’’ is a balanced parenthesis string but “())(‘’ is not.

Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 6


Balanced Parenthesis Sequence
IIT Bombay

• We define a function balanced(S), where S is a sequence of


parenthesis.
• To do this, we define another function valid(S1,S2), where
S1 is a sequence of parenthesis, while S2 is a stack of
opening parenthesis.
• The stack is used to “remember’’ the left parenthesis seen
so far in S1.

Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 7


Balanced Parenthesis Sequence
IIT Bombay

valid(Ф,S2) = empty(S2)
valid(push(‘(‘,S1),S2) = valid(S1, push(‘(‘,S2))
valid(push(‘)’,S1),S2) = false if empty(S2)
= valid(S1,pop(S2)) otherwise
balanced(S) = valid(S,Ф)
• Note that we use the allowed operations on the stack
variable. This simplifies description of more complicated
functions built using a stack.

Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 8


Reverse
IIT Bombay

• A simpler description of reverse using a stack.


• Use another function help(S1,S2) that uses a stack S2 to
reverse the sequence S1.
help(Ф,S2) = S2
help(push(x,S1),S2) = help(S1,push(x,S2))
reverse(S1) = help(S1,Ф)
• Can you prove that this is the same function as defined
earlier?

Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 9


Exercises
IIT Bombay

• Is it true that push(top(S),pop(S)) = S for all stacks S? If not, under


what conditions is this property true?
• Write a function bottom(S) that returns the bottom element of a
stack S. Note that you should only use the operations defined for a
stack. The function is undefined for an empty stack.

Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 10

Das könnte Ihnen auch gefallen