Lab04-Stacks-java 2
Lab04-Stacks-java 2
Laboratory Manual
GEIT 2421 Data Structures
1
PREFACE
This document has been prepared to serve as a laboratory manual for GEIT 2421 Data
structures course for Computer Science and Information Technology students. This
manual gives exercises on using JAVA programming language to create and
manipulate different types of data structures like stacks, queues, linked list, etc. It is
clear and organized. The student is recommended to read the lab subject and the lab
exercise carefully before coming to the lab session, because understanding the lab
subject in advance will make writing the lab exercise a very easy process.
2
GEIT2421 DATA STRUCTURE LAB 04
Stacks
A stack is a classic collection used to help solve many types of problems. A stack is a
linear collection whose elements are added in a last in, first out (LIFO) manner. That is,
the last element to be put on a stack is the first one to be removed. Think of a stack of
books, where you add and remove from the top, but can't reach into the middle
Stack Operations
3
Program to Evaluate Postfix Expression:
Exercise: Write Test program that will evaluate the given postfix expressions. Also draw
the stack that will clearly show how these expressions are solved using stack.
A. 3 4 * 2 5 + – 4 * 2 /
B. 10 2 5 * + 3 -
4
Implementing Stack using Arrays
A stack with elements A, B, C, and D pushed on in that
Stack Interface
order:
Exercise: In the array implementation of stack program the following code is missing.
Your task is to implement these code and write a test program to test the
MyArrayStack<T> class
1. Implement pop() and peek() methods
2. Implement StackEmptyException class to throw stack empty exception whenever
user tries to pop an element from the empty stack.
3. Write Test application to run the MyArrayStack<T> class.
(Optional)
Implementing Stack using Linked List
5
Program to implement Stack using Linked List:
6
Exercise: In the array implementation of stack program the following code is missing.
Your task is to implement these code and write a test program to test the
MyArrayStack<T> class
1. Implement pop() and peek() methods
2. Write Test application to run the MyLinkedListStack<T> class.