{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

Info iconThis preview shows pages 1–8. 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). i s E m p t y p o p p u s h 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 Document Right 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 Document Right Arrow Icon
New implementation in version 2.0: public class Stack_v2 implements Stack { private int elements[] = new int[5]; private int count = 0; 0 elements[0] elements[4] } Page 4 of 20
Background image of page 4
3 2 3 6 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 Document Right 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; } Page 6 of 20
Background image of page 6
Note: z if (count == elements.length) {
Background image of page 7

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

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

{[ snackBarMessage ]}