These are 2 methods in java for a generic node class and I c
These are 2 methods in java for a generic node class and I cannot figure them out.
toString() creates and returns a string message which reveals the stored value (data) but not the link (this method is not recursive)
toString(int dum) creates and returns a string message which reveals the stored value (data) and the links (recursive). This method is applicable for shorter lists only.
here is the node class
public class Node
{
private T data;
private Node link;
Node(T initialData, Node initialLink){
this.data = initialData;
this.link = initialLink;
}
public Node addNodeAfter(T element){
link = new Node(element,link);
return link;
}
public void removeNodeAfter(Node head){
int x =0;
Node cursor;
cursor = head;
for (x = 1; x < listLength(head); x++) {
cursor = cursor.link;
}
cursor.data = null;
}
/*
*/
public static Node listCopy(Node source){
Node copyHead;
Node copyTail;
if(source == null){
return null;
}
copyHead = new Node(source.getData(), null);
copyTail = copyHead;
while(source.link != null){
source = source.link;
copyTail.addNodeAfter(source.data);
copyTail = copyTail.link;
}
return source;
}
public static Object listPosition(Node head, long position){
Node cursor;
int i = 1;
if(position <= 0){
throw new IllegalArgumentException(\"position is not positive.\");
}
cursor = head;
for (i = 1; (i < position)&&(cursor != null); i++) {
cursor = cursor.link;
}
return cursor.getData();
}
public static int listLength(Node head){
Node cursor;
int answer = 0;
for(cursor = head; cursor != null; cursor = cursor.link){
answer++;
}
return answer;
}
public static Node getTail(Node source){
int i;
Node cursor;
cursor = source;
for(i=1; i < listLength(source); i++){
cursor = cursor.link;
}
return cursor;
}
public void setLink(Node newLink){
link = newLink;
}
public void setData(T newData){
data = newData;
}
public T getData() {
return data;
}
public Node getLink() {
return link;
}
}
Solution
public class Node<T> {
private T data;
private Node link;
Node(T initialData, Node initialLink) {
this.data = initialData;
this.link = initialLink;
}
public Node addNodeAfter(T element) {
link = new Node(element, link);
return link;
}
public void removeNodeAfter(Node head) {
int x = 0;
Node cursor;
cursor = head;
for (x = 1; x < listLength(head); x++) {
cursor = cursor.link;
}
cursor.data = null;
}
/*
*/
public static Node listCopy(Node source) {
Node copyHead;
Node copyTail;
if (source == null) {
return null;
}
copyHead = new Node(source.getData(), null);
copyTail = copyHead;
while (source.link != null) {
source = source.link;
copyTail.addNodeAfter(source.data);
copyTail = copyTail.link;
}
return source;
}
public static Object listPosition(Node head, long position) {
Node cursor;
int i = 1;
if (position <= 0) {
throw new IllegalArgumentException(\"position is not positive.\");
}
cursor = head;
for (i = 1; (i < position) && (cursor != null); i++) {
cursor = cursor.link;
}
return cursor.getData();
}
public static int listLength(Node head) {
Node cursor;
int answer = 0;
for (cursor = head; cursor != null; cursor = cursor.link) {
answer++;
}
return answer;
}
public static Node getTail(Node source) {
int i;
Node cursor;
cursor = source;
for (i = 1; i < listLength(source); i++) {
cursor = cursor.link;
}
return cursor;
}
public void setLink(Node newLink) {
link = newLink;
}
public void setData(T newData) {
data = newData;
}
public T getData() {
return data;
}
public Node getLink() {
return link;
}
public String toString(){
return \" \" + this.data;
}
}
================================================
public String toString(int dum) {
String s1 = \"\";
String s2 = \"\";
if (data == null) {
s1 = \"data: dummy\";
} else {
s1 = \"data: \" + data.toString();
}
if (link == null) {
s2 = \"link: null in tail\";
} else {
s2 = \"link: \" + link.toString();
}
return s1 + \"\ \" + s2;
}




