190 likes | 385 Vues
Introduction to Stacks. What are Stacks? Stack implementation using arrays. Stack application. 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.
E N D
Introduction to Stacks • What are Stacks? • Stack implementation using arrays. • Stack application.
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)
Push(8) Push(2) pop(8) pop(1) An Example of Stack top top top pop(2) top top top
Stack Interface and Implementation • Stack interface is shown below: 1 public interface Stackextends Container{ 2 Object getTop(); 3 void push (Object object); 4 Object pop(); • } • Stack is implemented using arrays as follow: 1 public class StackAsArray extends AbstractContainer implements Stack 2 { 3 protected Object[] array; 4 //.. 5 } • The fields in this class are: array of Objects count field inherited from AbstractContainer
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. 1 public class StackAsArray extends AbstractContainer implements Stack 2 { 3 protected Object[] array; 4 public StackAsArray (int size){ • array = new Object [size]; • } • }
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 { 3 while (count > 0) 4 array [--count] = null; 5 }
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) 4 throw new ContainerFullException(); 5 array [count++] = object; 6 }
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) 4 throw new ContainerEmptyException (); 5 Object result = array [--count]; 6 array [count] = null; 7 return result; 8 }
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 { 3 if (count == 0) 4 throw new ContainerEmptyException (); 5 return array [count - 1]; 6 }
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 { 5 visitor.visit (array [i]); 6 if (visitor.isDone()) 7 return; 8 } 9 }
getEnumeration() Method 1 public Enumeration getEnumeration () 2 { 3 return new Enumeration () 4 { 5 protected int position = 0; 6 public 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 }
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.
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.
Stack Application: Evaluating Postfix Expression 2 3 5 1 - * 2 / + 2 3 4 * 2 / + 2 12 2 / + 2 6 + 8
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
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}