Docsity

Implementing Stacks: Deriving from ArrayLinearList and Chain vs. Code from Scratch, Slides of Computer Science

Partial preview of the text

Download Implementing Stacks: Deriving from ArrayLinearList and Chain vs. Code from Scratch and more Slides Computer Science in PDF only on Docsity!

Stacks

public interface Stack

public boolean empty();

public Object peek();

public void push(Object theObject);

public Object pop();

Derive From A Linear List Class

  • ArrayLinearList
  • Chain

Derive From ArrayLinearList

• when top is left end of linear list

 push(theObject) => add(0, theObject)

 O(size) time

 pop() => remove(0)

 O(size) time

0 1 2 3 4 5 6

a b c d e

Derive From ArrayLinearList

 when top is right end of linear list

  • push(theObject) => add(size(), theObject)
  • O(1) time
  • pop() => remove(size()-1)
  • O(1) time

 use right end of list as top of stack

0 1 2 3 4 5 6

a b c d e

Derive From Chain

a b c d e

null

firstNode

– when top is left end of linear list

 peek() => get(0)  O(1) time  push(theObject) => add(0, theObject)  O(1) time  pop() => remove(0)  O(1) time Docsity.com

Derive From Chain

a b c d e

null

firstNode

– when top is right end of linear list

  • peek() => get(size() - 1)
  • O(size) time
  • push(theObject) => add(size(), theObject)
  • O(size) time
  • pop() => remove(size()-1)
  • O(size) time

– use left end of list as top of stack Docsity.com

Constructors

/** create a stack with the given initial

  • capacity */

public DerivedArrayStack(int initialCapacity)

{super(initialCapacity);}

/** create a stack with initial capacity 10 */

public DerivedArrayStack()

{this(10);}

empty() And peek()

public boolean empty()

{return isEmpty();}

public Object peek()

{

if (empty()) throw new EmptyStackException(); return get(size() - 1)

}

0 1 2 3 4 5 6

a b c d e

Evaluation

  • Merits of deriving from ArrayLinearList

 Code for derived class is quite simple and easy to develop.  Code is expected to require little debugging.  Code for other stack implementations such as a linked implementation are easily obtained.

  • Just replace extends ArrayLinearList with extends Chain
  • For efficiency reasons we must also make changes to use the left end of the list as the stack top rather than the right end.

Demerits

  • All public methods of ArrayLinearList may be

performed on a stack.

 get(0) … get bottom element  remove(5)  add(3, x)  So we do not have a true stack implementation.  Must override undesired methods.

public Object get(int theIndex) {throw new UnsupportedOperationException();} Change earlier use of get(i) to super.get(i). Docsity.com

Evaluation

  • Code developed from scratch will run faster but

will take more time (cost) to develop.

  • Tradeoff between software development cost

and performance.

  • Tradeoff between time to market and

performance.

  • Could develop easy code first and later refine it

to improve performance.

A Faster pop()

if (empty())

throw new EmptyStackException(); return remove(size() - 1);

vs.

try {return remove(size() - 1);}

catch(IndexOutOfBoundsException e)

{throw new EmptyStackException();}

Code From Scratch

package dataStructures;

import java.util.EmptyStackException;

import utilities.*; // ChangeArrayLength

public class ArrayStack implements Stack

{

// data members int top; // current top of stack Object [] stack; // element array // constructors come here // Stack interface methods come here

}

Constructors

public ArrayStack(int initialCapacity)

{

if (initialCapacity < 1) throw new IllegalArgumentException ("initialCapacity must be >= 1"); stack = new Object [initialCapacity]; top = -1;

}

public ArrayStack()

{this(10);}