java - Adding an element to a singlechained linked list -
okay, solution easy don't understand @ moment.
code:
listelem<t> first; int size = 0; public void add(t value) { if (value == null) return; listelem<t> elem = new listelem<t>(value); elem.next = first; first = elem; size++; }
how add element @ beginning of singlechained linked list? create new element given value.
what happens in next 2 lines? understand process of inserting element in list i'm not able relate code.
and first? head?
before adding stack looks :
first -> next -> next -> ... -> end;
you create elem.
then said "the next elem of elem first elem".
elem.next = first;
have
elem -> first;
finally set first elem elem. stack looks :
elem -> first -> next -> ... -> end;
and first id elem return first state :
first -> next -> next -> ... -> end; (first new elem added)
this schema may helpful :
Comments
Post a Comment