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

Programming With Data Structures Stacks: CMP Sci 187

The document discusses stacks and their applications in programming. It includes examples of using stacks to: 1) Track method calls and print the call stack. A Global class is provided to add and remove method names from a stack to track the call chain. 2) Implement web browser back/forward navigation buttons by storing previously visited pages in a stack. 3) Check if a string matches the pattern "anbn" by using a finite automaton with an accompanying stack.

Uploaded by

shubhang2392
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

Programming With Data Structures Stacks: CMP Sci 187

The document discusses stacks and their applications in programming. It includes examples of using stacks to: 1) Track method calls and print the call stack. A Global class is provided to add and remove method names from a stack to track the call chain. 2) Implement web browser back/forward navigation buttons by storing previously visited pages in a stack. 3) Check if a string matches the pattern "anbn" by using a finite automaton with an accompanying stack.

Uploaded by

shubhang2392
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Cmp Sci 187: Programming with Data Structures

Stacks

1. Package

package a.b.c; //1
public class X {} //2

package a.b.c.d; //3
public class X {} //4

Are the declarations in lines 1-4 correct?

Suppose the above hierarchy is stored in a base directory called Import. Which directory
tains the class X from lines 1-2? And Class X from lines 3-4? con


2. Expressions

1 + 2 + 3 + 4 //1
1 + (2 * 3) + 4 //2
(1 * 2) + 3 + 4 //3
1 + 2 + (3 * 4) //4

(1 + 2) * 3 + 4 //5
(1 + 2) * (3 + 4) //6
1 * (2 + 3) * 4 //7
1 * 2 * 3 * 4 //8

Convert to postfix notation.




Show the stack while evaluating these postfix expressions.







3. Execution Stack
Cmp Sci 187: Programming with Data Structures
Stacks

You have used printStackTrace(OutputStream) method that is available as part of the
Throwable class. It prints the method call chain at the point where the exception
occurred. This information is provided by the J ava run time. Many languages like
J ava, C++, C use a stack of method frames for execution of method calls. Each
method has an associated memory region called a frame which is pushed on a stack
when the method is invoked and is popped when it returns.

Suppose we want to view the call chain of our application as it is executing. Using the
given class Global below, how can you achieve that? For simplicity we will assume
that our application has just two classes, X and Y with one method each and the main
method is part of class X and that it uses only these three classes, X, Y and Global.


public class Global { //1
private static Stack<String> callstack;
static {
callstack = new Stack<String>();
}
static void addCall(String name) {}
static void removeCall () {}
static void printCallChain () {}
}

public class X { //2
public void methodOne (Y y) { y.methodTwo(); }
public static main (String [] args) {
X x = new X();
Y y = new Y();
x.methodOne(y);
}
}

public class Y { //3
public void methodTwo () {}
}






Cmp Sci 187: Programming with Data Structures
Stacks

4. Web Browser
Many web browsers like Netscape, IE, Firefox, Safari have two navigation buttons,
previous and next which allow you to navigate through the pages you have visited.
Give a skeleton of how these could be implemented. Hint: Does Stack fit in here ?
public class Page {
public Page (String url) {}
}
public class History {
public History () {}
public Page next () {}
public Page previous () {}
}


5. Push Down Automaton
A push down automaton (PDA) consists of a Finite Automaton (FA) and a Stack. A
finite automaton consists of a certain number of states. It looks at the current input
and can move to a different state based on the input. It may produce a certain output.
The PDA model of computation can perform certain functions which the FA cannot.
Write a program using an FA and a Stack to check if the strings given as input are of
the form a b i.e., have equal number of consecutive a's followed by consecutive
b's. The value of n is not known a priori. As you are simulating an FA in your
program you can only look at the input and switch states but you cannot count the
characters you have seen.
n n
public class SequenceAnBnChecker {
public SequenceAnBnChecker(Inputstream is) {}
public boolean check () {}
}

You might also like