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 :

enter image description here


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -