ADVANCED DATA
STRUCTURES
STACKS
CRISTIAN REY C. SECO, MIT
Objectives
At the end of the lesson, the student should be able
to:
 Explain the basic concepts and operations on the
ADT...
Introduction
 Stack - linearly ordered set of elements having
the last-in, first-out (LIFO) discipline
 Operations are d...
Operations
 Insertion of new element onto the stack (push)
 Deletion of the top element from the stack (pop)
 Getting t...
Operations
 Push
Operations
 Pop
Operations
procedure SPUSH(S,n,top,x)
array S(1:n)
if top = n then call STACKFULL
top <- top + 1
S(top) <- x
end SPUSH
pro...
Operations
public interface Stack
{
public int size(); /* returns the size of the stack */
public boolean isEmpty(); /* ch...
interface
 interface – reserved word in java, a class
that contains only the method headings, and
each method heading is ...
inheritance
 Inheritance means that a new class can
be derived from or based on an already
existing class.
 The new clas...
inheritance
 When you use inheritance, the class
containing your application program will
have more than one method.
 Co...
Implementations
 Sequential Representation
 Makes use of arrays
 Stack is empty if top=-1 and full if top=n-1
 Deletio...
Implementations
public class ArrayStack implements Stack
{
public static final int CAPACITY = 1000;
public int capacity;
O...
Implementations
public Object top()
{
if (isEmpty()) throw new
StackException("Stack empty.");
return S[top];
}
public Obj...
Implementations
•Linked Representation
– A linked list of stack nodes could be used to
implement a stack
Implementations
public class LinkedStack implements Stack
{
private Node top;
private int numElements = 0;
public int size...
Implementations
public Object pop()
{
Node temp;
if (isEmpty())
throw new StackException("Stack underflow.");
temp = top;
...
Application: Infix to Postfix
 Expressions can be in:
 infix form - every subexpression is of the
form operand-operator-...
Application: Infix to Postfix
 Theorem: A postfix expression is well-formed if the
rank of every proper head is greater t...
Application: Infix to Postfix
 Examples:
Infix Expression Postfix Expression
a * b + c / d a b * c d / -
a ^ b ^ c - d a ...
Application: Infix to Postfix
 Exercises:
Infix Expression
1) a * b + c / d a b * c d / -
2) a ^ b ^ c - d a b c ^ ^ d -
...
Upcoming SlideShare
Loading in …5
×

Advanced data structures slide 2 2+

1,627 views

Published on

Published in: Education, Technology
  • Be the first to comment

Advanced data structures slide 2 2+

  1. 1. ADVANCED DATA STRUCTURES STACKS CRISTIAN REY C. SECO, MIT
  2. 2. Objectives At the end of the lesson, the student should be able to:  Explain the basic concepts and operations on the ADT stack  Implement the ADT stack using sequential and linked representation  Discuss applications of stack: the pattern recognition problem and conversion from infix to postfix  Explain how multiple stacks can be stored using one dimensional array  Reallocate memory during stack overflow in multiple-stack array using unit-shift policy and Garwick's algorithm
  3. 3. Introduction  Stack - linearly ordered set of elements having the last-in, first-out (LIFO) discipline  Operations are done on top of the stack only and we have no access to other elements  Basic operations: push and pop  Representation: sequential or linked
  4. 4. Operations  Insertion of new element onto the stack (push)  Deletion of the top element from the stack (pop)  Getting the size of stack  Checking for empty stack  Getting the top element without deleting it from the stack
  5. 5. Operations  Push
  6. 6. Operations  Pop
  7. 7. Operations procedure SPUSH(S,n,top,x) array S(1:n) if top = n then call STACKFULL top <- top + 1 S(top) <- x end SPUSH procedure SPOP(S,n,top,x) array S(1:n) if top = 0 then call STACKEMPTY else [ x <- S(top); top <- top –1 ] end SPOP
  8. 8. Operations public interface Stack { public int size(); /* returns the size of the stack */ public boolean isEmpty(); /* checks if empty */ public Object top() throws StackException; public Object pop() throws StackException; public void push(Object item) throws StackException; } class StackException extends RuntimeException { public StackException(String err) { super(err); } }
  9. 9. interface  interface – reserved word in java, a class that contains only the method headings, and each method heading is terminated with a semicolon
  10. 10. inheritance  Inheritance means that a new class can be derived from or based on an already existing class.  The new class inherits features such as methods from the existing class, which saves a lot of time for programmers.  Example: a newly define class StackException that would extend the definition of RuntimeException.
  11. 11. inheritance  When you use inheritance, the class containing your application program will have more than one method.  Constructor  is a special type of method of a class that is automatically executed when an object of the class is created.  It is used to initialize object. The name of the constructor is always the same as the name of the class
  12. 12. Implementations  Sequential Representation  Makes use of arrays  Stack is empty if top=-1 and full if top=n-1  Deletion from an empty stack causes an underflow  Insertion onto a full stack causes an overflow
  13. 13. Implementations public class ArrayStack implements Stack { public static final int CAPACITY = 1000; public int capacity; Object S[]; int top = -1; public ArrayStack() { this(CAPACITY); } public ArrayStack(int c) { capacity = c; S = new Object[capacity]; } public int size() { return (top+1); } public boolean isEmpty() { return (top < 0); }
  14. 14. Implementations public Object top() { if (isEmpty()) throw new StackException("Stack empty."); return S[top]; } public Object pop() { Object item; if (isEmpty()) throw new StackException("Stack underflow."); item = S[top]; S[top--] = null; return item; } public void push(Object item) { if (size()==capacity) throw new StackException ("Stack overflow."); S[++top]=item; }
  15. 15. Implementations •Linked Representation – A linked list of stack nodes could be used to implement a stack
  16. 16. Implementations public class LinkedStack implements Stack { private Node top; private int numElements = 0; public int size() { return (numElements); } public boolean isEmpty() { return (top == null); } public Object top() { if (isEmpty()) throw new StackException ("Stack empty."); return top.info; }
  17. 17. Implementations public Object pop() { Node temp; if (isEmpty()) throw new StackException("Stack underflow."); temp = top; top = top.link; return temp.info; } public void push(Object item) { Node newNode = new Node(); newNode.info = item; newNode.link = top; top = newNode; } }
  18. 18. Application: Infix to Postfix  Expressions can be in:  infix form - every subexpression is of the form operand-operator-operand  postfix form - every subexpression is of the form operand-operand-operator, form most appropriate for computers
  19. 19. Application: Infix to Postfix  Theorem: A postfix expression is well-formed if the rank of every proper head is greater than or equal to 1 and the rank of the expression is 1. Operator Priority Property Example ^ 3 right associative a^b^c = a^(b^c) * / 2 left associative a*b*c = (a*b)*c + - 1 left associative a+b+c = (a+b)+c
  20. 20. Application: Infix to Postfix  Examples: Infix Expression Postfix Expression a * b + c / d a b * c d / - a ^ b ^ c - d a b c ^ ^ d - a * ( b + ( c + d ) / e ) - f a b c d + e /+* f - a * b / c + f * ( g + d ) / ( f – h ) ^ i a b * c / f g d + * f h – i ^ / +
  21. 21. Application: Infix to Postfix  Exercises: Infix Expression 1) a * b + c / d a b * c d / - 2) a ^ b ^ c - d a b c ^ ^ d - 3) a * ( b + ( c + d ) / e ) - f a b c d + e /+* f - 4) a * b / c + f * ( g + d ) / ( f – h ) ^ i a b * c / f g d + * f h – i ^ / +

×