Ive got the Linked Queue and the Singly Linked node class, I just need help chan
ID: 3625645 • Letter: I
Question
Ive got the Linked Queue and the Singly Linked node class, I just need help changing the type to Customer.
EDIT: Realized that I put in the singly linked list class instead of the node class,which is probably why this hasn't been answered yet.
You will implement simple LINKED FIFO queue. Each element in the queue will contain a customer object.
Your queue should have the following data members:
Node front;
Node rear;
int currentSize: This is a count of the number of items in the queue. This should be checked each time a new value is added to the list, to keep track of the maximum size the queue ever becomes.
You will need an isEmpty(), isFull(), an enqueue, and a dequeue operation
Node class: For the Queue, you will need a singly linked node class, where the item type is Customer
______________________________________________________________________________________
public class Queuel{
Node front;
Node rear;
int currentSize;
public Queuel() {
// purpose: default constructor for class Queuel
//
// preconditions: None
// postconditions: None
rear = null;
front = null;
currentSize =0;
}// end default constructor
public boolean isEmpty(){
// purpose: determines if a queue is empty, if empty count = 0
//
// preconditions: None
// postconditions: None
if (currentSize==0) return true;
else return false;
}// end isEmpty
public int peek(){
// purpose: returns the value stored in the node at the front
// of the queue without removing the node from the queue
// preconditions: Queue must not be empty
// postconditions: Value in node referenced by front is return, front is unaltered
int result = 0;
if (!this.isEmpty()) result = front.getItem();
else throw new RuntimeException("Cant peek at an empty Queue");
return result;
}// end peek
public void enqueue(int newValue) {
// purpose: this inserts a new node at the rear of a queue.
//
// preconditions: none
// postconditions: a new node is created and linked to the rear of the queue
Node newNode = new Node(newValue);
if (currentSize== 0){
front = newNode;
rear = newNode;
currentSize++;
}
else {
rear.setNext(newNode);
rear=newNode;
currentSize++;
}
}// end enqueue
public int dequeue(){
// purpose: removes the value at the front of the queue
//
// preconditions: Queue must not be empty
// postcondition: node at the front of the queue is removed and the value it contains
// is returned
int result=0;
if (this.isEmpty()) throw new RuntimeException("Dequeue invalid for an Empty Queue");
else {
result = front.getItem(); // get the value from the front element
front = front.getNext(); // advance front to the second element in the queue
currentSize = currentSize -1;
// if no elements exist in the queue insure front and rear are reset to null
if (currentSize ==0) front= rear =null;
}// end else
return result;
}// end dequeue
}// end class
__________________________________________________________________________
public class Node {
private int item;
private Node next;
public Node() {
item=0;
next=null;
}
public Node(int newItem) {
item = newItem;
next=null;
}
public Node (int newItem , Node nextNode) {
item = newItem;
next = nextNode;
}
public void setItem(int newItem)
{
item = newItem;
}
public int getItem() {
return item;
}
public void setNext(Node newNode) {
next = newNode;
}
public Node getNext() {
return next;
}
}
_____________________________________________________________________________________
public class Customer{
int customer_ID;
int arrivalTime;
int departureTime;
int transactionTime;
String transactionType;
public Customer(int customer_ID, int arrivalTime, String transactionType, int transactionTime)
{
this.customer_ID = customer_ID;
this.arrivalTime = arrivalTime;
this.transactionType = transactionType;
this.transactionTime = transactionTime;
}
public int getCustomerID()
{
return customer_ID;
}
public int getArrivalTime()
{
return arrivalTime;
}
public int getDepartureTime()
{
return departureTime;
}
public int getTransactionTime()
{
return transactionTime;
}
public String getTransactionType()
{
return transactionType;
}
public int setDepartureTime()
{
departureTime = arrivalTime + transactionTime;
return departureTime;
}
public int calculateTurnAround()
{
return departureTime-arrivalTime;
}
}
Explanation / Answer
I think it's really straight forward. All you really need to do is instead of "int item" in node class, use "Customer customer" and set appropriate functions. Also, change the queue functions from "public int peek()" to "public Customer peek()", "public void enqueue(int newValue)" to "public void enqueue(Customer customer)" and "public int dequeue()" to "public Customer dequeue()". I have completed the node class below. You need to appropriately change to Queuel now in the function signatures I have mentioned above. public class Node { private Customer customer; private Node next; public Node() { customer=null; next=null; } public Node(Customer customer) { this.customer = customer; next=null; } public Node (Customer customer , Node nextNode) { this.customer = customer; next = nextNode; } public void setCustomer(Customer customer) { this.customer= customer; } public Customer getCustomer() { return customer; } public void setNext(Node newNode) { next = newNode; } public Node getNext() { return next; }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.