hi,
im currently revising linked lists for my exams - but i cant quite understand the reasoning behind using current or current.next in the following examples
in the example below we are adding to the back of a linked list
void addToBack(int value)
{ // make a new element
Element newElement =new Element(value) ;
if (myList == null)
// add to empty list
myList = newElement ;
else { // scan to end of list ]
Element current = myList ;
while ([B]current.next[/B] != null)
current = current.next ;
// add to end of list
current.next = newElement ;
}
} // end of addToBack method
here we are removing the last element from the linked list
int removeLastElement()
{ if (myList == null)
REPORT AN ERROR
else
{ Element current = myList ;
Element previous = null ;
while ([B]current.next[/B] != null)
{ previous = current ;
current = current.next ;
}
int value = current.data ;
if (previous == null)
myList = null ;
else
previous.next = null ;
return value ;
}
} // end of removeLastElement method
in the example below we are removing the element marked X
{ Element current = myList ;
Element previous = null ;
boolean isFound = false ;
while (([B]current[/B] != null) && (!isFound))
{ if (current.data == X)
isFound = true ;
else
{ previous = current ;
current = current.next ;
}
}
if (!isFound)
REPORT “ELEMENT NOT FOUND”
else if (previous == null)
myList = current.next ;
else
previous.next = current.next ; }
and finally the code below searches for the value X
boolean isFound = false ;
Element current = myList ;
while (([B]current [/B]!= null) && (!isFound))
{ if (current.data == X)
isFound = true ;
else
current = current.next ; }
as you can see from the code segments above some of the while loops use current and some use current.next - i have copied these codes from my lecturers slides but i am unable to contact him at the moment and would really appriciate it if anyone can tell me why in some instances current is used, and some current.next is used
thanks