4 Replies - 434 Views - Last Post: 22 November 2015 - 04:55 AM Rate Topic: -----

#1 DDNPresident   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 58
  • Joined: 23-October 13

Remove node from linked list (Disclaimer:HW help)

Posted 21 November 2015 - 10:03 PM

I don't know how to approach the removal of an item that is put into a list. I mistakenly sought advice from someone else and they helped me with the code with adding the words/definitions. But , I obviously did not understand it fully so I don't know how to remove it. I can post the full code if needed but I wanted to just show a snippet of the code to see if what was done with add even makes sense.

void add(WordMeaning wm) {
        

        WordMeaningNode temp = new WordMeaningNode(wm);
        
        if (list == null)
            list = temp;
        else {

            WordMeaningNode currentEntry = list;
            WordMeaningNode back = null;
            boolean found = false;  
            
            while(currentEntry != null && !found)

                if( temp.getWordMeaning().getName().compareTo(currentEntry.getWordMeaning().getName()) <= 0 )
                    found = true;

                else {
                    back = currentEntry;
                    currentEntry = currentEntry.nextMean;
                }

            temp.nextMean = currentEntry;
            if (back == null)
                list = temp;
            else
                back.nextMean = temp;
        }
}



Any help would be appreciated. I don't want the answer directly because that won't help me. (it didn't help me with the add portion). I have searched around this forum as well as in other forums to try and understand the concept , but for some reason it's just not making sense. I don't understand how ".nextMean" works to sort around the list.

What I understand is I have...

A-B-C-D

If I want to remove C I need:

A-B-D

I come for advice from people who have much greater knowledge than I have at the moment. That's all. I have worked for a long time attempting different ways of attempting it . I thought I could use something similar to what was done with add but just couldn't get it. Sorry if this is long winded. I just don't know where to even begin.

Is This A Good Question/Topic? 0
  • +

Replies To: Remove node from linked list (Disclaimer:HW help)

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14155
  • View blog
  • Posts: 56,742
  • Joined: 12-June 08

Re: Remove node from linked list (Disclaimer:HW help)

Posted 21 November 2015 - 10:10 PM

What is a node? It's a container for some value and a pointer to the next node in the list, right?

The gist is you would start at the head.. Put that in a 'previous' bucket.
Examine the first node. Does that value match? If so save that node's "next". Delete the node and make the head's "next" point to the deleted node's "next". Basically you stitch the two ends together.
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12324
  • View blog
  • Posts: 45,424
  • Joined: 27-December 08

Re: Remove node from linked list (Disclaimer:HW help)

Posted 21 November 2015 - 10:23 PM

We have an excellent linked list tutorial you may find helpful. :)
Was This Post Helpful? 0
  • +
  • -

#4 DDNPresident   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 58
  • Joined: 23-October 13

Re: Remove node from linked list (Disclaimer:HW help)

Posted 21 November 2015 - 10:54 PM

Thank you macosxnerd101 and modi123_1. I previously looked at the tutorial that you linked to before posting. I think I will just spend the night really watching online tutorials to see what I am doing wrong. I believe I just need to look at different implementations and maybe something will click. I appreciate that you guys chimed in.
Was This Post Helpful? 0
  • +
  • -

#5 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 697
  • View blog
  • Posts: 5,341
  • Joined: 25-December 13

Re: Remove node from linked list (Disclaimer:HW help)

Posted 22 November 2015 - 04:55 AM

It also helps if you take a piece of paper and pencil and draw a linked list with squares for the nodes and lines connecting the squaress for links. Put the values in the squares.
Then pick a node in the middle of the chain of nodes and redraw the links to remove it from the ;inked list.

This post has been edited by NormR: 22 November 2015 - 04:56 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1