You are asked in this lab to use a linked list. You are asked to read from a fil
ID: 3767961 • Letter: Y
Question
You are asked in this lab to use a linked list. You are asked to read from a file called people.csv that contains a little under 100 lines of information about actors and directors who were involved in (for the most part) classic films. Each row contains a first name (some people have none), a surname, a year of birth and a year of death (some people are alive...). The information read from the file will be inserted into a list that will keep nodes in alphabetical order (lines are in random order in the file). You will check that everything is alphabetically ordered by displaying all the nodes in the list when the file is completely read. Additionally, you are asked to prompt for a surname, and display information about all people (there may be more than one) bearing that surname. At the end of the program, all the memory allocated to create the list must be freed.
Explanation / Answer
Complete Program:
// File: Person.java
public class Person
{
private String firstname;
private String surname;
private int birthyear;
private int deathyear;
public Person()
{
this.firstname = "";
this.surname = "";
this.birthyear = 0;
this.deathyear = 0;
}
public Person(String firstname, String surname, int birthyear, int deathyear)
{
this.firstname = firstname;
this.surname = surname;
this.birthyear = birthyear;
this.deathyear = deathyear;
}
public String getFirstname()
{
return firstname;
}
public void setFirstname(String firstname)
{
this.firstname = firstname;
}
public String getSurname()
{
return surname;
}
public void setSurname(String surname)
{
this.surname = surname;
}
public int getBirthyear()
{
return birthyear;
}
public void setBirthyear(int birthyear)
{
this.birthyear = birthyear;
}
public int getDeathyear()
{
return deathyear;
}
public void setDeathyear(int deathyear)
{
this.deathyear = deathyear;
}
public String toString()
{
if(deathyear > 0)
return String.format("%-12s%-12s%10d%10d", firstname, surname, birthyear, deathyear);
else
return String.format("%-12s%-12s%10d%10s", firstname, surname, birthyear, "");
}
}
// File: LinkedList.java
public class LinkedList
{
private class Node
{
private Person person;
private Node next;
public Node(Person aPerson)
{
person = aPerson;
next = null;
}
}
private Node head;
private int count;
public LinkedList()
{
head = null;
count = 0;
}
public void insert(Person aPerson)
{
Node newNode = new Node(aPerson);
if(isEmpty())
{
head = newNode;
}
else
{
Node current = head;
Node prev = null;
while(current != null && current.person.getSurname().compareToIgnoreCase(aPerson.getSurname()) < 0)
{
prev = current;
current = current.next;
}
if(prev == null)
{
newNode.next = head;
head = newNode;
}
else if(current == null)
{
prev.next = newNode;
}
else
{
prev.next = newNode;
newNode.next = current;
}
}
count++;
}
public LinkedList findAll(String surname)
{
LinkedList temp = new LinkedList();
Node current = head;
while(current != null)
{
if((current.person.getSurname().compareToIgnoreCase(surname)) == 0)
temp.insert(current.person);
current = current.next;
}
return temp;
}
public boolean isEmpty()
{
return (count == 0);
}
public int size()
{
return count;
}
public void printList()
{
Node current = head;
while(current != null)
{
System.out.println(current.person);
current = current.next;
}
System.out.println();
}
}
// File: LinkedListTest.java
import java.io.*;
import java.util.*;
public class LinkedListTest
{
public static void main(String[] args) throws FileNotFoundException
{
LinkedList list = new LinkedList();
Person aPerson = null;
Scanner infile = new Scanner(new File("people.csv"));
while(infile.hasNextLine())
{
String line = infile.nextLine();
String[] tokens = line.split(",");
if(tokens.length == 4)
aPerson = new Person(tokens[0], tokens[1], Integer.parseInt(tokens[2]), Integer.parseInt(tokens[3]));
else
aPerson = new Person(tokens[0], tokens[1], Integer.parseInt(tokens[2]), 0);
list.insert(aPerson);
}
infile.close();
System.out.println("List of all people:");
list.printList();
Scanner input = new Scanner(System.in);
System.out.print(" Enter a surname: ");
String surname = input.nextLine();
LinkedList temp = list.findAll(surname);
System.out.println("List of all people with surname '" + surname + "':");
temp.printList();
}
}
CSV file: people.csv
Sample Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.