Objectives Introduction Operations | Komputasi | Suatu Permulaan

J.E.D.I 2 Stacks

2.1 Objectives

At the end of the lesson, the student should be able to: • Explain the basic concepts and operations on the ADT stack • Implement the ADT stack using sequential and linked representation • Discuss applications of stack: the pattern recognition problem and conversion from infix to postfix • Explain how multiple stacks can be stored using one-dimensional array • Reallocate memory during stack overflow in multiple-stack array using unit-shift policy and Garwicks algorithm

2.2 Introduction

Stack is a linearly ordered set of elements having the the discipline of last-in, first out, hence it is also known as LIFO list. It is similar to a stack of boxes in a warehouse, where only the top box could be retrieved and there is no access to the other boxes. Also, adding a box means putting it at the top of the stack. Stacks are used in pattern recognition, lists and tree traversals, evaluation of expressions, resolving recursions and a lot more. The two basic operations for data manipulation are push and pop, which are insertion into and deletion from the top of stack respectively. Just like what was mentioned in Chapter 1, interface Application Program Interface or API is used to implement ADT in Java. The following is the Java interface for stack: public interface Stack{ public int size; returns the size of the stack public boolean isEmpty; checks if empty public Object top throws StackException; public Object pop throws StackException; public void pushObject item throws StackException; } StackException is an extension of RuntimeException: class StackException extends RuntimeException{ public StackExceptionString err{ supererr; } } Stacks has two possible implementations – a sequentially allocated one-dimensional Data Structures 22 J.E.D.I array vector or a linked linear list. However, regardless of implementation, the interface Stack will be used.

2.3 Operations

The following are the operations on a stack: • Getting the size • Checking if empty • Getting the top element without deleting it from the stack • Insertion of new element onto the stack push • Deletion of the top element from the stack pop Figure 1.6 PUSH Operation Figure 1.7 Figure 1.8 POP Operation Data Structures 23 J.E.D.I

2.4 Sequential Representation