L2_Stack - Defining a Stack ADT In the last lecture, we...

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

View Full Document Right Arrow Icon
Defining a Stack ADT In the last lecture, we learned to implement STACKS as an Abstract Data Type (ADT). isEmpty pop push Page 1 of 20
Background image of page 1

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

View Full DocumentRight Arrow Icon
/* * File: Stack.java */ public interface Stack { public void push(int element); public int pop(); public int stackDepth(); public boolean isEmpty(); } Page 2 of 20
Background image of page 2
Concrete Implementation of the Stack ADT (Version 2.0) We now improve the stackADT implementation version 2.0. The first issue is the size limitation in version 1.0. public class Stack_v2 implements Stack { private int elements[] = new int[100]; private int count = 0; } Page 3 of 20
Background image of page 3

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

View Full DocumentRight Arrow Icon
New implementation in version 2.0: public class Stack_v2 implements Stack { private int elements[] = new int[5]; private int count = 0; elements[0] elements[4] } Page 4 of 20
Background image of page 4
3 236 elements[0] elements[1] count elements[4] 2 3 6 Page 5 of 20
Background image of page 5

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

View Full DocumentRight Arrow Icon
* public void push(int element) { if (count == elements.length) { int tmp[] = new int[elements.length+5]; System.arraycopy(elements, 0, tmp, 0, elements.length); elements = tmp; } ; elements[count++]=element; }
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/28/2010 for the course CSC CSC1110 taught by Professor Cjyuan during the Fall '06 term at CUHK.

Page1 / 20

L2_Stack - Defining a Stack ADT In the last lecture, we...

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

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