Reputation: 3
I'm think inserting values correctly also I can view on console screen however search method does not work at weird way
What is the missing all my code ? it pretty clear and basic structure
at the output part "43" normally include on structure it need to show true but probably it does not working I dont know reason. output: 43 43 33 43 13 3 check: false
NodeList head;
public void insert(int insertKey) {
NodeList myhead = new NodeList();
if (head == null) {
myhead.setData(insertKey);
} else {
myhead.setData(insertKey);
myhead.setLink(head);
}
head = myhead;
}
public boolean search(int key) {
boolean check = false;
NodeList current = head;
while (current != null) {
if (current.getData() == key) {
check = true;
break;
}
current = current.getLink();
}
return check;
}
public void display() {
if (head == null) {
} else {
while (head != null) {
System.out.print(head.getData() + " ");
head = head.getLink();
}
System.out.println();
}
}
package ds_project;
public class NodeList {
private NodeList Link;
private int data;
public NodeList(int data) {
this.Link = null;
this.data = data;
}
public NodeList() {
}
public NodeList getLink() {
return Link;
}
public void setLink(NodeList Link) {
this.Link = Link;
}
public int getData() {
return this.data;
}
public void setData(int data) {
this.data = data;
}
result screen
SLL Linkedlist = new SLL();
Linkedlist.insert(3);
Linkedlist.insert(13);
Linkedlist.insert(43);
Linkedlist.insert(33);
Linkedlist.insert(43);
Linkedlist.insert(43);
Linkedlist.display();
boolean check=Linkedlist.search(43);
System.out.println("check: " + check);}
output: 43 43 33 43 13 3 check: false
Upvotes: 0
Views: 87
Reputation: 1877
The display method is wrong, head
will become the tail
node. Use a temp node:
public void display() {
NodeList tmp = head;
if (tmp != null) {
while (tmp != null) {
System.out.print(tmp.getData() + " ");
tmp = tmp.getLink();
}
System.out.println();
}
}
Edit: my test code:
public class NodeList {
private NodeList Link;
private int data;
public NodeList(int data) {
this.Link = null;
this.data = data;
}
public NodeList() {
}
public NodeList getLink() {
return Link;
}
public void setLink(NodeList Link) {
this.Link = Link;
}
public int getData() {
return this.data;
}
public void setData(int data) {
this.data = data;
}
NodeList head;
public void insert(int insertKey) {
NodeList myhead = new NodeList();
if (head == null) {
myhead.setData(insertKey);
} else {
myhead.setData(insertKey);
myhead.setLink(head);
}
head = myhead;
}
public boolean search(int key) {
boolean check = false;
NodeList current = head;
while (current != null) {
if (current.getData() == key) {
check = true;
break;
}
current = current.getLink();
}
return check;
}
public void display() {
NodeList tmp = head;
if (tmp != null) {
while (tmp != null) {
System.out.print(tmp.getData() + " ");
tmp = tmp.getLink();
}
System.out.println();
}
}
public static void main(String[] args) {
NodeList Linkedlist = new NodeList();
Linkedlist.insert(3);
Linkedlist.insert(13);
Linkedlist.insert(43);
Linkedlist.insert(33);
Linkedlist.insert(43);
Linkedlist.insert(43);
Linkedlist.display();
boolean check=Linkedlist.search(43);
System.out.println("check: " + check);
}
}
Upvotes: 1