Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

solve it with java Please apply the required instructions to literally solve the question Q1: Implement the class ArrayStack discussed in the lecture. a. Add

solve it with java image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
Please apply the required instructions to literally solve the question
Q1: Implement the class ArrayStack discussed in the lecture. a. Add a method copy to the class. This method should take a stack and copy its contents to a new stack and return the new stack. The original stack should not change. b. Add to the class a method printReverse that prints the elements of the stack in reverse (last element first). C. Add a test program that creates an empty stack and initialize it as in the sample output, maximum size is 10. Then it should call the copy method and print the original and the copied stack. It should remove the top element of the copied stack and then print the contents of both stacks. d. What is the time complexity of copy and printReverse methods? Sample output Please enter an integer or -1 to end 15 6 7 8 9 10-1 The contents of the original stack: 10 9 8 7 651 The contents of the copied stack: 10 9 8 7 651 Removing the top from copied stack: 10 The contents of the original stack: 10 9 8 7 651 The contents of the copied stack: 9 8 7651 Q2: Tower of Hanoi is a mathematical puzzle where we have three rods and n disks of different sizes. The objective of the puzzle is to move the entire stack to another rod, obeying the following simple rules: 1) Only one disk can be moved at a time. 2) Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack i.e. a disk can only be moved if it is the uppermost disk on a stack. 3) No disk may be placed on top of a smaller disk. Kuwait University Fall 2020 College of Engineering and Petroleum CpE-207: Data Structures & Department of Computer Engineering Algorithms Design and implement a program that solves the Tower of Hanoi problem. You have 3 towers a, b, and c, and n disks. You need to move the disks from tower a to tower c following the rules above. Hint: you can use tower b to hold disks temporarily. You can represent the towers as stacks. (use linked list implementation) a. Describe how can you solve the problem for n = 1, n = 2, n = 3, then conclude the general case for any value of n. Sample steps for n = 3 is shown in the image. b. Write a Java method towersOfHanoi that takes 4 arguments: the number of disks n, the source tower a, the temporarily tower b, and the destination tower c. The method should move the top n disks from tower a to tower c using tower bas temporarily. c. Implement the LinkedStack and Node as discussed in the lecture. Just change the type of element to char. d. Add a Java method printReverse to the class that prints the contents of the stack in reverse (last element first). e. Write a Java program to test your method. It should take the number of disks n from the user, and then create three stacks to represent each tower, a, b, and c. Then, it should ask the user to enter n characters in alphabetical order (to represent the disks and sizes) and pushed them into the first stack. Then it should print the contents of Kuwait University Fall 2020 College of Engineering and Petroleum CpE-207: Data Structures & Department of Computer Engineering Algorithms all stacks. Then, it should call the towersOfHanoi method. Then, print the contents of the three stacks. Check the sample output Sample output Please enter the number of disks 3 Please enter 3 characters in alphabetical order: ABC The contents of tower a: CBA The contents of tower b: The contents of tower c: After moving the disks from tower a to tower c: The contents of tower a: The contents of tower b: The contents of tower c: CBA boolean isEmpty(); //return true if queue is empty, false otherwise int size(): //return the number of elements in the queue void enqueue(int e): //add a new node with element value e to the end of queue void enqueue(Node n); //add noden to the end of the queue Node dequeuel)://remove the node at the front of the queue and return it Node first(); //return the node at the front of the queue without removing it Node last(); //return the node at the rear of the queue without removing it Implement a linked list queue of the interface shown as explained in the lecture notes, you can copy it and make any necessary changes, if any. You need to a. Define a Node class that has an integer element and a reference to the next node. Provide a constructor that initialize the value of the element and set and get methods for the element and the next variables. Kuwait University Fall 2020 College of Engineering and Petroleum CpE-207: Data Structures & Department of Computer Engineering Algorithms b. Define a LinkedQueue class that stores a reference to the front node, a reference to the rear node, and size. Implement the methods shown in the interface. C. Add a method print: void print(). This method should print the value of the nodes of non-empty queue to the screen starting from front to the rear of the queue. d. Add a method findAt: int findAt(int p). This method should return the element of the node at position p of non-empty queue. Note, p also must be a valid position in the queue

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design Query Formulation And Administration Using Oracle And PostgreSQL

Authors: Michael Mannino

8th Edition

1948426951, 978-1948426954

Students also viewed these Databases questions