I am working creating a fully encapsulated, homogeneous singly linked data structure. The Listing class and SinglyLinkedList class that are part of the whole application compile fine, but the problem that I am having is performing different operations such as insert, fetch, update, and delete inside of a do-while loop statement. For some reason when I compile my MainSinglyLinkedClass, the compiler keeps telling that the insert, fetch, update, and delete methods can't be applied when I attach to objects. What I am doing wrong? Can someone please help? Here is my MainSinglyLinkedClass application above.
public class Listing{
private String name; // key field
private int ID;
private double GPA;
public Listing(String name, int ID, double GPA){
this.name= name;
this.ID= ID;
this.GPA=GPA;
}
public String toString() {
return "Student Name: " + " " + name + " " + "Student ID: " + " " + ID + " " + "Student GPA: " + " " + GPA;
}
public Listing deepCopy(){
Listing clone= new Listing(name, ID, GPA);
return clone;
}
public int compareTo(String targetKey)
{
return(name.compareTo(targetKey));
}
}
public class SinglyLinkedList{
private Node h;
public SinglyLinkedList()
{
h = new Node(); // list header
h.l = null;
h.next = null; // dummy node
}
public boolean insert(Listing newListing)
{
Node n = new Node();
if(n == null)
return false;
else
{
n.next = h.next;
h.next = n;
n.l = newListing.deepCopy();
return true;
}
}
public Listing fetch(String targetKey)
{
Node p = h.next;
while(p != null && !(p.l.compareTo(targetKey) == 0))
{
p = p.next;
}
if(p != null)
return p.l.deepCopy();
else
return null;
}
public boolean delete(String targetKey)
{
Node q = h;
Node p= h.next;
while(p != null && !(p.l.compareTo(targetKey)==0))
{
q = p;
p = p.next;
}
if(p != null)
{
q.next = p.next;
return true;
}
else
return false;
}
public boolean update(String targetKey, Listing newListing)
{
if(delete(targetKey) == false)
return false;
else if(insert(newListing) == false)
return false;
return true;
}
public void showAll()
{
Node p = h.next;
while(p != null)
{
System.out.println(p.l.toString());
p = p.next;
}
}
public class Node
{
private Listing l;
private Node next;
public Node()
{
}
}
}
import java.io.*;
public class MainSinglyLinkedList{
public static void main(String[] args){
BufferedReader obj= new BufferedReader(new InputStreamReader(System.in));
int ch, temp;
SinglyLinkedList sll= new SinglyLinkedList();
do
{
// a user can choose between different operations by pressing a number
System.out.println("*****************(Menu Operations:)******************");
System.out.println("(Press 1). Insert a new student's information.");
System.out.println("(Press 2). Fetch and output a student's information");
System.out.println("(Press 3). Delete a student's information.");
System.out.println("(Press 4). Update a student's information.");
System.out.println("(Press 5). Output all the student's information.");
System.out.println("(Press 6). Exit the program.");
System.out.println("Enter your choice.");
ch = Integer.parseInt(obj.readLine());
switch(ch)
{
case 1:System.out.println("Enter the Student's Name, ID, and GPA: ");
sll.push(temp);
temp= Integer.parseInt(obj.readLine());
break;
case 2: temp = sll.fetch();
System.out.println("Here is the Student's Name, ID, and GPA you want to know about: ");
break;
case 3: temp = sll.delete();
System.out.println("The student's information that has been deleted is: ");
break;
case 4: temp = sll.update();
System.out.println("The student's information that has been updated is: ");
case 5: sll.showAll();
}
}while(ch!=6);
}
}