I have fully implemented a singly Linked List (code below), however, the assignment specifically requests that it be implemented using recursion. I've been attempting to translate the while loops that I wrote into recursive calls, but Have gotten stuck and could use some help. My most recent attempts at the recursion are included in the code, but have been commented out.Thanks in advance for your help.
public class AddressList
{
public Record firstLink ;
public String name, tel, email, addr, state, dob;
AddressList()
{
firstLink = null;
}
public boolean isEmpty()
{
return(firstLink == null);
}
public void addToFront(Record record)
{
if(firstLink == null)
{
firstLink = new Record(record.getName(), record.getTel(), record.getEmail(), record.getAddr(),
record.getState(), record.getDob());
}
else
{
Record newRecord = new Record(record.getName(), record.getTel(), record.getEmail(), record.getAddr(),
record.getState(), record.getDob());
newRecord.setNext(firstLink);
firstLink = newRecord;
}
}
public void addToBack(Record record)
{
if(firstLink == null)
{
firstLink = new Record(record.getName(), record.getTel(), record.getEmail(), record.getAddr(),
record.getState(), record.getDob());
}
else
{
Record newRecord = new Record(record.getName(), record.getTel(), record.getEmail(), record.getAddr(),
record.getState(), record.getDob());
Record currentRecord = firstLink;
while(currentRecord.getNext() != null)
{
currentRecord = currentRecord.getNext();
}
currentRecord.setNext(newRecord);
}
/*
if(firstLink == null)
{
firstLink = new Record(record.getName(), record.getTel(), record.getEmail(), record.getAddr(),
record.getState(), record.getDob());
}
else if (firstLink.getNext() == null)
{
firstLink.setNext(new Record(record.getName(), record.getTel(), record.getEmail(), record.getAddr(),
record.getState(), record.getDob()));
}
else
{
Record newRecord = new Record(record.getName(), record.getTel(), record.getEmail(), record.getAddr(),
record.getState(), record.getDob());
newRecord.setNext(firstLink);
firstLink = newRecord;
addToBack(newRecord);
}*/
/*
else
{
add
}
{
Record newRecord = new Record(record.getName(), record.getTel(), record.getEmail(), record.getAddr(),
record.getState(), record.getDob());
Record current = firstLink;
if(current.getNext() == null)
current.setNext(newRecord);
else
addToBack(current.getNext());
}
*/
}
public String toString()
{
if(firstLink == null)
return "Empty List\n\n";
String result = "";
Record current = firstLink;
result += "\nName: "+current.getName()+"\nTelephone: "+current.getTel()+"\nEmail: "+current.getEmail()+
"\nAddress: "+current.getAddr()+"\nState: "+current.getState()+"\nDOB: "+current.getDob()+"\n\n";
while(current.getNext() != null)
{
current = current.getNext();
result += "\nName: "+current.getName()+"\nTelephone: "+current.getTel()+"\nEmail: "+current.getEmail()+
"\nAddress: "+current.getAddr()+"\nState: "+current.getState()+"\nDOB: "+current.getDob()+"\n\n";
}
return "List: \n\n"+result;
}
public void reverse()
{
Record previousRecord = null;
Record currentRecord = firstLink;
while (currentRecord != null)
{
Record nextRecord = currentRecord.getNext();
currentRecord.setNext(previousRecord);
previousRecord = currentRecord;
currentRecord = nextRecord;
}
firstLink = previousRecord;
}
public int sizeOf()
{
Record currentRecord = firstLink;
int size = 1;
if (currentRecord == null)
{
return 0;
}
else
{
while (currentRecord.getNext() != null)
{
size++;
currentRecord = currentRecord.getNext();
}
}
return size;
}
public String phoneNumberByName(String name)
{
Record currentRecord = firstLink;
while(currentRecord.getName().equals(name) == false)
{
currentRecord = currentRecord.getNext();
}
return currentRecord.getTel();
/*
Record currentRecord = firstLink;
if (currentRecord.getName().equals(name))
{
return currentRecord.getTel();
}
else
{
firstLink = currentRecord.getNext();
phoneNumberByName(name);
}
return "Unexpected behaviour. You should never see this message.";
*/
}
public String emailByName(String name)
{
Record currentRecord = firstLink;
while(currentRecord.getName().equals(name) == false)
{
currentRecord = currentRecord.getNext();
}
return currentRecord.getEmail();
}
public String nameByPhoneNumber(String tel)
{
Record currentRecord = firstLink;
while(currentRecord.getTel().equals(tel) == false)
{
currentRecord = currentRecord.getNext();
}
return currentRecord.getName();
}
public String dobByName(String name)
{
Record currentRecord = firstLink;
while (currentRecord.getName().equals(name) == false)
{
currentRecord = currentRecord.getNext();
}
return currentRecord.getDob();
}
}
The Record class, in case you need that too:
public class Record
{
private String name;
private String tel; // Telephone number
private String email;
private String addr; // Address
private String dob; // Date of birth
private String state;
private Record next = null;
public Record(String name, String tel, String email, String addr, String state, String dob)
{
this.name = name;
this.tel = tel;
this.email = email;
this.addr = addr;
this.dob = dob;
this.state = state;
//this.next = null;
} // end of the constructor
public String getName()
{ return name; }
public String getTel()
{ return tel; }
public String getEmail()
{ return email; }
public String getAddr()
{ return addr; }
public String getState()
{ return state; }
public String getDob()
{return dob; }
public void setName(String name)
{ this.name = name; }
public void setTel(String tel)
{ this.tel = tel; }
public void setEmail(String email)
{ this.email = email; }
public void setAddr(String addr)
{ this.addr = addr; }
public void setState(String state)
{ this.state = state; }
public void setDob(String dob)
{ this.dob = dob; }
public Record getNext()
{ return next; }
public void setNext(Record record)
{ next = record; }
}