stack data structures in java
TRANSCRIPT
-
8/14/2019 Stack Data Structures in Java
1/14
Project
Array-based Stack Implementation in Java
Stack
Presented byAhmad Abbur RehmanKhayam Ahmed
-
8/14/2019 Stack Data Structures in Java
2/14
-
8/14/2019 Stack Data Structures in Java
3/14
-
8/14/2019 Stack Data Structures in Java
4/14
/*** Test if the stack is logically empty.* @return true if empty, false otherwise.
*/ public boolean isEmpty( ) { return topOfStack == -1;
}
-
8/14/2019 Stack Data Structures in Java
5/14
/*** Make the stack logically empty.*/
public void makeEmpty( ) {topOfStack = -1;}
-
8/14/2019 Stack Data Structures in Java
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 ];
}
-
8/14/2019 Stack Data Structures in Java
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--;}
-
8/14/2019 Stack Data Structures in Java
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-- ];
}
-
8/14/2019 Stack Data Structures in Java
9/14
-
8/14/2019 Stack Data Structures in Java
10/14
-
8/14/2019 Stack Data Structures in Java
11/14
-
8/14/2019 Stack Data Structures in Java
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
-
8/14/2019 Stack Data Structures in Java
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( );
-
8/14/2019 Stack Data Structures in Java
14/14
Thank You