Book - Data abstraction and problem-solving with Java - Walls and Mirrors 3rd edition
Language - Java
program code to copy
Test.java
public class Test
{
private static Queue<Integer> queue;
public static void main(String[] args)
{
queue = new Queue();
for (int i = 0; i < 15; i++)
{
queue.enqueue(i);
}
displayQueue();
System.out.println("\nCount: " +countQueuedItems());
System.out.println("Sum: " + sumQueuedItems());
System.out.println("Max: " + maxQueuedItem());
System.out.println("Min: " + minQueuedItem());
}
public static void displayQueue()
{
Integer last = queue.dequeue();
System.out.println(last);
queue.enqueue(last);
Integer item;
while ((item = queue.dequeue()) != last)
{
System.out.println(item);
queue.enqueue(item);
}
queue.enqueue(last);
}
public static int countQueuedItems()
{
Integer last = queue.dequeue();
queue.enqueue(last);
int count = 1;
Integer item;
while ((item = queue.dequeue()) != last)
{
queue.enqueue(item);
count++;
}
queue.enqueue(last);
return count;
}
public static int sumQueuedItems()
{
Integer last = queue.dequeue();
queue.enqueue(last);
int sum = last;
Integer item;
while ((item = queue.dequeue()) != last)
{
sum += item;
queue.enqueue(item);
}
queue.enqueue(last);
return sum;
}
public static int maxQueuedItem()
{
Integer last = queue.dequeue();
queue.enqueue(last);
int max = last;
Integer item;
while ((item = queue.dequeue()) != last)
{
if(item > max)
max = item;
queue.enqueue(item);
}
queue.enqueue(last);
return max;
}
public static int minQueuedItem()
{
Integer last = queue.dequeue();
queue.enqueue(last);
int min = last;
Integer item;
while ((item = queue.dequeue()) != last)
{
if(item < min)
min = item;
queue.enqueue(item);
}
queue.enqueue(last);
return min;
}
}
============================================================================
Queue.java
public class Queue<T> implements QueueInterface<T>
{
private Node lastNode;
private class Node
{
private T item;
private Node next;
Node(T item, Node next)
{
this.item = item;
this.next = next;
}
}
public Queue()
{
lastNode = null;
}
public boolean isEmpty()
{
return lastNode == null;
}
public void enqueue(T item)
{
Node newNode = new Node(item, null);
if (isEmpty())
{
newNode.next = newNode;
}
else
{
newNode.next = lastNode.next;
lastNode.next = newNode;
}
lastNode = newNode;
}
public T dequeue()
{
if (isEmpty()) throw new QueueException("Queue is empty");
Node firstNode = lastNode.next;
if(firstNode == lastNode) lastNode = null;
else lastNode.next = firstNode.next;
return firstNode.item;
}
public void dequeueAll()
{
lastNode = null;
}
public T peek()
{
if(isEmpty()) throw new QueueException("Queue is empty");
return lastNode.item;
}
}
====================================================================
QueueInterface.java
public interface QueueInterface<T>
{
boolean isEmpty();
void enqueue(T item);
T dequeue() throws QueueException;
void dequeueAll();
T peek();
}
=====================================================
QueueException.java
public class QueueException extends RuntimeException
{
public QueueException(String message)
{
super(message);
}
}
sample output
Book - Data abstraction and problem-solving with Java - Walls and Mirrors 3rd edition Language -...
In the textbook problem from Data Abstraction & Problem Solving with C++: Walls and Mirrors(7th edition), chapter 4, programming problem 6. If I want to build a copy constructor for the DoublyLinkedBag class. How can I do it. //this is the beginning of the copy constructor DoublyLinkBag(DoublyLinkBag& aBag){}
SIXTH EDITION Data Abstraction & Problem Solving with C+ WALLS AND MIRRORS FRANK M. CARRANO TIMOTHY HENRY
In Java language: 1. The complement of a queue is a stack. It uses first-in, last-out accessing and is often likened to a stack of plates. The first plate put on the table is the last plate used. Create a stack class called Stack that can hold characters. Call the methods that access the stack push( ) and pop( ). Allow the user to specify the size of the stack when it is created. Keep all other members of the...
Source material: Object-Oriented Data Structures Using Java, 3rd Edition by Nell Dale. What is the answer to exercise 13 in chapter 3 (page 232)?? "A. Create a "standard" exception class called ThirteenException. B. Write a program that repeatedly prompts the user to enter a string. After each string is entered the program outputs the length of the string, unless the length of the string is 13, in which case the ThirteenException is thrown with the message "Use thirteen letter words...
Java programming The purpose of this problem is to practice using a generic Urn class. NOTE: Refer to the code for the ArrayStack class from Chapter 12. Use that code as a starting point to create the Urn class, modifying it to remove the methods in the ArrayStack class (push, pop, etc) then add the methods described below. Also change the variable names to reflect the new class. For example the array name should NOT be stack, instead it should...
Please the answer have to be in JAVA programing language Problem 1: Write a class that implements a generic binary search tree including the methods add, contains and printInorder. (We will do this as a group) Problem 2: Add a toString method to the class. This method returns a string of the form “[e1, e2, …, en]” containing all of the elements in the tree in order. Problem 3: Add a method to the class that returns the number of...
Data Structures(2nd Edition Using JAVA Chapter Review, Problem 6PP does not provide an answer: "The Problem Statement is as follows": In a breadth-first traversal of a binary tree, the nodes are visited in an order prescribed by their level. First visit the node at level 1, the root node. Then visit the nodes at level 2, in left-to-right order, and so on. You can use a queue to implement a breadth-first traversal of a binary tree". Algorithm for Breath-First Traversal...
Data Structures(2nd Edition Using JAVA Chapter Review, Problem 6PP does not provide an answer: "The Problem Statement is as follows": In a breadth-first traversal of a binary tree, the nodes are visited in an order prescribed by their level. First visit the node at level 1, the root node. Then visit the nodes at level 2, in left-to-right order, and so on. You can use a queue to implement a breadth-first traversal of a binary tree". Algorithm for Breath-First Traversal...
Data Structures Java Language include also (Test class ) to test the methods Q2) Using a single queue (linkedQueue), re-implement the concept of Stack ADT, what is the complexity of the method push, pop, top, isEmpty, and size. You should not use any extra data structure.
please use java Question 1- Without using extra data structures, write a recursive method recursiveProdcutQueue ( Queue <Integer> q) in Test class (in stacks_queues package) that receives a queue of integers and return the product of the integers inside the queue. Then don't forget to test the method in the main. Note: Make sure not to change values in queue after calling the method. Example: If we have a Queue q of integer values as follows: 1 2 9 4...