In this code, I build a single-linked list using a node class that has been created. How could I change this code to take data of type T, rather than int. (PS: ignore the fact that IOHelper.getInt won't work for the type T... ie second half of main).
Here's my code right now:
public class SLList {
public SLNode head = null;
public SLNode tail = null;
public void add(int a) {// add() method present for testing purposes
SLNode newNode = new SLNode(a);
if (head == null) {
head = newNode;
tail = newNode;
} else {
tail.next = newNode;
tail = newNode;
}
}
public void SLswap(int i) throws illegalOperation {//i is index of node to change. method swaps specified node of index "i" with following node.
if(i>=(size()-1))
throw new illegalOperation("must swap one of the nodes, OR or you tried to swap last node... but cant do that since this method swaps node with next one");
if (size() < 2)
throw new illegalOperation("cant swap if theres less than two elements");
else if (size() == 2) {
SLNode placeholder = tail;
tail = head;
head = placeholder;
} // end else if
else if(i==0) {
SLNode anotherTemp = head.next;
SLNode temp = head.next.next;
head.next.next = head;
head.next = temp;
head = anotherTemp;
}//end else if
else {
SLNode iterationNode = head;
for (int j = 0; j<i; j++) {//order of pointing: iterationNode => iterationNode.next => nodeB(iterationNode.next.next) => tempA(iterationNode.next.next.next)... GOAL: swap iterationNode.next with nodeB(iteraitonNode.next.next)
if (j==(i-1)) {//ie: "if" condition met if the next iterationNode.next is of index i, therefore trying to swap iterationNode.next with iterationNode.next.next
SLNode tempA = iterationNode.next.next.next;
SLNode tempB = iterationNode.next.next;
iterationNode.next.next.next = iterationNode.next;
iterationNode.next.next = tempA;
iterationNode.next = tempB;
break;
}
iterationNode = iterationNode.next; //iterate to next node
} // end for-loop
}
}
public int size() {//returns number of nodes in SingleLinkedList
SLNode u = head;
int size = 0;
while (u != null) {
size++;
u = u.next;
}
return size;
}
public void display() { //prints values of all nodes in SLL, starting from head
SLNode current = head;
if(head == null) {
System.out.println("List is empty");
return;
}
System.out.println("Nodes of doubly linked list: ");
while(current != null) {
//Prints each node by incrementing the pointer.
System.out.print(current.data + " ");
current = current.next;
}
System.out.println('\n');
}
public static void main(String[] args) {
SLList myList = new SLList();
myList.add(3);
myList.add(6); //WANT TO BE ABLE TO ADD THINGS OF TYPE T!!
myList.add(1);
myList.add(4);
myList.add(2);
try {
myList.SLswap(2);
}catch(illegalOperation e) {};
myList.display();
SLList yourList = new SLList();
int numAdds = IOHelper.getInt("enter number of add calls to make: ");
int toAdd;
try {
for(int i=0; i<numAdds; i++) {
toAdd=IOHelper.getInt("enter integer to add: ");
yourList.add(toAdd);
}
int toSwap = IOHelper.getInt("enter index to swap: ");
yourList.SLswap(toSwap);} catch(illegalOperation e) {};
yourList.display();
System.out.println("DONE");
}//end main
}//end SLList
public class SLNode {
int data; //data that a node holds
SLNode next; // next node in the SSList that this given node will point to
public SLNode(int data) {
this.data = data;
this.next = null;
}
}
Hello,
Below is the code required for the above problem. I noticed a couple of discrepancies which I am pointing out as well. Hope it helps.
First to let the Single Linked List use any other data as well instead of int, we have to define the node class in such a way that it takes any data as well instead of just int. I have defined it below :
//Ndde class for the single linked list public class SLNode<T> { T data; //data that a node holds SLNode next; // next node in the SSList that this given node will point to public SLNode(T data) { this.data = data; this.next = null; } }
The class above will take any data now instead of just int.
One thing I noticed in the SLList class,which appeared to me as an error was the use of illegalOperation. I am guessing that it is a user defined exception class designed by you or it is a part of your code as it's not available to me. If it is not, I think you would like to change the name of the class and change it to IllegalOperationException instead of just illegalOperation. It enhances the readability and keep up with good Java coding practices as well. If it's not defined by you, and you're trying to throw an exception, I would suggest you use the basic Exception class like this
throw new Exception("your message here")
instead of illegalOperation. Throwing and catching this would be much better I guess.
Below is the code for the class SLList which will take any other data as well. I haven't made any changes to IOHelper taking in int, just defined how it can use any other data to create the list as required in the question above, so I have included the main method as it is :
public class SLList<T> { public SLNode head = null; public SLNode tail = null; public void add(T a) {// add() method present for testing purposes SLNode newNode = new SLNode(a); if (head == null) { head = newNode; tail = newNode; } else { tail.next = newNode; tail = newNode; } } public void SLswap(int i) throws IllegalOperation {//i is index of node to change. method swaps specified node of index "i" with following node. if(i>=(size()-1)) throw new illegalOperation("must swap one of the nodes, OR or you tried to swap last node... but cant do that since this method swaps node with next one"); if (size() < 2) throw new illegalOperation("cant swap if theres less than two elements"); else if (size() == 2) { SLNode placeholder = tail; tail = head; head = placeholder; } // end else if else if(i==0) { SLNode anotherTemp = head.next; SLNode temp = head.next.next; head.next.next = head; head.next = temp; head = anotherTemp; }//end else if else { SLNode iterationNode = head; for (int j = 0; j<i; j++) {//order of pointing: iterationNode => iterationNode.next => nodeB(iterationNode.next.next) => tempA(iterationNode.next.next.next)... GOAL: swap iterationNode.next with nodeB(iteraitonNode.next.next) if (j==(i-1)) {//ie: "if" condition met if the next iterationNode.next is of index i, therefore trying to swap iterationNode.next with iterationNode.next.next SLNode tempA = iterationNode.next.next.next; SLNode tempB = iterationNode.next.next; iterationNode.next.next.next = iterationNode.next; iterationNode.next.next = tempA; iterationNode.next = tempB; break; } iterationNode = iterationNode.next; //iterate to next node } // end for-loop } } public int size() {//returns number of nodes in SingleLinkedList SLNode u = head; int size = 0; while (u != null) { size++; u = u.next; } return size; } public void display() { //prints values of all nodes in SLL, starting from head SLNode current = head; if(head == null) { System.out.println("List is empty"); return; } System.out.println("Nodes of doubly linked list: "); while(current != null) { //Prints each node by incrementing the pointer. System.out.print(current.data + " "); current = current.next; } System.out.println('\n'); } public static void main(String[] args) { SLList myList = new SLList(); myList.add(3); myList.add(6); //WANT TO BE ABLE TO ADD THINGS OF TYPE T!! myList.add(1); myList.add(4); myList.add(2); try { myList.SLswap(2); }catch(illegalOperation e) {}; myList.display(); SLList yourList = new SLList(); int numAdds = IOHelper.getInt("enter number of add calls to make: "); int toAdd; try { for(int i=0; i<numAdds; i++) { toAdd=IOHelper.getInt("enter integer to add: "); yourList.add(toAdd); } int toSwap = IOHelper.getInt("enter index to swap: "); yourList.SLswap(toSwap); } catch(illegalOperation e) {}; yourList.display(); System.out.println("DONE"); }//end main }//end SLList
I hope that helped. :)
Get Answers For Free
Most questions answered within 1 hours.