10Stack - ITI 1121 Introduction to Computing II Marcel...

Info icon This preview shows pages 1–14. Sign up to view the full content.

View Full Document Right Arrow Icon
ITI 1121. Introduction to Computing II * Marcel Turcotte School of Information Technology and Engineering Version of February 8, 2011 Abstract Abstract Data Type Stack * These lecture notes are meant to be looked at on a computer screen. Do not print them unless it is necessary.
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Stacks Software stacks are abstract data types (structures) similar to physical stacks. Books PEZ dispenser Plates Trays The analogy with a plate dispenser in a cafeteria is particularly interesting: 1) for software stacks, just like physical stacks, only the top element is accessible and 2) the top element must be removed in order to access the remaining elements .
Image of page 2
Definition A stack is a linear data structure that is always accessed from the same extremity, one element at a time, and that element is called the top of the stack. Stacks are also called LIFO data structures: last-in first-out . "alpha" "beta" "alpha" "alpha" s = new StackImp() s.push( "alpha" ) s.push( "beta" ) o = s.pop()
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Applications Stacks are widely used in applications and system programming: In compilers, and formal language analysis in general; To implement backtracking algorithms, which are used in automatic theorem provers (Prolog), games algorithms and artificial intelligence algorithms; For memory management during program execution, system stack, it supports the development of recursive algorithms; To support “undo” operations or “back” buttons inside a web browser.
Image of page 4
Basic operations The basic operations of a stack are: push: add an element onto the top of the stack; pop: removes and returns the top element; empty: tests if the stack is empty.
Image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Stack ADT public interface Stack { public abstract boolean isEmpty(); public abstract Object push( Object o ); public abstract Object pop(); public abstract Object peek(); }
Image of page 6
Stack ADT (using Java 1.5) public interface Stack<E> { public abstract boolean isEmpty(); public abstract E push( E elem ); public abstract E pop(); public abstract E peek(); } Yes, that’s right, an interface can also be parametrized!
Image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Example public class Mystery { public static void main( String[] args ) { Stack<String> stack = new StackImplementation<String>(); for ( int i=0; i<args.length(); i++ ) stack.push( args[ i ] ); while ( ! stack.empty() ) System.out.print( stack.pop() ); } } What does this print: “ java Mystery a b c d e ”?
Image of page 8
Remarks Elements come out of a stack in reverse order; Frequently occurring idiom: while ( ! stack.empty() ) { element = stack.pop(); // ... } Make sure to not forget the pop()!
Image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Operations (contd) peek: returns the object at the top of this stack without removing it;
Image of page 10
Implementations How would you implement this interface? There are two popular families of implementation techniques: Array-based; Using linked-nodes. Stack<Token> s; s = new ArrayStack<Token>(); s = new DynamicArrayStack<Token>(); s = new LinkedStack<Token>();
Image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Question One of the proposed implementations will be using an array, why not using an array instead of stack in our programs since the implementation of the interface stack will be using an array anyway?
Image of page 12
Implementing a Stack using an array: ArrayStack What are the instance variables?
Image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 14
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern