Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Similar presentations


Presentation on theme: "Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved."— Presentation transcript:

1 Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

2 Overview ● Stacks and Queues – Data structure holding a collection of objects, ordered by when they were inserted into the structure. – Stack can access newest object. – Queue can access oldest object.

3 Overview ● 4.1 – The Stack Interface ● Stack behavior ● Generic classes ● 4.2 – The Java Call Stack ● 4.4 – The Queue Interface

4 The Stack Interface ● Stack – Data structure holding several items. – New items added to the top of the stack. ● Pushing – Push items onto the stack ● Pop – Pop items off of the stack or extract the topmost item. ● Peek – Peek at the top item or check if the stack is empty.

5 The Stack Interface

6 ● Iast-in, first-out or LIFO policy – Last item pushed on the stack is next item popped off the stack. – Example: ● A stack of plates.

7 The Stack Interface

8

9 ● Generics – The Stack interface describes a stack of Objects. ● s.push(new Die(3)); – When we pop a Stack we get back an Object. – We usually cast it to a more specific class so we can use specific functionality. ● ((Die)(s.pop())).roll(); – This is not only inconvenient, it is slightly unsafe. – We must remember what we put on the Stack, or we might try to cast something that is not really a Die to be a Die.

10 The Stack Interface ● Generic classes and interfaces – Has one or more type parameters. – We have to specify a type for each type of parameter. ● Stack s = new Stack (): ● s.push(new Die()); s.pop().roll(); – Java won't let us push anything of the wrong type onto this Stack. – The pop() method's return type is specified by the type parameter.

11 The Stack Interface

12 ● In UML diagrams type parameters are shown as dashed boxes are at the upper right of a class.

13 The Stack Interface

14

15

16

17 ● Generics do not play well with arrays – We can declare an array field or variable involving type parameters, like the field stacks. – We cannot actually allocate an array involving a type parameter. ● Example: stacks = new Stack [4] – Assigning each cell in the array a generic type parameter produces a warning, but not an error.

18 The Stack Interface

19

20

21 ● The pop() method returns a value, but we don't use that value in this particular program.

22 The Stack Interface

23

24

25

26

27 The Call Stack

28

29 ● The hypotenuse() method invokes the square() method twice. ● How does Java know where to go when it finishes an invocation of square()? ● Java keeps track of what it was doing before the invocation started by using a stack. – This stack is called a call stack.

30 The Call Stack ● Every time a method is invoked, a call frame is created. – The call frame keeps track of the current state of the method. – It stores any arguments or variables for the method. – It also keeps track of how far along the method has proceeded.

31 The Call Stack

32 ● Knowledge of the call stack helps us understand some of Java's error messages. ● The error message shows a stack trace (a snapshot of the call stack) indicating what was going on when the program crashed.

33 The Call Stack

34 The Queue Interface ● Queue – Very similar to a stack. – Items are inserted in one end (the back) and removed from the other end (the front). – first-in, first-out, or FIFO

35 The Queue Interface

36

37

38

39

40

41

42

43

44

45

46

47

48 Summary ● Stacks and queues are collections of objects. – Stack follows a last-in, first-out policy. ● Objects are pushed onto and popped off the top. – Queue follows a first-in, first-out policy. ● Objects are added to the back and removed from the front.

49 Summary ● Java 1.5 introduces generic types and interfaces. – Generics greatly reduces the amount of casting we have to do when using general-purpose data structures.

50 Summary ● Call Stack – Whenever a method is invoked, a new call frame is pushed onto the stack. ● This keeps track of any variables in the method, as well as how far the method has progressed.


Download ppt "Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved."

Similar presentations


Ads by Google