Stacks - Stacks A stack is an abstract data type based on...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Stacks A stack is an abstract data type based on the principle of Last In First Out (LIFO). Just think about a stack of books or dishes. A stack supports the following operations: • Push something on the top. • Look at the top. • Pop something off the top of the stack. When we do not care about how these operations are implemented, and just want to express the fact that a stack must provide these operations, we make use of an interface . In Java, this looks as follows: public interface Stack<AnyType> { public void clear(); public boolean isEmpty(); public AnyType top(); public AnyType pop(); public void push(AnyType x); } Since Stack provides only names and argument types for methods, it is obviously not enough to implement a stack. We will have to define a class , for instance DsStack , that implements the Stack interface. In Java, we express that a class implements an interface like this: public class DsStack<AnyType> implements Stack<AnyType> { // implementation } Classes that implement an interface must provide an implementation for all methods declared in the interface. So interfaces capture the common characteristics and behavior of the classes that implementinterface....
View Full Document

This note was uploaded on 04/20/2010 for the course CS CS206 taught by Professor Lee during the Fall '09 term at 카이스트, 한국과학기술원.

Page1 / 2

Stacks - Stacks A stack is an abstract data type based on...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online