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

JTable listen for entering editing mode
Preserve Hibernate Lazy loading in data transfer object design pattern
When I check for signed jar's expiration date, I see there are more than one certificates?
Spring Boot: Use different Tomcat Version
Java TreeMap malfunctioning
Date parsing error by selecting different language selection on android device
Java: How to have if statements read both numbers and words
Hibernate - does not create table on Ubuntu mysql when #Column(length= 40000) is used
Some example usage of Appache parquet
Prerender.io not caching pages - followed all steps as per documentation
Chrome CustomTab error: java.lang.NoSuchMethodError: No static method startActivity
passing string values to other function from onclick date listner
Search Queries for Gerrit
java.lang.IllegalArgumentException: object is not an instance of declaring class?
Taking control of the app if a url loads after the initial in a WebView
Java Swing GUI error Null Pointer [duplicate]

Categories

HOME
coq
push-notification
elm
twitter-bootstrap-4
retrofit
constraint-programming
qore
paradox
esper
amazon-cloudformation
node-pdfkit
quickbooks
google-apps-marketplace
moonmail
commonmark
orchardcms
fortumo
vaadin7
propel
connection-string
beyondcompare
crystal-reports-2008
custom-wordpress-pages
nas
xlsxwriter
java-3d
errorlevel
wijmo
sqlcipher
shopware
one-to-many
numerical-methods
sparse-matrix
virtualdub
maquette
wtx
primitive
tasklet
sql-server-2012-express
galsim
hybridauth
windows-dev-center
uiswipegesturerecognizer
catalog
bower-install
wixsharp
multilingual
dynamics-crm-2013
c11
sqlite2
grid.mvc
http-live-streaming
spring-security-kerberos
estimote
outlook-api
galleria
cubic-spline
osx-mavericks
appcompat
apache-fop
atomicity
theming
typescript1.8
snmptrapd
np-complete
color-profile
yt-project
root-framework
cubes
setuptools
underscore.js-templating
bstr
qpid
pickadate
bluemix-app-scan
apachebench
itextpdf
photobucket
xna-4.0
t4mvc
django-unittest
tmuxinator
wordml
dukescript
drawbitmap
responsive-images
apache-commons-fileupload
composite
fscommand
typekit
expected-exception
system.net.webexception
mbr
imdbpy
page-layout
oam
quantlib-swig
drools-planner
bubble-chart
asyncfileupload
custom-backend
eclipse-templates
nsobject
heartbeat
celltable
pyinotify
imac
google-friend-connect
dmx512
asp.net-profiles
multi-tier
sef
ctp4
eqatec
misv

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App