0% found this document useful (0 votes)
3 views6 pages

Lab 05

Uploaded by

Bình Hoàng
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views6 pages

Lab 05

Uploaded by

Bình Hoàng
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Lab 05: Stacks, Queues

1. Push Stack
Please initialize a Stack with the data type of the elements stored inside the Stack as Integer. Then, add (push)
elements onto the top of the Stack using the push() method. Check the status of the stack before and after adding
the element.

Examples
Input Output
20, 13, 89, 90, 90, Element Stack: [20, 13, 89, 90, 11, 45, 18]
11, 45, 18

Output

Page 1 of 6
The process of pushing elements 20, 13, 89, 90, 11, 45, 18 to the above Stack is illustrated as below:

2. Delete Stack and Seach element


From lesson 1, get the top 3 elements of the Stack and delete (pop) that element via the pop() method. Then
perform an element search in the Stack with 2 instances of existence and non-existence

Examples

Output

Page 2 of 6
The process of deleting 3 elements on the top of the Stack is: 18, 45, 11 is illustrated as below

3. Loop Stack
Use a Foreach loop to traverse the Stack just created above and change the value of 1 element during the traversal
process.

Examples

Output

4. Queue
You create the Queue then:

Page 3 of 6
 Add data to Queue in 2 ways
 Delete data in 2 ways
 Get the first element from the Queue in 2 ways

Examples

Output

5. Task Processing with Queue using forEach


Write a Java program to simulate the process of task processing using a queue data structure. The program should
perform the following functions:
 Create the Task class with attributes name (task name) and description (task description).
 Implement the TaskProcessor class with a queue to manage Task objects.
 In TaskProcessor, create the enqueueTask method to add tasks to the queue.

Page 4 of 6
 In TaskProcessor, create the processTasks method to process tasks from the queue and display task
information.
 Write a test program that creates tasks, adds them to the queue, and processes the queue using
TaskProcessor.

Examples

Page 5 of 6
Output

Page 6 of 6

You might also like