{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

Info icon This 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]
Image of page 1

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

View Full Document Right Arrow Icon
file:///H|/pu.data/Desktop/0116.html } Uses of stack data structure: + Stack are used during the execution of a program
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}