Presentation is loading. Please wait.

Presentation is loading. Please wait.

Ics202 Data Structures. U n i v e r s i t y o f H a i l 1. Stacks 1 7 2 1 7 2 8 1 7 2 8 2 top push (8)push (2)

Similar presentations


Presentation on theme: "Ics202 Data Structures. U n i v e r s i t y o f H a i l 1. Stacks 1 7 2 1 7 2 8 1 7 2 8 2 top push (8)push (2)"— Presentation transcript:

1 Ics202 Data Structures

2 U n i v e r s i t y o f H a i l 1. Stacks 1 7 2 1 7 2 8 1 7 2 8 2 top push (8)push (2)

3 U n i v e r s i t y o f H a i l 1. Stacks 1 7 2 1 7 2 8 1 7 2 8 2 top pop ( )

4 public interface Stack extends Container { // the interface Stack inherits all the methods defined in Container Object getTop (); void push (Object object); // add an element at the top of the stack Object pop (); // delete the top of the stack } public class StackAsArray extends AbstractContainer implements Stack { protected Object [ ] array; public StackAsArray (int size) { array = new Object [size]; // call of the constructor to initialize the } // array with size elements public void purge () // delete all the array’s elements { // count must be initialized with array.length while (count > 0) array [--count] = null; } //...} Java Class

5 public class StackAsArray extends AbstractContainer implements Stack { protected Object [ ] array; public void push (Object object) { if (count == array.length) \\ count from AbstractContainer class to get the number of items in the container throw new ContainerFullException (); array [count++] = object; } public Object pop () { if (count == 0) throw new ContainerEmptyException (); Object result = array [--count]; array [count] = null; return result; }

6 public Object getTop () { if (count == 0) throw new ContainerEmptyException (); return array [count - 1]; } //... } public class StackAsArray extends AbstractContainer implements Stack { // to accept a visitor and to cause it to visit one-by-one all of the // contained objects. protected Object [ ] array; public void accept (Visitor visitor) { for (int i = 0; i < count; ++i) { visitor.visit (array [i]); if (visitor.isDone ()) return; } } //...}

7 public class StackAsArray extends AbstractContainer implements Stack { // to access one-by-one all of the objects in a container. protected Object [ ] array; public Enumeration getEnumeration ( ) { return new Enumeration () { protected int position = 0; public boolean hasMoreElements () { return position = getCount ()) throw new NoSuchElementException (); return array [position++]; } } //...}

8 // An enumeration is meant to be used like this is meant to be used like this Stack stack = new StackAsArray (57); stack.push (new Integer (3)); stack.push (new Integer (1)); stack.push (new Integer (4)); Enumeration e = stack.getEnumeration (); while (e.hasMoreElements ()) { Object obj = e.nextElement; System.out.println (obj); }

9 import ADTs.StackAsArray; import ADTs.Enumeration; import ADTs.Visitor; import ADTs.IntegerPrintingVisitor; class StackAsArrayTest { public static void main(String[] args) { StackAsArray stack = new StackAsArray(10); StackAsArray stack2 = new StackAsArray(10); Object obj; stack.push (new Integer(3)); stack.push (new Integer(1)); stack.push (new Integer(5)); System.out.println("Visiting Stack Elements Using Visitor"); Visitor v = new IntegerPrintingVisitor(); stack.accept(v);

10 stack2.push (new Integer(3)); stack2.push (new Integer(1)); System.out.println(stack.getCount()); if (stack2.isLT(stack)) System.out.println("stack2 has less elements than stack"); obj = stack.pop(); System.out.println(obj); obj = stack.pop(); System.out.println(obj); obj = stack.pop(); System.out.println(obj); obj = stack.getTop(); System.out.println(obj); } }


Download ppt "Ics202 Data Structures. U n i v e r s i t y o f H a i l 1. Stacks 1 7 2 1 7 2 8 1 7 2 8 2 top push (8)push (2)"

Similar presentations


Ads by Google