0% found this document useful (0 votes)
2 views

Lab04-Stacks-java 2

The document serves as a laboratory manual for the GEIT 2421 Data Structures course at Prince Mohammad Bin Fahd University, focusing on using JAVA to manipulate data structures like stacks and queues. It includes exercises on stack implementation, evaluating postfix expressions, and programming tasks for students to complete. Students are encouraged to understand the lab subjects before attending sessions to facilitate easier execution of lab exercises.

Uploaded by

fahoodies
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lab04-Stacks-java 2

The document serves as a laboratory manual for the GEIT 2421 Data Structures course at Prince Mohammad Bin Fahd University, focusing on using JAVA to manipulate data structures like stacks and queues. It includes exercises on stack implementation, evaluating postfix expressions, and programming tasks for students to complete. Students are encouraged to understand the lab subjects before attending sessions to facilitate easier execution of lab exercises.

Uploaded by

fahoodies
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

College of Computer Engineering and Sciences

Prince Mohammad Bin Fahd University

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

Lab #04: Stacks


Objectives:
1. About Stack
2. Solving Postfix Expression using Stack
3. Stack Implementation using Arrays.
4. (Optional) Stack Implementation using Linked List.

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 - Conceptual View

Stack Operations

Evaluating Postfix Expressions


We'll use a stack as follows to evaluate a postfix expression:
- Scan the expression left to right
- When an operand is encountered, push it on the stack
- When an operator is encountered, pop the top two elements on the stack, perform
the operation, then push the result on the stack
When the expression is exhausted, the value on the stack is the final result

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:

Program to implement Stack using Arrays:

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

The stack after A, B, C, and D are pushed, in that order:

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.

You might also like