Help with linked list problem
This is my code and after printing the result i get is 0 for each item
Code :
import java.util.Scanner;
class Node {
public int data;
public Node next;
//Link constructor
public Node(int d1) {
this.data=data;
}
//Print Link data
public void printNode() {
System.out.print(data);
}
}
class LinkList {
private Node first;
//LinkList constructor
public LinkList() {
first=null;
}
//Returns true if list is empty
public boolean isEmpty() {
return first == null;
}
//Inserts a new Link at the first of the list
public void insert(int d1) {
Node newNode = new Node(d1);
newNode.next = first;
first = newNode;
}
//Deletes the link at the first of the list
public Node delete() {
Node temp = first;
first = first.next;
return temp;
}
//Prints list data
public void printList() {
Node current = first;
System.out.print("List: ");
while(current != null) {
current.printNode();
current = current.next;
}
System.out.println("");
}
}
class LinkListTest {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
LinkList list = new LinkList();
int data=0;
System.out.print("Please enter a succession of integers. Enter 0 to end ");
while(data!=999){
data=scan.nextInt();
list.insert(data);
}
list.printList();
while(!list.isEmpty()) {
Node deletedLink = list.delete();
System.out.print("deleted: ");
deletedLink.printNode();
System.out.println("");
}
list.printList();
}
}
Re: Help with linked list problem
Where does the code assign a value to data?
What does the constructor do with the arg that is passed to it in d1?
Re: Help with linked list problem
what does this.data=data mean in your constructor? you only have one data member, public int data, defined so your constructor isn't assigning anything... your boolean method in the LinkList class i think would be better to have a conditional statement for first == null and then have it explicitly return true
Re: Help with linked list problem