Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Stacks What are Stacks? Stack implementation using arrays. Stack application.

Similar presentations


Presentation on theme: "Introduction to Stacks What are Stacks? Stack implementation using arrays. Stack application."— Presentation transcript:

1 Introduction to Stacks What are Stacks? Stack implementation using arrays. Stack application.

2 What are Stacks? Stacks are containers that provide exactly one method, push, for putting objects into the container, and one method, pop, for taking objects out of the container. When an element to be added to the stack, it should be placed on the top of the stack. When an element to be removed from the stack it should be removed from the top of the stack too. It is always the last element that is pushed into the stack, to be the first element to be popped from the stack (LIFO)

3 An Example of Stack 2 8 1 7 2 7 2 1 7 2 1 7 2 8 1 7 2 8 1 7 2 top Push(8) Push(2) pop(2) pop(8) pop(1)

4 Stack Interface and Implementation Stack interface is shown below: 1public interface Stackextends Container{ 2Object getTop(); 3void push (Object object); 4Object pop(); 5} Stack is implemented using arrays as follow: 1 public class StackAsArray extends AbstractContainer implements Stack 2 { 3protected Object[] array; 4//.. 5 } The fields in this class are: array of Objects count field inherited from AbstractContainer

5 StackAsArray Constructor The constructor takes a single parameter, size, which specifies the maximum number of items that can be stored in the stack. The variable array is initialized to be an array of length size. 1public class StackAsArray extends AbstractContainer implements Stack 2 { 3protected Object[] array; 4 public StackAsArray (int size){ array = new Object [size]; }

6 purge() Method The purpose of the purge method is to remove all the contents of a container. To empty the stack, the purge method simply assigns the value null to the first count positions of the array. 1 public void purge() 2 { 3while (count > 0) 4array [--count] = null; 5 }

7 push() Method push() method adds an element to the stack. It takes as an argument an Object to be pushed. It first checks if there is room left in the stack. If no room is left, it throws a ContainerFullException exception. Otherwise, it puts the object into the array, and then increments count variable by one. 1 public void push (Object object) 2 { 3 if (count == array.length) 4throw new ContainerFullException(); 5array [count++] = object; 6 }

8 pop() Method The pop method removes an item from the stack and returns that item. The pop method first checks if the stack is empty. If the stack is empty, it throws a ContainerEmptyException. Otherwise, it simply decreases count by one and returns the item found at the top of the stack. 1 public Object pop () 2 { 3 if (count == 0) 4throw new ContainerEmptyException (); 5Object result = array [--count]; 6array [count] = null; 7return result; 8 }

9 getTop() Method getTop() method first checks if the stack is empty. getTop() method is a stack accessor which returns the top item in the stack. without removing that item. If the stack is empty, it throws a ContainerEmptyException. Otherwise, it returns the top item found at position count-1. 1 public Object getTop() 2 { 3if (count == 0) 4throw new ContainerEmptyException (); 5return array [count - 1]; 6 }

10 accept() Method The body of the accept method is simply a loop which calls the visit method for each object in the stack. 1 public void accept (Visitor visitor) 2 { 3 for (int i = 0; i< count; ++i) 4 { 5visitor.visit (array [i]); 6if (visitor.isDone()) 7return; 8} 9 }

11 getEnumeration() Method 1 public Enumeration getEnumeration () 2 { 3 return new Enumeration () 4 { 5protected int position = 0; 6public boolean hasMoreElements () 7 { return position < getCount(); } 8 public Object nextElement() 9 { 10 if (position >= getCount ()) 11 throw new NoSuchElementException (); 12 return array [position++]; 13 } 14 }; //return 15 }

12 Stack Application: Evaluating Postfix Expression (5+9)*2+6*5 infix-notation binary operators appear in between their operands. The order of operations evaluation is determined by the presedence rules and paranthesis. When an evaluation order is desired that is different from that provided by the precedence, parentheses are used to override precedence rules.

13 Stack Application: Evaluating Postfix Expression prefix notation: binary operators appear before there operands: A+B  +AB postfix notation: binary operators appear next to their operands: A+B  AB+ The advantage of these notations is that the order of operation evaluation is unique without the need for presedance rules or paranthesis.

14 Stack Application: Evaluating Postfix Expression 2 3 5 1 - * 2 / + 2 3 4 * 2 / + 2 12 2 / + 2 6 + 8

15 Stack Application: Evaluating Postfix Expression To evaluate a Psotfix expression we can use the following algorithm initialize operqand Stack While items remian in postfix expession Get next ietm from postfix expression if item is an operand Push item onto operand stack else Pop operand into op1 Pop operand into op2 if Operator is '+' result=op1+op2 elseif Operator is '-' result=op1-op2 elseif Operator is '*' result=op1*op2 else Operator is '/' result=op1/op2 Push the result into the stack

16 Evaluating Postfix Expression 1 public class Algorithms 2 { 3 public static void calculator (Reader in, PrintWriter out) 4 throws IOException 5 { 6 Stack stack = new StackAsLinkedList(); 7 int i; 8 while ((i = in.read()) > 0) 9 { 10 char c = (char) i; 11 if (Character.isDigit (c)) 12 stack.push(new Int (Character.digit (c, 10))); 13 else if (c == '+') 14 { 15 Int arg2= (Int) stack.pop(); 16 Int arg1 = (Int)stack.pop(); 17 stack.push (new Int ( 18 arg1.intValue () + arg2.intValue())); 19 } 20 } 21 } 22}


Download ppt "Introduction to Stacks What are Stacks? Stack implementation using arrays. Stack application."

Similar presentations


Ads by Google