I am in a computer science class for part of my degree, but I cannot grasp how to implement linked list in Java. I know how to in C and C++, however this one for some reason is hard for me to grasp. I am supposed to be making a class that stores an address, with two constructors (one for an apartment number and one without). I thought it would be good to use a linked list on this one rather than an array.
I haven't finished the Address class yet, but so far this is what I have:
import java.util.*;
public class Address {
private int houseNumber, aptNumber, zipCode;
private String Street, City, State;
private Address next;
private int count = 0;
public Scanner in;
// Not an apartment
public Address(){
in = new Scanner(System.in);
}
// functions to request data, because I hate having to make more variables.
public String Street(){
System.out.println("Please input street name: ");
return this.in.nextLine();
}
public int HouseNum(){
System.out.println("Please enter street number: ");
return this.in.nextInt();
}
public String City(){
System.out.println("Please input city name: ");
return this.in.nextLine();
}
public int AptNum(){
System.out.println("Please enter apartment number: ");
return this.in.nextInt();
}
public int ZipCode(){
System.out.println("Please enter zip code: ");
return this.in.nextInt();
}
public Address(int houseNum, String street, int zip, String city, String state) {
this.houseNumber = houseNum;
this.Street = street;
this.zipCode = zip;
this.City = city;
this.State = state;
this.aptNumber = 0;
in = new Scanner(System.in);
}
// An apartment
public Address(int houseNum, String street, int zip, String city, String state, int aptNum){
this.houseNumber = houseNum;
this.Street = street;
this.zipCode = zip;
this.City = city;
this.State = state;
aptNumber = aptNum;
}
}
As you can see, I have no idea what I am doing for linked list. I know that she didn't specify to use it, nor have we had any instruction on it yet but I think it's the most practical way. Long story short, I spent 8 hours last night reading about linked list (with many different types) rather than completing the assignment.
Any help explaining to me how to implement a linked list in java would be helpful!
Question
Gone
Hello all,
I am in a computer science class for part of my degree, but I cannot grasp how to implement linked list in Java. I know how to in C and C++, however this one for some reason is hard for me to grasp. I am supposed to be making a class that stores an address, with two constructors (one for an apartment number and one without). I thought it would be good to use a linked list on this one rather than an array.
I haven't finished the Address class yet, but so far this is what I have:
As you can see, I have no idea what I am doing for linked list. I know that she didn't specify to use it, nor have we had any instruction on it yet but I think it's the most practical way. Long story short, I spent 8 hours last night reading about linked list (with many different types) rather than completing the assignment.
Any help explaining to me how to implement a linked list in java would be helpful!
Link to comment
Share on other sites
18 answers to this question
Recommended Posts