25540842 stack data structures in java(1)

14
Project Array-based Stack Implementation in Java tack  Presented by  Ahmad Abbur Rehman Khayam Ahmed

Upload: adchy7

Post on 14-Apr-2018

231 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 25540842 Stack Data Structures in Java(1)

7/27/2019 25540842 Stack Data Structures in Java(1)

http://slidepdf.com/reader/full/25540842-stack-data-structures-in-java1 1/14

Project

Array-based Stack Implementation in Java

Stack  

Presented by

 Ahmad Abbur Rehman

Khayam Ahmed

Page 2: 25540842 Stack Data Structures in Java(1)

7/27/2019 25540842 Stack Data Structures in Java(1)

http://slidepdf.com/reader/full/25540842-stack-data-structures-in-java1 2/14

// CONSTRUCTION: with no initializer //

// ******************PUBLIC OPERATIONS*********************

// void push( x ) --> Insert x

// void pop( ) --> Remove most recently inserted item// Object top( ) --> Return most recently inserted item

// Object topAndPop( ) --

> Return and remove most recent item

// boolean isEmpty( ) --> Return true if empty; else false

// void makeEmpty( ) --> Remove all items// ******************ERRORS********************************

// top, pop, or topAndPop on empty stack

Building ArrayStack class

Page 3: 25540842 Stack Data Structures in Java(1)

7/27/2019 25540842 Stack Data Structures in Java(1)

http://slidepdf.com/reader/full/25540842-stack-data-structures-in-java1 3/14

/**

* Array-based implementation of the stack.

*/

public class ArrayStack implements Stack {/**

* Construct the stack.

*/

  public ArrayStack( ) {theArray = new Object[ DEFAULT_CAPACITY ];

topOfStack = -1;

}

 

Page 4: 25540842 Stack Data Structures in Java(1)

7/27/2019 25540842 Stack Data Structures in Java(1)

http://slidepdf.com/reader/full/25540842-stack-data-structures-in-java1 4/14

/**

* Test if the stack is logically empty.

* @return true if empty, false otherwise.

*/  public boolean isEmpty( ) {

  return topOfStack == -1;

}

Page 5: 25540842 Stack Data Structures in Java(1)

7/27/2019 25540842 Stack Data Structures in Java(1)

http://slidepdf.com/reader/full/25540842-stack-data-structures-in-java1 5/14

/**

* Make the stack logically empty.

*/

  public void makeEmpty( ) {topOfStack = -1;

}

Page 6: 25540842 Stack Data Structures in Java(1)

7/27/2019 25540842 Stack Data Structures in Java(1)

http://slidepdf.com/reader/full/25540842-stack-data-structures-in-java1 6/14

/**

* Get the most recently inserted item in the stack.

* Does not alter the stack.

* @return the most recently inserted item in the stack.* @throws UnderflowException if the stack is empty.

*/

  public Object top( ) {

  if ( isEmpty( ) )

  throw new UnderflowException( "ArrayStack top" );  return theArray[ topOfStack ];

}

Page 7: 25540842 Stack Data Structures in Java(1)

7/27/2019 25540842 Stack Data Structures in Java(1)

http://slidepdf.com/reader/full/25540842-stack-data-structures-in-java1 7/14

/**

* Remove the most recently inserted item from the stack.

* @throws UnderflowException if the stack is empty.

*/  public void pop( ) {

  if ( isEmpty( ) )

  throw new UnderflowException( "ArrayStack pop" );

topOfStack--;

}

Page 8: 25540842 Stack Data Structures in Java(1)

7/27/2019 25540842 Stack Data Structures in Java(1)

http://slidepdf.com/reader/full/25540842-stack-data-structures-in-java1 8/14

/**

* Return and remove the most recently inserted item

* from the stack.

* @return the most recently inserted item in the stack.* @throws Underflow if the stack is empty.

*/

  public Object topAndPop( ) {

  if ( isEmpty( ) )

  throw new UnderflowException( "ArrayStack topAndPop" );

  return theArray[ topOfStack-- ];

}

Page 9: 25540842 Stack Data Structures in Java(1)

7/27/2019 25540842 Stack Data Structures in Java(1)

http://slidepdf.com/reader/full/25540842-stack-data-structures-in-java1 9/14

/**

* Insert a new item into the stack.

* @param x the item to insert.

*/  public void push( Object x ) {

  if ( topOfStack + 1 == theArray.length )

doubleArray( );

theArray[ ++topOfStack ] = x;

}

Page 10: 25540842 Stack Data Structures in Java(1)

7/27/2019 25540842 Stack Data Structures in Java(1)

http://slidepdf.com/reader/full/25540842-stack-data-structures-in-java1 10/14

/**

* Internal method to extend theArray.

*/

  private void doubleArray( ) {Object [ ] newArray;

 

newArray = new Object[ theArray.length * 2 ];

  for ( int i = 0; i < theArray.length; i++ )

newArray[ i ] = theArray[ i ];theArray = newArray;

}

 

private Object [ ] theArray;  private int topOfStack;

 

private static final int DEFAULT_CAPACITY = 10;

 

}

Page 11: 25540842 Stack Data Structures in Java(1)

7/27/2019 25540842 Stack Data Structures in Java(1)

http://slidepdf.com/reader/full/25540842-stack-data-structures-in-java1 11/14

/**

* Exception class for access in empty containers

* such as stacks, queues, and priority queues.

* @author Mark Allen Weiss*/

public class UnderflowException extends RuntimeException

{

/**

* Construct this exception object.* @param message the error message.

*/

  public UnderflowException( String message ) {

  super ( message );}

}

Page 12: 25540842 Stack Data Structures in Java(1)

7/27/2019 25540842 Stack Data Structures in Java(1)

http://slidepdf.com/reader/full/25540842-stack-data-structures-in-java1 12/14

//// ******************PUBLIC OPERATIONS*********************

// void push( x ) --> Insert x

// void pop( ) --> Remove most recently inserted item

// Object top( ) --> Return most recently inserted item// Object topAndPop( ) --

> Return and remove most recent item

// boolean isEmpty( ) --> Return true if empty; else false

// void makeEmpty( ) --> Remove all items

// ******************ERRORS********************************

// top, pop, or topAndPop on empty stack

Creating Stack Interface

Page 13: 25540842 Stack Data Structures in Java(1)

7/27/2019 25540842 Stack Data Structures in Java(1)

http://slidepdf.com/reader/full/25540842-stack-data-structures-in-java1 13/14

/**

* Protocol for stacks.

*/

  public interface Stack {/**

* Insert a new item into the stack.

* @param x the item to insert.

*/

  void push( Object x ); 

/**

* Remove the most recently inserted item from the stack

.* @exception UnderflowException if the stack is empty.

*/

  void pop( );

Page 14: 25540842 Stack Data Structures in Java(1)

7/27/2019 25540842 Stack Data Structures in Java(1)

http://slidepdf.com/reader/full/25540842-stack-data-structures-in-java1 14/14

Thank You