Question: Data Structures and Algorithms in Java - LAB 2 (Stacks and Queues) B- Practical exercises Question 1. Write a Java program to implement a stack

Data Structures and Algorithms in Java - LAB 2 (Stacks and Queues)

Data Structures and Algorithms in Java - LAB 2 (Stacks and Queues)B- Practical exercises Question 1. Write a Java program to implement a

B- Practical exercises Question 1. Write a Java program to implement a stack of integer values with the following operations: 1) boolean isEmpty() - return true if the stack is empty and false otherwise. 2) void clear() - clear the stack. 3) void push(int x) - insert a node with value x at the top of the stack. 4) int pop() - remove the top element on the stack and return it's value; throw EmptyStackException for empty stack. 5) int top() - return value of a node at the top of the stack; throw EmptyStackException for empty stack. 6) void traverse() - display all values in the stack from the top to the bottom. 7) Use a stack to convert an integer number in decimal system to binary system and display on the screen. Note: You can write some constructors and other methods as you see they are necessary. Question 2. Write a Java program to implement a queue of integer values with the following operations: 1) boolean isEmpty() - return true if the queue is empty and false otherwise. 2) void clear() - clear the queue. 3) void enqueue(int x) - insert a node with value x at the end of the queue. 4) int dequeue() - remove the first element on the queue and return it's value; throw Exception for empty queue. 5) int first() - return value of the first node of the queue; throw Exception for empty queue. 6) void traverse() - display all values in the queue from the front to the rear. 7) Use a queue to convert a real number less than 1 in decimal system to binary system and display on the screen. Note: You can write some constructors and other methods as you see they are necessary. Question 3. Modify programs in questions 1 and 2 so that elements in the stack and queue are String types and write the Main class to test them. Question 4. Modify programs in questions 1 and 2 so that elements in the stack and queue are character types and write the Main class to test them. Question 5. Modify programs in questions 1 and 2 so that elements in the stack and queue are Object types and write the Main class to test them (e.g. stack and queue contains computer names)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!