You are on page 1of 15

John Ngotho

Informatics 1B Chapter 5
Using a Stack
Pringles! Last chip to go in the can is the first one in my
stomach.
Stacks make excellent mechanisms for temporary storage of
information within procedures. A primary reason for this is
that they allow recursive invocations of procedures without
risk of destroying data from previous invocations of the
routine.
One of the most common use of stack in Computer Science is
XML processing. The tags in XML are always nested, also the
closing of tags in XML must be done in order of last-in-first-
out.
Another use of stack is in evaluation of arithmetic
expressions in scripting languages.
Applications of Stacks
Call stack (recursion).
Searching networks, traversing trees (keeping a track where
we are).
Examples:
Checking balanced expressions
Recognizing palindromes
Evaluating algebraic expressions
Simple Applications of the ADT Stack: Checking for
Balanced Braces
A stack can be used to verify whether a program
contains balanced braces
An example of balanced braces
abc{defg{ijk}{l{mn}}op}qr
An example of unbalanced braces
abc{def}}{ghij{kl}m
abc{def}{ghij{kl}m
Checking for Balanced Braces
Requirements for balanced braces
Each time you encounter a }, it matches an already
encountered {
When you reach the end of the string, you have matched each
{
Checking for Balanced Braces
Figure 7-3
Traces of the algorithm that checks for balanced braces
Evaluating Postfix Expressions
A postfix (reverse Polish logic) calculator
Requires you to enter postfix expressions
Example:234+*
When an operand is entered, the calculator
Pushesitontoastack
When an operator is entered, the calculator
Appliesittothetoptwooperandsofthestack
Popstheoperandsfromthestack
Pushestheresultoftheoperationonthestack
Evaluating Postfix Expressions
Figure 7-8
The action of a postfix calculator when evaluating the expression 2 * (3 + 4)
Evaluating Postfix Expressions
Pseudo code:
int eval uat e( St r i ng expr essi on)
{
St ack st ack=new St ack( ) ; / / cr eat y empt y st ack
while ( true) {
St r i ng c=expr essi on. get Next I t em( ) ;
if ( c==ENDOFLI NE)
return st ack. pop( ) ;
if ( c i s oper and)
st ack. push( c) ;
else { / / oper at i on
int oper and2=st ack. pop( ) ;
int oper and1=st ack. pop( ) ;
st ack. push( execut e( c, oper and1, oper and2) ) ;
}
}
}
Using a Stack
Stack class is in java.util package of java.
Stack works like last in first out policy.
Stack does not require any fix dimension like String array and
int array.
Using a Stack
This example shows how to use the Stack class. A Stack adds
and removes elements on a First-In First-Out basis.
The key methods of the Stack class are push(), peek(), pop(),
empty() and search().
push() adds an element to the top of the stack.
peek() returns the first element from the top of the stack
without removing it from the stack.
pop() as peek() but removes it from the stack.
empty() checks wheter there are elements in the stack or
not.
search() returns the position of an element in the stack.
Using a Stack
//Create the Stack instance and add a couple of elements to it
Stack stack = new Stack();
String s1 = "element 1";
String s2 = "element 2";
stack.push(s1);
stack.push(s2);
Using a Stack
Now we have two elements in the stack, and to check what
element is at the top of the stack (and will be the first to be
removed) we use the peek() method to find out.
System.out.println(stack.peek());
The output is:
element 2
Using a Stack
To find out the position of the first element, we use the
method search().
//Find position of a certain element
int pos = stack.search("element 1");
System.out.println(pos);
This will print out the position within the stack.
2
Using a Stack
To remove elements from the stack, we use the method pop().
System.out.println(stack.pop());
System.out.println(stack.pop());
This will result in the output:
element2
element1
The element 2 was added after element 1 so that element is
removed first.
Now the stack is empty, and to be sure we check with the empty
method:
System.out.println(stack.empty());
true

You might also like