notes101

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

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

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; }

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

View Full Document
} Applications of stack RPN notation It is a way to evaluate and represent arithmetic equations using a stack. 2 3 + equivalent to 2 + 3 5 3 + 2 - 6 2 * - equivalent to ((5+3)-2)-((6*2)) Advantages of RPN notation No problem with operator precedence.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern