Question: Java Program: Using 3 classes: class Link { public int iData; // data item public double dData; // data item public Link next; // next
Java Program:

Using 3 classes:
class Link {
public int iData; // data item public double dData; // data item public Link next; // next link in list
// ------------------------------------------------------------- public Link(int id, double dd) // constructor
{
iData = id; // initialize data dData = dd; // ('next' is automatically
} // set to null)
// -------------------------------------------------------------
public void displayLink() // display ourself
{
System.out.print("{" + iData + ", " + dData + "} ");
}
} // end class Link
////////////////////////////////////////////////////////////////
class LinkList
{
private Link first; // ref to first link on list
// ------------------------------------------------------------- public LinkList() // constructor
{
first = null; // no links on list yet
}
// ------------------------------------------------------------- public boolean isEmpty() // true if list is empty
{
return (first==null);
}
// ------------------------------------------------------------- // insert at start of list public void insertFirst(intid, double dd) { // make new link Link newLink = new Link(id, dd);
newLink.next = first; // newLink --> old first first = newLink; // first --> newLink
}
// ------------------------------------------------------------- public Link deleteFirst() // delete first item { // (assumes list not empty) Link temp = first; // save reference to link first = first.next; // delete it: first-->old next return temp; // return deleted link
}
// ------------------------------------------------------------- public void displayList()
{
System.out.print("List (first-->last): ");
Link current = first; // start at beginning of list while(current != null) // until end of list,
{
current.displayLink(); // print data current = current.next; // move to next link
}
System.out.println("");
}
// ------------------------------------------------------------- } // end class LinkList
aboratory Exercise: 1. Using the Linked List showed above, write a program that will compute the total cost purchased by a customer. The program has a menu that will show the items and the prices. The program will ask the user the item to purchase and after the user chooses the item, the program will ask the quantity purchased. The user can add more item purchased as soon as the user quit from the menu. Note: The ItemNo, Qty and Price should be saved in the list. Since the program discussed above has only two data and a link to point to a node, you need to add one more data. It means the total data should be three (3) and a link to point to a node. List of Products Menu [1] Enter Order [2] Print Receipt [3] Exit Enter your choice: 1 Enter Item No.:101 Enter Quantity:2 Menu [1] Enter Order [2] Print Receipt [3] Exit Enter your choice: 1 Enter Item No.:102 Enter Quantity:1 Menu [1] Enter Order [2] Print Receipt [3] Exit Enter your choice: 2 Total Cost: 301.25
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
