Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is a Java Assignment. Your job in this assignment is to create a generic li

ID: 3679490 • Letter: T

Question

This is a Java Assignment.

Your job in this assignment is to create a generic linked list. The linked list should have the ability to perform the following actions:

check if the list is empty

check the size of the list

add data to the list

add data to a specific (valid) location in the list

remove an item from the list based on position

remove an item from the list based on data value

removes all elements from the list

gets an item from the list based on the position

outputs the contents of the list

You can simply use integers to display them in the list.

Explanation / Answer

FOR STRING BASED

BASIC GENARIC LINKED LIST

public class LinkedList3<T>
2 {
3 private class Node<T>
4 {
5 private T data;
6 private Node<T> link;
7 public Node( )
8 {
9 data = null;
10 link = null;
11 }
12 public Node (T newData, Node<T> linkValue)
13 {
14 data = newData;
15 link = linkValue;
16 }
17 }//End of Node<T> inner class
18 private Node<T> head;
19 public LinkedList3( )
20 {
21 head = null;
22 }
23 /**
24 Adds a node at the start of the list with the specified data.
25 The added node will be the first node in the list.
26 */
27 public void addToStart(T itemData)
28 {
29 head = new Node<T> (itemData, head);
30 }
31 /**
32 Removes the head node and returns true if the list contains at
33 least one node. Returns false if the list is empty.
34 */
35 public boolean deleteHeadNode( )
36 {
37 if (head != null)
38 {
39 head = head.link;
40 return true;
41 }
42 else
43 return false;
44 }
45 /**

Returns the number of nodes in the list.
47 */
48 public int size( )
49 {
50 int count = 0;
51 Node<T> position = head;
52 while (position != null)
53 {
54 count++;
55 position = position.link;
56 }
57 return count;
58 }
59 public boolean contains(T item)
60 {
61 return (find(item) != null);
62 }
63 /**
64 Finds the first node containing the target item, and returns a
65 reference to that node. If target is not in the list, null is
returned.
66 */
67 private Node find(T target)
68 {
69 Node<T> position = head;
70 T itemAtPosition;
71 while (position != null)
72 {
73 itemAtPosition = position.data;
74 if (itemAtPosition.equals(target))
75 return position;
76 position = position.link;
77 }
78 return null; //target was not found
79 }
80 /**
81 Finds the first node containing the target and returns a reference
82 to the data in that node. If target is not in the list, null is
returned.
83 */
84 public T findData(T target)
85 {
86 Node<T> result = find(target);
87 if (result == null)
88 return null;
89 else
90 return result.data;

}
92 public void outputList( )
93 {
94 Node<T> position = head;
95 while (position != null)
96 {
97 System.out.println(position.data);
98 position = position.link;
99 }
100 }
101 public boolean isEmpty( )
102 {
103 return (head == null);
104 }
105 public void clear( )
106 {
107 head = null;
108 }
109 /*
110 For two lists to be equal they must contain the same data items in
111 the same order. The equals method of T is used to compare data
items.
112 */
113 public boolean equals(Object otherObject)
114 {
115 if (otherObject == null)
116 return false;
117 else if (getClass( ) != otherObject.getClass( ))
118 return false;
119 else
120 {
121 LinkedList3<T> otherList = (LinkedList3<T>)otherObject;
122 if (size( )!= otherList.size( ))
123 return false;
124 Node<T> position = head;
125 Node<T> otherPosition = otherList.head;
126 while (position != null)
127 {
128 if (!(position.data.equals(otherPosition.data)))
129 return false;
130 position = position.link;
131 otherPosition = otherPosition.link;
132 }
133 return true; //no mismatch was not found
134 }
135 }
136 }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote