list - Removing nodes in LinkedList in Java -


okey, understand basics of linked lists. know how each node has reference next node, , how it's tied up.

my code working no issues, issue don't how it's working (yeah...)

my question removing node in linked list. let's i'm removing person in list, in middle of list. in method, i'm making temp variable of nextperson, , right logic, i'm deleting node, i'm not changing in "global" nextperson afterwards. thing is.. apperently affects firstperson anyways , removing same node firstperson.

i know linked list confusing, , question. if there's i'm not clear about, can explain further..

  public class store {     private person firstperson;      void newperson(person person)     {     if (firstperson == null)     {         firstperson = person;     }     else     {         person.nextperson = firstperson;         firstperson = person;     } }  void checkout() {     person temp = firstperson;     while (temp.nextperson != null)     {         system.out.println(temp.getname() + " bought" + temp.getitem() + ".");         system.out.println("- hade bra!");         temp = temp.nextperson;     } }  //this method: boolean deleteperson(string name) {       person temp = firstperson;     if (temp.getname().equals(name))     {         temp = temp.nextperson;     }     else     {         while (temp.nextperson != null)         {             if (temp.nextperson.getname().equals(name))             {                 //removing node                 temp.nextperson = temp.nextperson.nextperson;                 //not changing in "firstperson", still changes                 return true;             }             else             {                 temp  = temp.nextperson;             }         }     }     return false;      } }     public class person { private string name, item; person nextperson;  person(string name, string item) {     this.name = name;     this.item = item; }  public string getitem() {     return item; }  public string getname() {         return name;     } } 

in java, variables containing objects contain references objects, when assign temp firstperson, every time use temp equivalent using firstperson because both reference same object in memory.


Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -