Carlos Delgado Kloos Dep. Ingeniería Telemática Univ ... ?· Carlos Delgado Kloos Dep. Ingeniería…

Download Carlos Delgado Kloos Dep. Ingeniería Telemática Univ ... ?· Carlos Delgado Kloos Dep. Ingeniería…

Post on 06-Jun-2018

212 views

Category:

Documents

0 download

Embed Size (px)

TRANSCRIPT

  • cdk@it.uc3m.es

    Stacks

    Carlos Delgado Kloos

    Dep. Ingeniera Telemtica

    Univ. Carlos III de Madrid

    Java: Stacks / 1

  • cdk@it.uc3m.es

    Example

    Java: Stacks / 2

  • cdk@it.uc3m.es

    Example

    Java: Stacks / 3

  • cdk@it.uc3m.es

    Example

    Java: Stacks / 4

  • cdk@it.uc3m.es

    Characteristics

    Linear structure

    Access on one end both for insertion andextraction

    Java: Stacks / 5

  • cdk@it.uc3m.es

    Main methods

    Insert on one end: push(x)

    Extract at the same end: pop()

    Java: Stacks / 6

  • cdk@it.uc3m.es

    Example:

    Check parentheses

    Good:

    ()

    (()(()))

    Bad:

    )(

    (()

    ())

    Java: Stacks / 7

    Rules:

    Basic:

    Sequentiation: ()()

    Nesting: (())

  • cdk@it.uc3m.es

    Example:

    Check parentheses

    Rules:

    Each time we find a (we'll put it in the stack.

    Each time we find a )we'll extract the upper ( of the stack.

    The string of parentheses is correctif the stack if empty after having gonethrough to complete string.

    Java: Stacks / 8

  • cdk@it.uc3m.es

    Example:

    check (()(()())())

    Java: Stacks / 9

    (()(()())())

  • cdk@it.uc3m.es

    Example:

    check (()(()())())

    Java: Stacks / 10

    (()(()())())

    (

  • cdk@it.uc3m.es

    Example:

    check (()(()())())

    Java: Stacks / 11

    (()(()())())

    ((

  • cdk@it.uc3m.es

    Example:

    check (()(()())())

    Java: Stacks / 12

    (()(()())())

    (

  • cdk@it.uc3m.es

    Example:

    check (()(()())())

    Java: Stacks / 13

    (()(()())())

    ((

  • cdk@it.uc3m.es

    Example:

    check (()(()())())

    Java: Stacks / 14

    (()(()())())

    (((

  • cdk@it.uc3m.es

    Example:

    check (()(()())())

    Java: Stacks / 15

    (()(()())())

    ((

  • cdk@it.uc3m.es

    Example:

    check (()(()())())

    Java: Stacks / 16

    (()(()())())

    (((

  • cdk@it.uc3m.es

    Example:

    check (()(()())())

    Java: Stacks / 17

    (()(()())())

    ((

  • cdk@it.uc3m.es

    Example:

    check (()(()())())

    Java: Stacks / 18

    (()(()())())

    (

  • cdk@it.uc3m.es

    Example:

    check (()(()())())

    Java: Stacks / 19

    (()(()())())

    ((

  • cdk@it.uc3m.es

    Example:

    check (()(()())())

    Java: Stacks / 20

    (()(()())())

    (

  • cdk@it.uc3m.es

    Example:

    check (()(()())())

    Java: Stacks / 21

    (()(()())())

    Correct: We have completed thestring and the stack is empty

  • cdk@it.uc3m.es

    Example:

    check ([]{()}())

    Java: Stacks / 22

    ([]{()}())

    Correct: We have completed thestring and the stack is empty

  • Example: HTML

    hello([)]

    Correct with HTML 1.0-4.0

    Incorrect with XHTML

    hello([])

    Correct with HTML 1.0-4.0

    Correct with XHTML

    cdk@it.uc3m.es Java: Stacks / 23

  • cdk@it.uc3m.es

    Stack interface

    public interface Stack {

    public void push(Object o)

    throws StackOverflowException;

    public Object pop()throws EmptyStackException;

    public Object top()throws EmptyStackException;

    public int size();

    public boolean isEmpty();

    } Java: Stacks / 24

  • cdk@it.uc3m.es

    One interface and

    several implementations

    Java: Stacks / 25

    ArrayStack

    Stack

    LinkedStack

  • cdk@it.uc3m.es

    Array-based

    implementation

    Java: Stacks / 26

    data

    0 1 2 N-13 4 5

    1

    top

    2

    top

    3

    top

    4

    top

    5

    toptop

  • cdk@it.uc3m.es

    Array-based

    implementation

    public class ArrayStack implements Stack {

    public static final int DEFAULT_CAPACITY = 1000;

    private int capacity;

    private Object data[];

    private int top = -1;

    public ArrayStack() {

    this(DEFAULT_CAPACITY);

    }

    public ArrayStack(int capacity) {this.capacity = capacity;

    data = new Object[capacity];

    }Java: Stacks / 27

  • cdk@it.uc3m.es

    Array-based

    implementation

    public int size() {return (top + 1);

    }public boolean isEmpty() {return (top < 0);

    }public Object top()throws EmptyStackException {if (top == -1)throw new EmptyStackException("empty");

    return data[top];}

    Java: Stacks / 28

  • cdk@it.uc3m.es

    Array-based

    implementation

    public void push(Object o)throws StackOverflowException {if (top == capacity - 1)

    throw new StackOverflowException();data[++top] = o;

    }

    Java: Stacks / 29

  • cdk@it.uc3m.es

    Array-based

    implementation

    public Object pop()throws EmptyStackException {Object o;if (top == -1)throw new EmptyStackException();

    o = data[top];data[top--] = null;return o;

    }

    Java: Stacks / 30

  • cdk@it.uc3m.es Java: Stacks /cdk@it.uc3m.es

    Implementation

    based on linked lists

    Java: Stacks / 31

    Madrid Miami Munich

    top

  • cdk@it.uc3m.es

    Implementation

    based on linked lists

    class Node {

    private Object info;

    private Node next;

    public Node(Object info, Node next) {this.info = info;

    this.next = next;

    }

    void setInfo(Object info) {this.info = info;}

    void setNext(Node next) {this.next = next;}

    Object getInfo() {return info;}

    Node getNext() {return next;}

    }Java: Stacks / 32

  • cdk@it.uc3m.es

    Implementation

    based on linked lists

    public class LinkedStack implements Stack {

    private Node top;

    private int size;

    public LinkedStack() {

    top = null;

    size = 0;

    }

    public int size() {

    return size;

    }

    public boolean isEmpty() {return (top == null);

    }Java: Stacks / 33

  • cdk@it.uc3m.es Java: Stacks /cdk@it.uc3m.es

    Insertion (push)

    Java: Stacks / 34

    Moscow

    Madrid Miami Munich

    top

  • cdk@it.uc3m.es

    Implementation

    based on linked lists

    public void push(Object info) {

    Node n = new Node(info, top);

    top = n;

    size++;

    }

    Java: Stacks / 35

  • cdk@it.uc3m.es

    Implementation

    based on linked lists

    public Object top()

    throws EmptyStackException {

    if (top == null)

    throw new EmptyStackException();

    return top.getInfo();

    }

    Java: Stacks / 36

  • cdk@it.uc3m.es Java: Stacks /cdk@it.uc3m.es

    Extraction (pop)

    Java: Stacks / 37

    Madrid Miami MunichMoscow

    top

  • cdk@it.uc3m.es

    Implementation

    based on linked lists

    public Object pop()throws EmptyStackException {Object info;if (top == null)throw new EmptyStackException();

    info = top.getInfo();top = top.getNext();size--;return info;

    }Java: Stacks / 38

  • cdk@it.uc3m.es Java: Stacks /

    Arrays vs. Linked lists

    Advantages of arrays: Efficient use of memory when the number ofelements is known before creation

    Fast random access (to any position)

    Disadvantages of arrays: Data needs to be moved in insertions, extractions, concatenations, etc.

    Static size (resizing is possible but requiresmovement of data)

    Needs contiguous memory

  • cdk@it.uc3m.es Java: Stacks /

    Arrays vs. Linked lists

    Advantages of linked lists: Insertions, extractions, concatenations, partitions without movement of data

    Dynamic size

    No need of contiguous memory

    Disadvantages of linked lists: Slow access to random positions

    Memory overhead to store nodes

  • cdk@it.uc3m.es Java: Stacks /

    Arrays vs. Linked lists

    Advantages of arrays: Efficient use of memory when the number ofelements is known before creation

    Fast random access (to any position)

    Disadvantages of arrays: Data needs to be moved in insertions, extractions, concatenations, etc.

    Static size (resizing is possible but requiresmovement of data)

    Needs contiguous memory

  • cdk@it.uc3m.es

    Stacks and recursion

    public static long fac (int n) {

    if (n

  • cdk@it.uc3m.es

    Execution

    fac(4)

    4*fac(3)

    4*(3*fac(2))

    4*(3*(2*fac(1)))

    4*(3*(2*1)))

    4*(3*2)

    4*6

    24

    Java: Stacks / 43

    2*

    3*4*

Recommended

View more >