USER-DEFINED Linked List YANKEES Modify yankees111c.java with the user-defined linked list of ynode objects to yankees11

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

USER-DEFINED Linked List YANKEES Modify yankees111c.java with the user-defined linked list of ynode objects to yankees11

Post by answerhappygod »

USER-DEFINED Linked List YANKEES
Modify yankees111c.java with the user-defined linked list of
ynode objects to yankees111d.java. We will add on some more
methods similar to the LinkedList class. Add the following
new methods, based off the LinkedList class (slightly modified to
work with our classes), to yankees111d.java. All new methods
must function similar to the methods we did in class and use the
yankees111d.java data fields of hptr (first), cptr (iterator), and
tptr (last) ynode references and the integer size. The new
yankees111d.java file must work with the given main program of
ex111d.java.
Make a new ynode and put the ynode at the beginning or ending of
the yankees111d linked list.
public void addFirst(String nam, int num);
public void addLast(String nam, int num);
Look up the ynode at the index number, beginning, or ending of
the linked list and return a concatenated name and number
fields.
public String get(int index);
public String getFirst();
public String getLast();
Change the ynode at index number index to the new nam and
num.
public void set(int index, String nam, int num);
Find the index number of the ynode with nam and num, if it
doesn’t exist, return -1.
public int indexOf(String nam, int num);
Check if the yankees111d linked list is empty or not.
public boolean isEmpty();
On methods that work with an index number, check to make sure
the index number is inside the linked list. On methods that
return a String for the ynode, concatenate the data fields (see
output below).
Add and code the above new methods to the yankees111d linked
list class definition. The yankees111d linked list will still
have the inner class of ynode as the type of objects in the linked
list. Your new yankees111d.java must work with the given main
and produce the given output (see output below).
Document the class definition code of yankees111d.java with a 4
- 5 line paragraph explaining the program at the top and at least 5
lines of explanations for the new parts throughout the
program. Run the program and save the console output into an
output .txt file. Submit your class definition file and your output
.txt file. The main class will be the same as the file given.
Add some players
Index Out Of Bounds Exception 5 0
Index Out Of Bounds Exception 5 3
Index Out Of Bounds Exception 26 6
Spot 2 is Name: Rizzo
Number: 48
Spot 10 is Name: Chapman
Number: 54
Index of Cole-44 is -1
Index of Cole-45 is 4
First is Name: Torres
Number: 25
Last is Name: Gallo
Number: 13
Final List
List starts at yankees111d$ynode@513df0bd
yankees111d$ynode@513df0bd
0
Torres 25
yankees111d$ynode@36557e4d
yankees111d$ynode@36557e4d
1 Britton
53 yankees111d$ynode@5e20a2d0
yankees111d$ynode@5e20a2d0
2
Rizzo 48
yankees111d$ynode@72117367
yankees111d$ynode@72117367
3
Judge 99
yankees111d$ynode@1b0b33fc
yankees111d$ynode@1b0b33fc
4
Cole 45
yankees111d$ynode@fdd926
yankees111d$ynode@fdd926
5 Stanton
27 yankees111d$ynode@7c2a43aa
yankees111d$ynode@7c2a43aa
6 LeMahieu
26 yankees111d$ynode@62649104
yankees111d$ynode@62649104
7 Donaldson
28 yankees111d$ynode@5de19080
yankees111d$ynode@5de19080
8
Hicks 31
yankees111d$ynode@474733d8
yankees111d$ynode@474733d8
9
Green 57
yankees111d$ynode@131d7063
yankees111d$ynode@131d7063
10 Chapman
54 yankees111d$ynode@4160bf1d
yankees111d$ynode@4160bf1d
11 Taillon
50 yankees111d$ynode@3bca4a36
yankees111d$ynode@3bca4a36
12 Severino
40 yankees111d$ynode@474ae32c
yankees111d$ynode@474ae32c 13
Kiner-Falefa 12
yankees111d$ynode@4af99a94
yankees111d$ynode@4af99a94
14 Montgomery
47 yankees111d$ynode@6b36740
yankees111d$ynode@6b36740
15
Gallo
13
null
/* Will Wohlber
* Exercise 11-1
* This class will define a yankees linked list
* */
public class yankees111c // outer class for linked list
{
private class ynode // inner class for individual nodes
{
private String name;
private int number;
private ynode next; // reference to next ynode
public ynode() // reference nodes
{
name = "";
number = 0;
next = null;
}
public ynode(String na, int nu) // nodes in linked list
{
name = na;
number = nu;
next = null;
}
} // end inner class
private ynode hptr; // reference to first node
private ynode cptr; // reference to each node - current -
iterator
private ynode tptr; // reference to last node
private int size;
public yankees111c()
{
hptr = new ynode();
cptr = new ynode();
tptr = new ynode();
size = 0;
}
public void add(String nam, int num)
{
ynode nptr = new ynode(nam, num);
if (size == 0)
{
hptr = nptr; // hptr holds address of first node
tptr = nptr; // tptr - only node is last node
size = 1; // first(nam, num);
}
else
{
tptr.next = nptr; // last nodes' next field references new
node
tptr = nptr; // reposistion tptr on new last node
size++;
}
}
public void add(int index, String nam, int num)
{
if (index < 0 || index > size)
System.out.println("Index Out Of Bounds Exception " +
index);
else
{
ynode nptr = new ynode(nam, num); // nptr holds address of new
node
if (size == 0)
{
hptr = nptr; // hptr holds address of first node
tptr = nptr; // tptr - only node is last node
size = 1; // first(nam, num);
}
else if (index == 0)
{
nptr.next = hptr; // new node's next field holds address of old
first
hptr = nptr; // new node is first
size++;
}
else
{
cptr = hptr;
int i;
for (i = 0; i < index - 1; i++)
cptr = cptr.next; // moves cptr to node before where new node
goes
ynode bptr = new ynode();
ynode aptr = new ynode();
bptr = cptr;
aptr = cptr.next;
bptr.next = nptr; // previous nodes' next field references new
node
nptr.next = aptr;
size++;
}
}
}
public void display()
{
int index = 0;
cptr = hptr; // start at beginning - li =
list.listIterator();
System.out.println("List starts at " + hptr);
while (cptr != null) // traverse until no more nodes - while
(li.hasNext())
{
//System.out.println(cptr + "\t" + index++ + "\t" + cptr.name +
"\t" + cptr.number + "\t" + cptr.next);
System.out.printf("%-26s%6d%13s%7d%31s\n", cptr, index++,
cptr.name, cptr.number, cptr.next);
cptr = cptr.next; // move to next node - li.next()
}
}
}
/* Bill Wohlleber
* Exercise 11-1d
* This program will create a yankees linked list
* */
import java.util.Scanner;
public class ex111d2rr
{
public static void main(String[] args)
{
int num;
String player;
Scanner sc = new Scanner(System.in);
yankees111d y = new yankees111d(); // linked list
if (y.isEmpty())
System.out.println("Add some players");
else
y.display();
System.out.println();
y.add(5, "Judge", 99);
y.add("Judge", 99);
y.add("Stanton", 27);
y.add("Donaldson", 28);
y.set(5, "Judge", 99);
y.add(0, "Rizzo", 48);
y.add("Hicks", 31);
y.add("Hicks", 31);
y.add(26, "LeMahieu", 26);
y.add(3, "LeMahieu", 26);
y.add("Chapman", 54);
y.add(2, "Cole", 45);
y.add("Taillon", 50);
y.addFirst("Britton", 53);
y.addLast("Severino", 40);
y.add("Kiner-Falefa", 12);
y.addFirst("Torres", 25);
y.add("Montgomery", 47);
y.add("Gallo", 13);
y.set(9, "Green", 57);
System.out.println();
System.out.println("Spot 2 is " + y.get(2));
System.out.println();
System.out.println("Spot 10 is " + y.get(10));
System.out.println();
System.out.println("Index of Cole-44 is " + y.indexOf("Cole",
44));
System.out.println("Index of Cole-45 is " + y.indexOf("Cole",
45));
System.out.println();
System.out.println("First is " + y.getFirst());
System.out.println("Last is " + y.getLast());
System.out.println();
System.out.println("Final List");
if (y.isEmpty())
System.out.println("Add some players");
else
y.display(); // iterator
System.out.println();
}
}
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply