notes27 - file:/H|/pu.data/Desktop/0116.html Stack, Queues...

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

View Full Document Right Arrow Icon
file:///H|/pu.data/Desktop/0116.html Stack, Queues and LinkedLists Stack It s a simple data structure that has two operations: push(o) pushes one element into the stack o = pop pop the last inserted element from the stack As stack is a LIFO (last in first out) data structure Additional Methods isEmpty() returns if stack is empty or not size() # of element in stack top() returns top element without poping it Implementation of Stack using an array public class ArrayStack{ // Stack of type double type private double array[]; private int top; public ArrayStack (int capacity){ array = new double[capacity]; top = -1; } public void push (double d) throws StackFullException{ if(top == array.length){ // if Stack is full throw new StackFullException(); } top++; array[top] = d; } public double pop() throws StackEmptyException{ if(top == -1){ // if Stack is empty throw new StackFullException(); } double d = array[top]; top--; return d; } public boolean isEmpty(){ return top = -1; } file:///H|/pu.data/Desktop/0116.html (1 of 3) [2/3/2008 1:48:03 AM]
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:///H|/pu.data/Desktop/0116.html
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/02/2012 for the course CS 251 taught by Professor Staff during the Fall '08 term at Purdue University-West Lafayette.

Page1 / 3

notes27 - file:/H|/pu.data/Desktop/0116.html Stack, Queues...

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

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