notes101

notes101 - CS251 2nd week note Stacks, Queues and Linked...

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

View Full Document Right Arrow Icon
CS251 2nd week note Stacks, Queues and Linked Lists Stacks It is a container of objects that are inserted and removed according to the last-in-first- out principle.(LIFO) Basic Operations Push--insert an item to the top of stacks. Pop --removes an item from the top and returns item. Implementation of a stack using an array The ArrayStack only sotres elements of type object. To sotre primitive types, you need ot use wrappers. You need to use type cast to connect object to the element type you are using. Sample program public class ArrayStack implements Stack { private int capacity; //max size private object s[]; //stack array private int top; // top of stack public ArrayStack(int cap) { capacity = cap; s = new object[cap]; top = -1; } public int size() { return top+1; //Number of elements in stack } public boolean isEmpty() {return (top==-1);} public void push(Object obj) throws StackFullException { if(size() == capacity) { throw new stackFullException("Stack Full"); } top++; s[top] = obj; } public Object pop() throws StackEmpty Exception { if(isEmpty()) {throw new stackEmptyException("Stack empty"); } Object tmp = s[top]; s[top] = null; top--; return temp; }
Background image of page 1

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

View Full DocumentRight Arrow Icon
} Applications of stack RPN notation It is a way to evaluate and represent arithmetic equations using a stack. 2 3 + equivalent to 2 + 3
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.

Page1 / 5

notes101 - CS251 2nd week note Stacks, Queues and Linked...

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