i am new to Java bro-gramming(about 4 months). I have just picked up a copy of "Data Structures & Algorithms in Java" Second Edition by Robert Lafore and i am reading chapters and attempting the problems at the back. Since i am not taking an official class, i need some form of grading to teach me how my work can be improved.. what better place than the geniuses on the stack.
I am currently on chapter 5, Linked-Lists.
Here is problem 5.1
Implement a priority queue based on a sorted linked list. The remove operation
on the priority queue should remove the item with the smallest key.
Container class
public class LinkLL {
public static LinkLL leftConnector; //left side of node
public static String fName; //item in actual node
public static LinkLL rightConnector; //Right side of node
public LinkLL(String key)
{
fName = key;
//A new LinkLL linked list has been Instantiated,
//although it is empty.
rightConnector = null;
leftConnector = null;
}
public void showName()
{
System.out.print(fName + " " + "There are " + SortedLL.nElems + "in the list at this moment.");
}
}
here we have methods such as insert, delete..etc. I feel like i did a good job on the "insert" and maybe the delete? but my "sort" and "display" are giving me a run for my money. For instance, i want to display EVERY string inputted, but the way i have it, only the first one shows.
"Abstraction"
public class SortedLL {
static LinkLL currentNode;
static LinkLL firstNode;
static final LinkLL lastNode = null;
static LinkLL prequelNode;
static LinkLL sequelNode;
static float nElems;
static LinkLL l;
public static void insert(String key)
{
if ( nElems == 0) //could have also been if(nElems == 0)
{
l = new LinkLL(key); //if nothing exists, create one
l.rightConnector = sequelNode;//the right connector is equal to the sequelNode
sequelNode = lastNode; // Sequel Node is equal to lastNode which is 'null'.
prequelNode = sequelNode;
firstNode.leftConnector = lastNode;
}
else if( !(nElems == 0) )
{
LinkLL NewEndNode;
NewEndNode = lastNode;
l.rightConnector = NewEndNode;
l.leftConnector = prequelNode.rightConnector;
}
//when such occurs, nodes must be re-connected
nElems++;
}
public void remove()
{
if(nElems == 0) System.out.println("There are no items to remove.");
//if( !find(key) ) System.out.println(key +" could not be located, " + "and thus cannot be removed.");
//while ( find(key) ) //while the key can be found
{
//currentNode = key;
prequelNode.rightConnector = sequelNode.leftConnector;
nElems--;
}
//when such occurs, nodes should be reconnected
}
public boolean find(LinkLL key)
{
if (isEmpty() == true)
{
System.out.println("The List is Empty");
}
else
{
for(int i = 0; i<nElems+1; i++)
{
if (i == nElems+1)
//added '+1' to make sure that the item to be searched for is not at the bottom(list scanned throughly)
System.out.println("The key " + key + "has NOT been found!");
else
System.out.println("The key " + key + "has been found!");
}
}
return true;
}
public void sort()
{
}
public boolean isEmpty()
{
if (firstNode != null) return false;
else return false;
}
public void displayNode()
{
LinkLL first = null;
LinkLL current = first ;
while (current != null)
{
l.showName();
current = current.rightConnector;
}
}
}
Main()
public class sortedLLApp {
public static void main(String []args)
{
SortedLL s = new SortedLL();
s.isEmpty();
s.insert("Jack");
s.insert("Jill");
s.insert("John");
s.insert("Jenn");
s.insert("James");
s.displayNode();
}
}