I have completed my singly linked that adds nodes to a list from a text file. I now I am trying to let the user select a node from the linked list to either check in a movie or check out a movie from the text file. I have been trying to figure this out for a very long time with no success. Any contributions or help will be greatly appreciated!
here is my search function that is not complete...
public void search ()
{
System.out.print ("Which one would like like to check out: ");
Scanner check = new Scanner(System.in);
String usercheck = check.next();
SinglyLinkedList myList = new SinglyLinkedList();
Node n = myList.gethead();
for (int i = 0; i < myList.getSize; i++)
{
if (n.getTitle.equals(usercheck))
{
return ("found");
} else {
return n;
}
n = n.getNext();
}
}
here is my singly linked list I completed for reference to the search function...
public class SinglyLinkedList
{
//Implement a Node
private static class Node
{
private String[] actors;
private String title;
private String director;
private String year;
private int numActors;
private String status;
private String userID;
private String date;
private Node next;
public Node(String e, Node n)
{
String[] data = e.split(" ");
title = data[0];
director = data[1];
year = data[2];
numActors = Integer.parseInt(data[3]);
actors = new String[numActors];
int i = 0;
for (i = 0; i < numActors; i++)
{
actors[i] = data[4 + i];
}
status = data[4 + numActors];
if (status.equals("out"))
{
userID = data[5 + numActors];
date = data[6 + numActors];
}
next = n;
}
public int getNumActors()
{
return numActors;
}
public String gettitle()
{
return title;
}
public String[] getActors()
{
return actors;
}
public String getDirector()
{
return director;
}
public String getYear()
{
return year;
}
public String getStatus()
{
return status;
}
public String getUserID()
{
return userID;
}
public String getDate()
{
return date;
}
public Node getNext()
{
return next;
}
public void setNext(Node n)
{
next = n;
}
}
//List Implementation
private Node head = null;
private Node tail = null;
private int size = 0;
public SinglyLinkedList()
{};
public int getSize()
{
return size;
}
public Node getHead()
{
return head;
}
public boolean isEmpty()
{
return size == 0;
}
public String first()
{
if(isEmpty())
{
return null;
}
return head.gettitle();
}
public String last()
{
if(isEmpty())
{
return null;
}
return tail.gettitle();
}
public void addFirst(String e)
{
head = new Node(e, head);
if(size == 0)
{
tail = head;
}
size++;
System.out.println("Added head node with " + head.getDirector());
}
public void addLast(String e)
{
Node newNode = new Node(e, null);
if(isEmpty())
{
head = newNode;
}else{
tail.setNext(newNode);
}
tail = newNode;
size++;
System.out.println("Added tail node with " + tail.getDirector());
}
public String removeFirst()
{
if(isEmpty())
{
return null;
}
String answer = head.gettitle();
head = head.getNext();
size--;
if(size == 0)
{
tail = null;
}
System.out.println("Removed head node with " + answer);
return answer;
}
}
Your SinglyLinkedList
implementation is fine, but there are a few issues in your search()
method.
Everytime you call search()
you create a new SinglyLinkedList
. In other words, you aren't using the list that you previously generated from your text file. Pass in your file-generated list as a method parameter or make that list a global variable to access it.
Also, as another user commented, your search won't compile: you used return ("found")
while your method is void
. Since you want to return a value to let the user know that the value was found, you would then rewrite your method as
public String search()
The String
tells Java that this method will return a String
, while void
tells Java to not expect any value upon completion.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.