Modify the class LinkedStack given in class in the following ways. Add the follo
ID: 3588339 • Letter: M
Question
Modify the class LinkedStack given in class in the following ways.
Add the following methods:
A constructor that loads the stack from an array parameter. The last array element should be at the top of the stack
A method named peekNextTop that allows you to retrieve the element just below the one at the top of the stack without removing it. It should return null if the stack has just one element, and it should throw an exception if the stack is empty.
Note: You CAN NOT add or remove data fields to LinkedStack class. You CAN NOT change the definition of Node class.
Modify the class ListQueue2 given in class in the following ways.
Add the following methods:
public int size() Return the size of the current queue (the calling ListQueue2 object).
public boolean empty() Return true if the queue is empty, false otherwise
Note: You CAN NOT add or remove data fields to ListQueue2 class. You CAN NOT change the definition of Node class.
Write a java application called HiringApp that you will use to hire and fire workers for a company. The application HiringApp must use the LinkedStack and LinkedQueue2 classes that are covered in the class. The rules of hiring and firing are described as follows:
1) If you are asked to fire somebody at a time when the firm has no employees, you should notify your supervisor (print a message). 2) If you are asked to fire somebody when the firm has 1 or more employees, you must fire the most recently hired. 3) You are to keep a list of applicants and the order in which they applied. 4) When you are asked to hire someone, if anybody has been fired, the most recently fired must be re-hired. 5) If there is nobody who has been fired, then the person who applied earliest is to be hired. 6) If there is nobody available for hiring, then you must notify your supervisor (print a message).
The program should have a simple menu that allows you to specify the three actions: • Accept application • Hire • Fire • Quit
"Accept application" should prompt you for the name of an applicant and add that person's information to an appropriate data structure. "Hire" should choose the appropriate person to hire, print his or her name, and appropriately update the internal data structures. "Fire" should choose the appropriate person to fire, print his or her name, and appropriately update the internal data structures. "Quit" should exit the program.
Hints: 1. The code that are related to this assignment and covered in the lecture section is in the attached Assign3RelatedCode.zip. 2. Before you start programming, you need to decide which data structure you will use to store the people involved in this HiringApp.
Related Code
public class LinkedStack<E> implements StackInt<E> {
// Insert inner class Node<E> here (see Listing 2.1)
/** A Node is the building block for a single-linked list. */
private static class Node<E> {
// Data Fields
/** The reference to the data. */
private E data;
/** The reference to the next node. */
private Node<E> next;
// Constructors
/**
* Creates a new node with a null next field.
* @param dataItem The data stored
*/
private Node(E dataItem) {
data = dataItem;
next = null;
}
/**
* Creates a new node that references another node.
* @param dataItem The data stored
* @param nodeRef The node referenced by new node
*/
private Node(E dataItem, Node<E> nodeRef) {
data = dataItem;
next = nodeRef;
}
}
//End of inner class Node<E>
// Data Fields
/** The reference to the first stack node. */
private Node<E> topOfStackRef = null;
/**
* Insert a new item on top of the stack.
* @post The new item is the top item on the stack.
* All other items are one position lower.
* @param obj The item to be inserted
* @return The item that was inserted
*/
@Override
public E push(E obj) {
topOfStackRef = new Node<E>(obj, topOfStackRef);
return obj;
}
/**
* Remove and return the top item on the stack.
* @pre The stack is not empty.
* @post The top item on the stack has been
* removed and the stack is one item smaller.
* @return The top item on the stack
* @throws EmptyStackException if the stack is empty
*/
@Override
public E pop() {
if (empty()) {
throw new EmptyStackException();
} else {
E result = topOfStackRef.data;
topOfStackRef = topOfStackRef.next;
return result;
}
}
/**
* Return the top item on the stack.
* @pre The stack is not empty.
* @post The stack remains unchanged.
* @return The top item on the stack
* @throws EmptyStackException if the stack is empty
*/
@Override
public E peek() {
if (empty()) {
throw new EmptyStackException();
} else {
return topOfStackRef.data;
}
}
/**
* See whether the stack is empty.
* @return true if the stack is empty
*/
@Override
public boolean empty() {
return topOfStackRef == null;
}
//End of class content for Students.
}
---------------------------------------------------------------------------
public class ListQueue2<E> {
// Data Fields
/** Reference to front of queue. */
private Node<E> front;
/** Reference to rear of queue. */
private Node<E> rear;
/** Size of queue. */
private int size;
// Insert inner class Node<E> here (see Listing 2.1)
/** A Node is the building block for a single-linked list. */
private static class Node<E> {
// Data Fields
/** The reference to the data. */
private E data;
/** The reference to the next node. */
private Node<E> next;
// Constructors
/**
* Creates a new node with a null next field.
* @param dataItem The data stored
*/
private Node(E dataItem) {
data = dataItem;
next = null;
}
/**
* Creates a new node that references another node.
* @param dataItem The data stored
* @param nodeRef The node referenced by new node
*/
private Node(E dataItem, Node<E> nodeRef) {
data = dataItem;
next = nodeRef;
}
} //end class Node
// Methods
/**
* Insert an item at the rear of the queue.
* @post item is added to the rear of the queue.
* @param item The element to add
* @return true (always successful)
*/
public boolean offer(E item) {
// Check for empty queue.
if (front == null) {
rear = new Node<E>(item);
front = rear;
} else {
// Allocate a new node at end, store item in it, and
// link it to old end of queue.
rear.next = new Node<E>(item);
rear = rear.next;
}
size++;
return true;
}
/**
* Remove the entry at the front of the queue and return it
* if the queue is not empty.
* @post front references item that was second in the queue.
* @return The item removed if successful, or null if not
*/
public E poll() {
E item = peek(); // Retrieve item at front.
if (item == null) {
return null;
}
// Remove item at front.
front = front.next;
size--;
return item; // Return data at front of queue.
}
/**
* Return the item at the front of the queue without removing it.
* @return The item at the front of the queue if successful;
* return null if the queue is empty
*/
public E peek() {
if (size == 0) {
return null;
} else {
return front.data;
}
}
}
---------------------------------------------------------------------------
public interface StackInt<E> {
/**
* Pushes an item onto the top of the stack and returns
* the item pushed.
* @param obj The object to be inserted
* @return The object inserted
*/
E push(E obj);
/**
* Returns the object at the top of the stack
* without removing it.
* @post The stack remains unchanged.
* @return The object at the top of the stack
* @throws EmptyStackException if stack is empty
*/
E peek();
/**
* Returns the object at the top of the stack
* and removes it.
* @post The stack is one item smaller.
* @return The object at the top of the stack
* @throws EmptyStackException if stack is empty
*/
E pop();
/**
* Returns true if the stack is empty; otherwise,
* returns false.
* @return true if the stack is empty
*/
boolean empty();
}
---------------------------------------------------------------------------
public class TestLinkedStack {
public static void main(String args[]) {
LinkedStack<String> movieStack = new LinkedStack<String>();
System.out.println(movieStack.empty());
movieStack.push("The choice");
movieStack.push("How to be single");
System.out.println(movieStack.empty());
System.out.println(movieStack.peek());
System.out.println(movieStack.pop());
System.out.println(movieStack.pop());
}
}
---------------------------------------------------------------------------
public class TestListQueue2 {
public static void main(String[] args){
ListQueue2<String> customerQueue = new ListQueue2<String>(); //create empty queue
customerQueue.offer("Customer1"); //add items to queue
customerQueue.offer("Customer2");
customerQueue.offer("Customer3");
System.out.println("queue size: " + customerQueue.size()); //check queue size
System.out.println("peek return: " + customerQueue.peek()); //peek first item in queue
System.out.println("queue size: " + customerQueue.size());
String who = customerQueue.poll(); //remove first item in queue
System.out.println("poll return: " + who);
System.out.println("queue size: " + customerQueue.size());
}
}
Explanation / Answer
public class LinkedStack<E> implements StackInt<E> {
// Insert inner class Node<E> here (see Listing 2.1)
/** A Node is the building block for a single-linked list. */
private static class Node<E> {
// Data Fields
/** The reference to the data. */
private E data;
/** The reference to the next node. */
private Node<E> next;
// Constructors
/**
* Creates a new node with a null next field.
* @param dataItem The data stored
*/
private Node(E dataItem) {
data = dataItem;
next = null;
}
/**
* Creates a new node that references another node.
* @param dataItem The data stored
* @param nodeRef The node referenced by new node
*/
private Node(E dataItem, Node<E> nodeRef) {
data = dataItem;
next = nodeRef;
}
}
//End of inner class Node<E>
// Data Fields
/** The reference to the first stack node. */
private Node<E> topOfStackRef = null;
/**
* Insert a new item on top of the stack.
* @post The new item is the top item on the stack.
* All other items are one position lower.
* @param obj The item to be inserted
* @return The item that was inserted
*/
@Override
public E push(E obj) {
topOfStackRef = new Node<E>(obj, topOfStackRef);
return obj;
}
/**
* Remove and return the top item on the stack.
* @pre The stack is not empty.
* @post The top item on the stack has been
* removed and the stack is one item smaller.
* @return The top item on the stack
* @throws EmptyStackException if the stack is empty
*/
@Override
public E pop() {
if (empty()) {
throw new EmptyStackException();
} else {
E result = topOfStackRef.data;
topOfStackRef = topOfStackRef.next;
return result;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.