what is the method for removing a node from a linked list in pseudocode?

i know how to add (im coding in java btw) but deleting has me stumped

any pointers?

Recommended Answers

All 16 Replies

any pointers?

I'll pardon the pun.

Do you want it for a singly linked list or a doubly linked list? I'll consider doubly linked.

Say you've got three nodes (which contain a value and left and right pointers), at locations W, X, and Y:

W={value:_; left: V; right: X}
X={value:_; left: W; right: Y}
Y={value:_; left: X; right: Z}

If you delete X, the list should look like:
W={value:_; left: V; right: Y}
X={who cares}
Y={value:_; left: W; right: Z}

Given the reference with value X, it's a simple matter of code to make stuff look like the version where X is removed. The only edge cases (maybe) are where the node you want to remove is at the beginning or end of the list. Sometimes those are edge cases, and sometimes not---it depends on your list representation.

Singly linked

I have nodes which are instance of classes called Node. Node has the fields Name and Price. It also has a field called Head which is a pointer to the next node.

I have the list which is an instance of the List class

The deletenode() method in List takes a value (name) and deletes it,

I just cant figure out the logic of how to do it.

As you walk through the nodes, looking for the matching value, keep track of the previous node.

yeah i can find it, its when i delete it, do i note what is before and after it? that way i can adjust the head pointer of the preceeding node to point to the one after the node to be deleted?

Imagine what the list should look like after you've deleted the element; and make it so.

ah sorted it

the logic was right after all
i was using = to compare strings, not .equals, which explained why it couldnt find the node

That's what you get for using the worst programming language in popular use today :P

we use it in class just for learning OOP and
understanding core computer science concepts (using the BlueJ environment), so it doesnt really matter whether or not its viable as an industry tool. Next semester we move to C++

Ah, moving on to the second-worst!

That's what you get for using the worst programming language in popular use today :P

But thats improper use rather than the language fault. Also are you a C guy. Nowadays many Java loyals are turning to Python, they seem to be of the same opinion. I know this might seem off-topic, but we java programmers are curious about the reason.

i dunno i like java because its write once, run anywhere (so long as a VM exists for that platform of course) which is nice.

But thats improper use rather than the language fault.

No, Java is actually the worst, and it's the language's fault. It has null pointers and no closures. What other popular language his this combination?

but it has auto garbage collection?

So does every other language.

Edit: That matters. Besides C++ and C, which fill their niches decently.

i dunno i like java because its write once, run anywhere

Don't know if I'm violating any rules or something, but someone once told me:

Saying that Java is great because it runs on all operating system, is saying anal sex is great because it works on all genders

:)

haha thats a good one

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.