java


Linked List remove


I have this piece of code. It is syntactically correct, but when I am trying to remove a node from front it says "The list is Empty".
I believe that my mistake is in the isEmpty method, because it returns the wrong value, but I don't know why.
Could you please help me to detect why?
Here is my code:
public class link {
public String bookName ;
public int millionSold;
public link next ;
public static void main(String [] args)
{
linkList linkedlist = new linkList();
linkedlist.insertLink("Head first java", 200);
linkedlist.insertLink("Head first design pattern", 400);
linkedlist.insertLink("Head first design", 600);
linkedlist.display();
System.out.println(linkedlist.isEmpty());
linkedlist.findItem("Abed");
}
public void display()
{
System.out.println(bookName +":"+millionSold+"000.000");
}
public link(String bookName,int millionSold)
{
this.bookName=bookName;
this.millionSold=millionSold;
}
public String toString()
{
return bookName ;
}
}
Linklist
class linkList
{
public link firstLink ;
linkList()
{
firstLink = null ;
}
public boolean isEmpty()
{
boolean empty ;
if(firstLink==null)
{
empty = true ;
}
else
{
empty = false ;
}
return empty;
}
public void insertLink(String bookName , int millionSold)
{
link newLink = new link(bookName,millionSold);
newLink.next = firstLink;
firstLink=newLink ;
}
public link removeLink()
{
link linkReference = firstLink ;
if(!isEmpty())
linkReference.next= firstLink;
else
System.out.println("The List is Empty");
return linkReference ;
}
public void display()
{
while(firstLink!=null)
{
firstLink.display();
System.out.println("The Next :"+firstLink.next);
firstLink = firstLink.next;
System.out.println();
}
}
public link findItem(String bookName)
{
if(!isEmpty())
{
while(firstLink.bookName != bookName)
{
if(firstLink.next == null)
{
return null ;
}
else
{
firstLink=firstLink.next;
}
}
}
else
{
System.out.println("Can not find a match");
}
return firstLink ;
}
}
In the removeLink the assignment linkReference.next= firstLink; assigned firstLink to linkReference.next where infact you want the other way round. You want firstLink to be linkReference.next so try changing firstLink=linkReference.next;
Another observation is in display and findItem do not use firstLink as you changing the pointer to your list (your only entry-point) as you run the method.
The problem is in your display() method. You iterate through the list and after that the firstLink variable becomes null. You need to add a temporary variable just for iteration in the display() method, like:
link tempLink = firstLink;
while (tempLink != null) {
tempLink.display();
System.out.println("The Next: " + firstLink.next);
tempLink = tempLink.next;
System.out.println();
}
Use the same technique in other places where you iterate through the list but don't want to change a head (e.g. findItem() method).
I would also recommend capitalize name of all classes in Java. You can read more about Java naming conventios here.
Use temp variable (cursor below) to navigate through your list. Otherwise you're defining null to firstLink and it seems like list is empty.
public void display()
{
link cursor = firstLink;
while(cursor!=null)
{
cursor.display();
System.out.println("The Next :"+cursor.next);
cursor = cursor.next;
System.out.println();
}
}
and doing the same where you want to loop through list ( ex. findItem()) will be better approach.

Related Links

regular expression to avoid placing dot if there is already a dot
How do I save objects/instances while running a jar-file?
Issue about spring and spring boot properties file. I cannot pass properties as well as I want from one application in springBoot to another in spring
ArrayList objects storing unexpected values
Should I map all my indexes with my analyzer on ElasticSearch
If-else statement not working
Java Twitter4j addListener not defined for type twitterStream
Java servlet: How to remove header “referer” on redirect?
Android Display Images from directory
Spring MVC 4.1 ResourceUrlProvider not found
Running Java program by a Java program [closed]
duplicated blocks are not detected from Sonar Eclipse Plugin
Converting Cursor into generic object android
Whats wrong with my delete method in LinkedList (generic)?
What is the simplest way to run method asynchronously and being able to see its state?
Why does Java have both CountDownLatch and CyclicBarrier?

Categories

HOME
compiler-construction
deployment
debugging
mediawiki
iot
malloc
microservices
ojdbc
add
gitpitch
u-sql
ibm-odm
nhibernate-envers
google-cloud-speech
karma-jasmine
jprofiler
zapier
java-3d
cloudhub
pass-by-reference
opennlp
phpfox
gitignore
galsim
fog
repo
fusionpbx
io-redirection
swisscomdev
event-driven
libvpx
impersonation
powershell-dsc
rails-routing
setter
carrot
r-forge
akka-cluster
pillow
mako
spring-cache
player
flash-cs5
iis-arr
deis
responsive-images
web-controls
notify
dia
typekit
c3
issuu
googlemock
android-radiobutton
reactfx
aqtime
ora-00911
referrer
word-processor
labwindows
twrequest
reddot
pendrive
punbb
hgsubversion
eclipse-templates
gcj
tomcat-valve
android-sdk-2.1
remember-me
yslow
revisions
jquery-ui-droppable
dbal
web-architecture
iweb
virtual-functions
premature-optimization
port-number
zune
w3wp
wsdl.exe
caching-application-block

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App