CS212: Data Structure - fac.ksu.edu.safac.ksu.edu.sa/sites/default/files/slides_08.pdf · Elements:...

90
CS212: Data Structure 1

Transcript of CS212: Data Structure - fac.ksu.edu.safac.ksu.edu.sa/sites/default/files/slides_08.pdf · Elements:...

CS212: Data Structure

1

Stack: Last In First Out (LIFO). ◦ Used in procedure calls, to compute arithmetic

expressions etc.

Used in Operating system to implement method calls, and in evaluating Expirations.

2

Elements: The elements are of a generic type <Type>. (In a linked implementation an element is placed in a node)

Structure: the elements are linearly arranged, and ordered according to the order of arrival, most recently arrived element is called top.

Domain: the number of elements in the stack is bounded therefore the domain is finite. Type of elements: Stack

3

Operations: All operations operate on a stack S. 1. Method Push (Type e) requires: Stack S is not full. input: Type e. results: Element e is added to the stack as its most recently

added elements. output: none. 2. Method Pop (Type e) requires: Stack S is not empty. input: results: the most recently arrived element in S is removed

and its value assigned to e. output: Type e. 3. Method Empty (boolean flag) input: results: If Stack S is empty then flag is true,

otherwise false. output: flag.

4

Operations:

4. Method Full (boolean flag).

requires: input: .

results: If S is full then Full is true, otherwise Full is false. output: flag.

5

6

T

null

public class Node<T> { public T data; public Node<T> next; public Node () { data = null; next = null; } public Node (T val) { data = val; next = null; } // Setters/Getters? }

7

public class LinkedStack<T> {

private Node<T> top;

/* Creates a new instance of LinkStack */

public LinkStack() {

top = null;

}

8

public class LinkedStack<T> {

private Node<T> top;

/* Creates a new instance of LinkStack */

public LinkStack() {

top = null;

}

9

T null

public boolean empty(){

return top == null;

}

public boolean full(){

return false;

}

10

public boolean empty(){

return top == null;

}

public boolean full(){

return false;

}

11

T null

T

null

false

true

public void push(T e){

Node<T> tmp = new Node(e);

tmp.next = top;

top = tmp;

}

12

public void push(T e){

Node<T> tmp = new Node(e);

tmp.next = top;

top = tmp;

}

13

T null

Example #1

public void push(T e){

Node<T> tmp = new Node(e);

tmp.next = top;

top = tmp;

}

14

T null

Example #1

tmp

public void push(T e){

Node<T> tmp = new Node(e);

tmp.next = top;

top = tmp;

}

15

T null

Example #1

tmp

public void push(T e){

Node<T> tmp = new Node(e);

tmp.next = top;

top = tmp;

}

16

T

null

Example #1

tmp

public void push(T e){

Node<T> tmp = new Node(e);

tmp.next = top;

top = tmp;

}

17

T

null

Example #2

public void push(T e){

Node<T> tmp = new Node(e);

tmp.next = top;

top = tmp;

}

18

T

null

Example #2

tmp

public void push(T e){

Node<T> tmp = new Node(e);

tmp.next = top;

top = tmp;

}

19

T

null

Example #2

tmp

public void push(T e){

Node<T> tmp = new Node(e);

tmp.next = top;

top = tmp;

}

20

T

null

Example #2

tmp

public void push(T e){

Node<T> tmp = new Node(e);

tmp.next = top;

top = tmp;

}

21

T

null

Example #3

public void push(T e){

Node<T> tmp = new Node(e);

tmp.next = top;

top = tmp;

}

22

T

null

Example #3

tmp

public void push(T e){

Node<T> tmp = new Node(e);

tmp.next = top;

top = tmp;

}

23

T

null

Example #3

tmp

public void push(T e){

Node<T> tmp = new Node(e);

tmp.next = top;

top = tmp;

}

24

T

null

Example #3

tmp

public void push(T e){

Node<T> tmp = new Node(e);

tmp.next = top;

top = tmp;

}

25

T

null

Example #3

public T pop(){

T e = top.data;

top = top.next;

return e;

}

}

26

public T pop(){

T e = top.data;

top = top.next;

return e;

}

}

27

T

null

Example #1

public T pop(){

T e = top.data;

top = top.next;

return e;

}

}

28

T

null

Example #1

e

public T pop(){

T e = top.data;

top = top.next;

return e;

}

}

29

null

Example #1

e

T

public T pop(){

T e = top.data;

top = top.next;

return e;

}

}

30

null

Example #1

e

T

public T pop(){

T e = top.data;

top = top.next;

return e;

}

}

31

null

Example #1

T

public T pop(){

T e = top.data;

top = top.next;

return e;

}

}

32

null

Example #2

T

public T pop(){

T e = top.data;

top = top.next;

return e;

}

}

33

null

Example #2

T

e

public T pop(){

T e = top.data;

top = top.next;

return e;

}

}

34

null

Example #2

e

T

public T pop(){

T e = top.data;

top = top.next;

return e;

}

}

35

null

Example #2

e

T

public T pop(){

T e = top.data;

top = top.next;

return e;

}

}

36

null

Example #2

T

public T pop(){

T e = top.data;

top = top.next;

return e;

}

}

37

null

Example #3

T

public T pop(){

T e = top.data;

top = top.next;

return e;

}

}

38

null

Example #3

T

e

public T pop(){

T e = top.data;

top = top.next;

return e;

}

}

39

null

Example #3

e

T

public T pop(){

T e = top.data;

top = top.next;

return e;

}

}

40

null

Example #3

e

T

public T pop(){

T e = top.data;

top = top.next;

return e;

}

}

41

null

Example #3

T

public T pop(){

T e = top.data;

top = top.next;

return e;

}

}

42

null

Example #3

T

43

0

1

2

3

T

max n

n-1

.

.

.

.

public class ArrayStack<T> {

private int maxsize;

private int top;

private T[] nodes;

/** Creates a new instance of ArrayStack */

public ArrayStack(int n) {

maxsize = n;

top = -1;

nodes = (T[]) new Object[n];

}

44

public class ArrayStack<T> {

private int maxsize;

private int top;

private T[] nodes;

/** Creates a new instance of ArrayStack */

public ArrayStack(int n) {

maxsize = n;

top = -1;

nodes = (T[]) new Object[n];

}

45

0

1

2

3

T

max 4

public boolean empty(){

return top == -1;

}

public boolean full(){

return top == maxsize - 1;

}

46

public boolean empty(){

return top == -1;

}

public boolean full(){

return top == maxsize - 1;

}

47

0

1

2

3

T

max 4

true

0

1

2

3

T

max 4

false

public boolean empty(){

return top == -1;

}

public boolean full(){

return top == maxsize - 1;

}

48

0

1

2

3 T

max 4

true

0

1

2

3

T

max 4

false

public void push(T e){

nodes[++top] = e;

}

public T pop(){

return nodes[top--];

}

}

49

public void push(T e){

nodes[++top] = e;

}

public T pop(){

return nodes[top--];

}

}

50

0

1

2

3

T

max 4

Example #1

public void push(T e){

nodes[++top] = e;

}

public T pop(){

return nodes[top--];

}

}

51

0

1

2

3

max 4

Example #1

T

S1 ← ++top

nodes[S1] = e

public void push(T e){

nodes[++top] = e;

}

public T pop(){

return nodes[top--];

}

}

52

0

1

2

3

max 4

Example #1

e

T

S1 ← ++top

nodes[S1] = e

public void push(T e){

nodes[++top] = e;

}

public T pop(){

return nodes[top--];

}

}

53

0

1

2

3

max 4

Example #2

T

public void push(T e){

nodes[++top] = e;

}

public T pop(){

return nodes[top--];

}

}

54

0

1

2

3

max 4

Example #2

T

S1 ← ++top

nodes[S1] = e

public void push(T e){

nodes[++top] = e;

}

public T pop(){

return nodes[top--];

}

}

55

0

1

2

3

max 4

Example #2

T

e

S1 ← ++top

nodes[S1] = e

public void push(T e){

nodes[++top] = e;

}

public T pop(){

return nodes[top--];

}

}

56

0

1

2

3

max 4

Example #3

T

public void push(T e){

nodes[++top] = e;

}

public T pop(){

return nodes[top--];

}

}

57

0

1

2

3

max 4

Example #3

T S1 ← nodes[top]

top-- return S1

public void push(T e){

nodes[++top] = e;

}

public T pop(){

return nodes[top--];

}

}

58

0

1

2

3

max 4

Example #3

T

S1 ← nodes[top]

top-- return S1

public void push(T e){

nodes[++top] = e;

}

public T pop(){

return nodes[top--];

}

}

59

0

1

2

3

max 4

Example #4

T

public void push(T e){

nodes[++top] = e;

}

public T pop(){

return nodes[top--];

}

}

60

0

1

2

3

max 4

Example #4

T

S1 ← nodes[top]

top-- return S1

public void push(T e){

nodes[++top] = e;

}

public T pop(){

return nodes[top--];

}

}

61

0

1

2

3

max 4

Example #4

T

S1 ← nodes[top]

top-- return S1

public void push(T e){

nodes[++top] = e;

}

public T pop(){

return nodes[top--];

}

}

62

0

1

2

3

max 4

Example #4

T

63

Some applications of stacks are: ◦ Balancing symbols.

◦ Computing or evaluating postfix expressions.

◦ Converting expressions from infix to postfix.

64

Expressions: mathematical (a + ((b-c)*d)) or programs have delimiters.

begin {

S1 S1

S2 {

begin S2

S3 S3

begin } …. S4

end }

end

end

65

Delimiters must be balanced. [()] is legal but [(]) illegal.

A stack can be used to check if the delimiters are balanced. ◦ Read characters from the start of the expression to the

end. ◦ If the token is a starting delimiter, then push on to the

stack. ◦ If the token is a closing delimiter, then pop from the

stack. If symbol from this pop operation matches the closing

delimiter, then we carry on. If not, or the stack was empty, then we have unbalanced

symbols (report an error). ◦ If stack is empty at the end of expression, we have

balanced symbols. ◦ If not (stack is not empty), then we have unbalanced

symbols (report an error).

66

Evaluating Postfix Expressions: ◦ Infix expression: 4.99*1.06+5.99+6.99*1.06

◦ Value 18.69 correct parenthesis used.

◦ Value 19.37 incorrect no parenthesis used.

◦ In postfix form, above expression becomes:

4.99 1.06 * 5.99 + 6.99 1.06*+

Advantage: no brackets are needed and a stack can be used to compute the expression.

67

Example: ◦ infix: 6*(5+((2+3)*8)+3) ◦ postfix: 6 5 2 3 + 8 * + 3 + *.

Algorithm to compute postfix expression: ◦ Read the postfix expression left to right. ◦ When a number is read push it on the stack. ◦ When an operator is read:

pop two numbers from the stack

carry out the operation on them

push the result back on the stack.

68

Example: ◦ infix: 6*(5+((2+3)*8)+3) ◦ postfix: 6 5 2 3 + 8 * + 3 + *.

Algorithm to compute postfix expression: ◦ Read the postfix expression left to right. ◦ When a number is read push it on the stack. ◦ When an operator is read:

pop two numbers from the stack

carry out the operation on them

push the result back on the stack.

69

Example: ◦ infix: 6*(5+((2+3)*8)+3) ◦ postfix: 6 5 2 3 + 8 * + 3 + *.

Algorithm to compute postfix expression: ◦ Read the postfix expression left to right. ◦ When a number is read push it on the stack. ◦ When an operator is read:

pop two numbers from the stack

carry out the operation on them

push the result back on the stack.

6

70

Example: ◦ infix: 6*(5+((2+3)*8)+3) ◦ postfix: 6 5 2 3 + 8 * + 3 + *.

Algorithm to compute postfix expression: ◦ Read the postfix expression left to right. ◦ When a number is read push it on the stack. ◦ When an operator is read:

pop two numbers from the stack

carry out the operation on them

push the result back on the stack.

5

6

71

Example: ◦ infix: 6*(5+((2+3)*8)+3) ◦ postfix: 6 5 2 3 + 8 * + 3 + *.

Algorithm to compute postfix expression: ◦ Read the postfix expression left to right. ◦ When a number is read push it on the stack. ◦ When an operator is read:

pop two numbers from the stack

carry out the operation on them

push the result back on the stack.

2

5

6

72

Example: ◦ infix: 6*(5+((2+3)*8)+3) ◦ postfix: 6 5 2 3 + 8 * + 3 + *.

Algorithm to compute postfix expression: ◦ Read the postfix expression left to right. ◦ When a number is read push it on the stack. ◦ When an operator is read:

pop two numbers from the stack

carry out the operation on them

push the result back on the stack.

3

2

5

6

73

Example: ◦ infix: 6*(5+((2+3)*8)+3) ◦ postfix: 6 5 2 3 + 8 * + 3 + *.

Algorithm to compute postfix expression: ◦ Read the postfix expression left to right. ◦ When a number is read push it on the stack. ◦ When an operator is read:

pop two numbers from the stack

carry out the operation on them

push the result back on the stack.

3

2

5

6

74

Example: ◦ infix: 6*(5+((2+3)*8)+3) ◦ postfix: 6 5 2 3 + 8 * + 3 + *.

Algorithm to compute postfix expression: ◦ Read the postfix expression left to right. ◦ When a number is read push it on the stack. ◦ When an operator is read:

pop two numbers from the stack

carry out the operation on them

push the result back on the stack.

5

6

2 + 3 = 5

75

Example: ◦ infix: 6*(5+((2+3)*8)+3) ◦ postfix: 6 5 2 3 + 8 * + 3 + *.

Algorithm to compute postfix expression: ◦ Read the postfix expression left to right. ◦ When a number is read push it on the stack. ◦ When an operator is read:

pop two numbers from the stack

carry out the operation on them

push the result back on the stack.

5

5

6

2 + 3 = 5

76

Example: ◦ infix: 6*(5+((2+3)*8)+3) ◦ postfix: 6 5 2 3 + 8 * + 3 + *.

Algorithm to compute postfix expression: ◦ Read the postfix expression left to right. ◦ When a number is read push it on the stack. ◦ When an operator is read:

pop two numbers from the stack

carry out the operation on them

push the result back on the stack.

8

5

5

6

77

Example: ◦ infix: 6*(5+((2+3)*8)+3) ◦ postfix: 6 5 2 3 + 8 * + 3 + *.

Algorithm to compute postfix expression: ◦ Read the postfix expression left to right. ◦ When a number is read push it on the stack. ◦ When an operator is read:

pop two numbers from the stack

carry out the operation on them

push the result back on the stack.

8

5

5

6

78

Example: ◦ infix: 6*(5+((2+3)*8)+3) ◦ postfix: 6 5 2 3 + 8 * + 3 + *.

Algorithm to compute postfix expression: ◦ Read the postfix expression left to right. ◦ When a number is read push it on the stack. ◦ When an operator is read:

pop two numbers from the stack

carry out the operation on them

push the result back on the stack.

5

6

5 * 8 = 40

79

Example: ◦ infix: 6*(5+((2+3)*8)+3) ◦ postfix: 6 5 2 3 + 8 * + 3 + *.

Algorithm to compute postfix expression: ◦ Read the postfix expression left to right. ◦ When a number is read push it on the stack. ◦ When an operator is read:

pop two numbers from the stack

carry out the operation on them

push the result back on the stack.

40

5

6

5 * 8 = 40

80

Example: ◦ infix: 6*(5+((2+3)*8)+3) ◦ postfix: 6 5 2 3 + 8 * + 3 + *.

Algorithm to compute postfix expression: ◦ Read the postfix expression left to right. ◦ When a number is read push it on the stack. ◦ When an operator is read:

pop two numbers from the stack

carry out the operation on them

push the result back on the stack.

40

5

6

81

Example: ◦ infix: 6*(5+((2+3)*8)+3) ◦ postfix: 6 5 2 3 + 8 * + 3 + *.

Algorithm to compute postfix expression: ◦ Read the postfix expression left to right. ◦ When a number is read push it on the stack. ◦ When an operator is read:

pop two numbers from the stack

carry out the operation on them

push the result back on the stack.

6

5 + 40 = 45

82

Example: ◦ infix: 6*(5+((2+3)*8)+3) ◦ postfix: 6 5 2 3 + 8 * + 3 + *.

Algorithm to compute postfix expression: ◦ Read the postfix expression left to right. ◦ When a number is read push it on the stack. ◦ When an operator is read:

pop two numbers from the stack

carry out the operation on them

push the result back on the stack.

45

6

5 + 40 = 45

83

Example: ◦ infix: 6*(5+((2+3)*8)+3) ◦ postfix: 6 5 2 3 + 8 * + 3 + *.

Algorithm to compute postfix expression: ◦ Read the postfix expression left to right. ◦ When a number is read push it on the stack. ◦ When an operator is read:

pop two numbers from the stack

carry out the operation on them

push the result back on the stack.

3

45

6

84

Example: ◦ infix: 6*(5+((2+3)*8)+3) ◦ postfix: 6 5 2 3 + 8 * + 3 + *.

Algorithm to compute postfix expression: ◦ Read the postfix expression left to right. ◦ When a number is read push it on the stack. ◦ When an operator is read:

pop two numbers from the stack

carry out the operation on them

push the result back on the stack.

3

45

6

85

Example: ◦ infix: 6*(5+((2+3)*8)+3) ◦ postfix: 6 5 2 3 + 8 * + 3 + *.

Algorithm to compute postfix expression: ◦ Read the postfix expression left to right. ◦ When a number is read push it on the stack. ◦ When an operator is read:

pop two numbers from the stack

carry out the operation on them

push the result back on the stack.

6

45 + 3 = 48

86

Example: ◦ infix: 6*(5+((2+3)*8)+3) ◦ postfix: 6 5 2 3 + 8 * + 3 + *.

Algorithm to compute postfix expression: ◦ Read the postfix expression left to right. ◦ When a number is read push it on the stack. ◦ When an operator is read:

pop two numbers from the stack

carry out the operation on them

push the result back on the stack.

48

6

45 + 3 = 48

87

Example: ◦ infix: 6*(5+((2+3)*8)+3) ◦ postfix: 6 5 2 3 + 8 * + 3 + *.

Algorithm to compute postfix expression: ◦ Read the postfix expression left to right. ◦ When a number is read push it on the stack. ◦ When an operator is read:

pop two numbers from the stack

carry out the operation on them

push the result back on the stack.

48

6

88

Example: ◦ infix: 6*(5+((2+3)*8)+3) ◦ postfix: 6 5 2 3 + 8 * + 3 + *.

Algorithm to compute postfix expression: ◦ Read the postfix expression left to right. ◦ When a number is read push it on the stack. ◦ When an operator is read:

pop two numbers from the stack

carry out the operation on them

push the result back on the stack.

6 * 48 = 288

89

Example: ◦ infix: 6*(5+((2+3)*8)+3) ◦ postfix: 6 5 2 3 + 8 * + 3 + *.

Algorithm to compute postfix expression: ◦ Read the postfix expression left to right. ◦ When a number is read push it on the stack. ◦ When an operator is read:

pop two numbers from the stack

carry out the operation on them

push the result back on the stack.

288

6 * 48 = 288

90

Example: ◦ infix: 6*(5+((2+3)*8)+3) ◦ postfix: 6 5 2 3 + 8 * + 3 + *.

Algorithm to compute postfix expression: ◦ Read the postfix expression left to right. ◦ When a number is read push it on the stack. ◦ When an operator is read:

pop two numbers from the stack

carry out the operation on them

push the result back on the stack.

288 result

End!