Download presentation
Presentation is loading. Please wait.
Published byMarilynn Davidson Modified over 9 years ago
1
Stack Implementations Chapter 6 Copyright ©2012 by Pearson Education, Inc. All rights reserved
2
Contents A Linked Implementation An Array-Based Implementation A Vector-Based Implementation Java Class Library: The Class Vector Using a Vector to Implement the ADT Stack Copyright ©2012 by Pearson Education, Inc. All rights reserved
3
Objectives Implement ADT stack by using either Linked chain Array Vector Compare and contrast various implementations and their performance Copyright ©2012 by Pearson Education, Inc. All rights reserved
4
Linked Implementation Consider push, pop, peek Each involves top of stack Best to put top of stack at head node Fastest, easiest to access Java will manage reclaiming memory without instruction from programmer Note source code for linked implementation Listing 6-1 Listing 6-1 Note: Code listing files must be in same folder as PowerPoint files for links to work Note: Code listing files must be in same folder as PowerPoint files for links to work Copyright ©2012 by Pearson Education, Inc. All rights reserved
5
Figure 6-1 A chain of linked nodes that implements a stack Copyright ©2012 by Pearson Education, Inc. All rights reserved
6
FIGURE 6-2 (a) A new node that references the node at the top of the stack; (b) the new node is now at the top of the stack Copyright ©2012 by Pearson Education, Inc. All rights reserved
7
Linked Implementation Copyright ©2012 by Pearson Education, Inc. All rights reserved
8
Figure 6-3 The stack (a) before the first node in the chain is deleted Copyright ©2012 by Pearson Education, Inc. All rights reserved
9
Figure 6-3 The stack (b) after the first node in the chain is deleted Copyright ©2012 by Pearson Education, Inc. All rights reserved
10
Array Based Implementation Again the question: Were to place the top entry? More efficient operations with bottom of stack at beginning of array Top of stack at last occupied entry Must consider memory wastage of unused array elements Copyright ©2012 by Pearson Education, Inc. All rights reserved
11
Figure 6-4 An array that implements a stack; its first location references (a) the top entry in the stack; Copyright ©2012 by Pearson Education, Inc. All rights reserved
12
Figure 6-4 An array that implements a stack; its first location references (b) the bottom entry in the stack; Copyright ©2012 by Pearson Education, Inc. All rights reserved
13
Adding to the Top Copyright ©2012 by Pearson Education, Inc. All rights reserved
14
Retrieving the Top Copyright ©2012 by Pearson Education, Inc. All rights reserved
15
Figure 6-5 An array-based stack after its top entry is removed by (a) decrementing topIndex ; Copyright ©2012 by Pearson Education, Inc. All rights reserved
16
Figure 6-5 An array-based stack after its top entry is removed by (b) setting stack[topIndex] to null and then decrementing topIndex Copyright ©2012 by Pearson Education, Inc. All rights reserved
17
Method Pop Copyright ©2012 by Pearson Education, Inc. All rights reserved
18
Vector-Based Implementation Vector is a structure which behaves like a high level array Has methods to access entries Grows in size as needed (hidden from client) Copyright ©2012 by Pearson Education, Inc. All rights reserved
19
Figure 6-6 A client using the methods given in StackInterface ; these methods interact with a vector’s methods to perform stack operations Copyright ©2012 by Pearson Education, Inc. All rights reserved
20
Constructors and Methods of Vector public Vector() public Vector(int initialCapacity) public boolean add(T newEntry) public T remove(int index) public void clear() public T lastElement() public boolean isEmpty() public int size() Copyright ©2012 by Pearson Education, Inc. All rights reserved
21
Using a Vector to Implement ADT Stack Similar to using an array But easier First element is bottom of stack Vector adjusts size automatically Our stack implementation need not deal with adjusting size View outline, Listing 6-3Listing 6-3 Copyright ©2012 by Pearson Education, Inc. All rights reserved
22
Stack Methods for Vector Implementation Copyright ©2012 by Pearson Education, Inc. All rights reserved
23
Should VectorStack Extend Vector? If we use inheritance It would have available all methods of Vector Would allow access to any element This violates premise of ADT Stack Design decision Do NOT use inheritance Copyright ©2012 by Pearson Education, Inc. All rights reserved
24
End Chapter 6 Copyright ©2012 by Pearson Education, Inc. All rights reserved
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.