Transcript

[email protected]

Stacks

Carlos Delgado Kloos

Dep. Ingeniería Telemática

Univ. Carlos III de Madrid

Java: Stacks / 1

[email protected]

Example

Java: Stacks / 2

[email protected]

Example

Java: Stacks / 3

[email protected]

Example

Java: Stacks / 4

[email protected]

Characteristics

❚ Linear structure

❚ Access on one end both for insertion andextraction

Java: Stacks / 5

[email protected]

Main methods

❚ Insert on one end: push(x)

❚ Extract at the same end: pop()

Java: Stacks / 6

[email protected]

Example:

Check parentheses

❚ Good:

�()

�(()(()))

❚ Bad:

�)(

�(()

�())

Java: Stacks / 7

❚ Rules:

❙ Basic:

❙ Sequentiation: ()()

❙ Nesting: (())

[email protected]

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

[email protected]

Example:

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

Java: Stacks / 9

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

[email protected]

Example:

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

Java: Stacks / 10

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

(

[email protected]

Example:

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

Java: Stacks / 11

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

((

[email protected]

Example:

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

Java: Stacks / 12

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

(

[email protected]

Example:

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

Java: Stacks / 13

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

((

[email protected]

Example:

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

Java: Stacks / 14

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

(((

[email protected]

Example:

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

Java: Stacks / 15

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

((

[email protected]

Example:

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

Java: Stacks / 16

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

(((

[email protected]

Example:

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

Java: Stacks / 17

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

((

[email protected]

Example:

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

Java: Stacks / 18

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

(

[email protected]

Example:

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

Java: Stacks / 19

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

((

[email protected]

Example:

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

Java: Stacks / 20

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

(

[email protected]

Example:

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

Java: Stacks / 21

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

Correct: We have completed thestring and the stack is empty

[email protected]

Example:

check ([]{()<>}())

Java: Stacks / 22

([]{()<>}())

Correct: We have completed thestring and the stack is empty

Example: HTML

❚ <b><i>hello</b></i>�([)]

�Correct with HTML 1.0-4.0

�Incorrect with XHTML

❚ <b><i>hello</i></b>�([])

�Correct with HTML 1.0-4.0

�Correct with XHTML

[email protected] Java: Stacks / 23

[email protected]

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

[email protected]

One interface and

several implementations

Java: Stacks / 25

ArrayStack

Stack

LinkedStack

[email protected]

Array-based

implementation

Java: Stacks / 26

data

0 1 2 N-13 4 5

1

top

2

top

3

top

4

top

5

toptop

[email protected]

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

[email protected]

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

[email protected]

Array-based

implementation

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

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

}

Java: Stacks / 29

[email protected]

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

[email protected] Java: Stacks /[email protected]

Implementation

based on linked lists

Java: Stacks / 31

Madrid Miami Munich

top

[email protected]

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

[email protected]

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

[email protected] Java: Stacks /[email protected]

Insertion (push)

Java: Stacks / 34

Moscow

Madrid Miami Munich

top

[email protected]

Implementation

based on linked lists

public void push(Object info) {

Node n = new Node(info, top);

top = n;

size++;

}

Java: Stacks / 35

[email protected]

Implementation

based on linked lists

public Object top()

throws EmptyStackException {

if (top == null)

throw new EmptyStackException();

return top.getInfo();

}

Java: Stacks / 36

[email protected] Java: Stacks /[email protected]

Extraction (pop)

Java: Stacks / 37

Madrid Miami MunichMoscow

top

[email protected]

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

[email protected] 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

[email protected] 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

[email protected] 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

[email protected]

Stacks and recursion

public static long fac (int n) {

if (n <= 1)return 1;

elsereturn n * fac(n - 1);

}

Java: Stacks / 42

[email protected]

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*


Top Related