Lab 2
Lab 2
Lab 2
Stack and Queue
Quang D. C.
[email protected]
Note
In this tutorial, we will approach two new data structures: Stack and Queue which
you can implement by using Linked list.
After completing this tutorial, you can:
• Implement a Stack ADT.
• Implement a Queue ADT.
• Apply the Stack and the Queue to solve the problems.
Part I
Classwork
In this part, lecturer will:
• During these part, students may ask any question that they don’t understand or
make mistakes. Lecturers can guide students, or do general guidance for the whole
class if the errors are common.
1. Introduction
1.1. Stack
First, we demonstrate Stack, which is one of the most popular ADT. The order in
which elements come off a Stack gives rise to its alternative name, LIFO (last in, first
out). In Stack, we have two important methods:
[email protected] 1
Ton Duc Thang University
Faculty of Information Technology Data Structures and Algorithms, Fall 2022-2023
1.2. Queue
Next, we consider Queue, which is one of the most popular ADT like Stack. The
order in which elements come off a Queue gives rise to its alternative name, FIFO (first
in, first out). In Queue, we have two important methods:
Figure 2 illustrates the two methods, enQueue and deQueue. We must notice that
queue maintains and tracks two positions, front and rear.
In the next section, we will consider the UML model of Stack and Queue.
[email protected] 2
Ton Duc Thang University
Faculty of Information Technology Data Structures and Algorithms, Fall 2022-2023
2. UML Model
2.1. Class Diagram of Stack
• MyStack class implements StackInterface and includes items that have Node type.
[email protected] 3
Ton Duc Thang University
Faculty of Information Technology Data Structures and Algorithms, Fall 2022-2023
• MyQueue class implements QueueInterface and includes items have Node type.
Part II
Excercise
Responsibility of the students in this part:
• Complete all the exercises with the knowledge from Part I.
Exercise 1
We can implement Stack and Queue with Array, Linked List (via composition or
inheritance), or from scratch with the ListNode (as the class diagrams in the previous
session). In this Lab, you can choose one way you prefer to define the Stack ADT and
Queue ADT. The Stack ADT and the Queue ADT need contain general data type <E>.
Then, implement Fraction class and test your Stack and Queue.
After defining your Stack and Queue, apply them to solve the below Exercises.
Exercise 2
Compute the result of the following expression by using recursive approach and
eliminate recursive by using Stack.
2𝑛 + 𝑛2 + 𝑃 (𝑛 − 1), n>1
𝑃 (𝑛) = {
3, n=1
Exercise 3
Write a program that reads in a sequence of characters and prints them in reverse
order, using Stack.
Exercise 4
Write a program that reads in a sequence of characters, and determines whether its
parentheses, braces, and curly braces are ”balanced”.
Hint: for left delimiters, push onto Stack; for right delimiters, pop from Stack and
check whether popped element matches right delimiter.
[email protected] 4
Ton Duc Thang University
Faculty of Information Technology Data Structures and Algorithms, Fall 2022-2023
Exercise 5
Show how to implement a Queue using two Stack.
Exercise 6
A palindrome is a word or a phrase that is spelled the same forward and backward.
For example, ”dad” is a palindrome; ”A man, a plan, a canal: Panama” is a palindrome
if you take out the spaces and ignore the punctuation.
Implement a program to determine whether an input is palindrome using one <Char-
acter> Stack and one <Character> Queue.