views:

579

answers:

6

EDITED: using c++ to code.

void circularList::deleteNode(int x)
{
    node *current;
    node *temp;
    current = this->start;

    while(current->next != this->start)
    {
        if(current->next->value == x)
        {
            temp = current->next;
            current->next = current->next->next;
            delete current->next;
        }
    else{ 
            current = current->next;
             }
    }
}

Added the else i'm sorry i kinda forgot to copy that part of the code and yes it is for learning purposes. I'm new to coding with c++ and probably come off as a noob sorry about that.

Also as for this line of code

this->start->value == x

im not sure what you mean by it or where you think it goes, yes there are nodes in the linked list and assume that it will always have at lease 1 node all the time.

+6  A: 

Think about this two lines:

current->next = current->next->next;

delete current->next;

Try to determine what you are actually deleting (no its not current->next;, at least not the one you want to delete).

Let_Me_Be
Yep, this is another problem.
Justin Peel
+6  A: 

You never move to the next node in your while loop. After your if, you should have:

else
    current = current->next;

Also, you might want to consider returning from the function after you've found the node (unless you suspect that two nodes have the same value).

Justin Peel
+3  A: 

In addition to Justin's and Let_Me-Be's answers, consider what you might need to take care of when

this->start->value == x

If you don't handle that right, you'll lose your whole list (or crash trying to get to it)...

Michael Burr
currently it looks like it would never check the start node
matt
@matt: right, I missed that. But once that bug was fixed, this one would be next on the list, no?
Michael Burr
yeah, and is by far A LOT worse.
matt
the way I like to fix this problem is to not make start a pointer to a node in the list, but a dummy node itself in the list. that way you don't have to worry about checking this edge case, well, at least that's what I do for doubly linked lists, and in this case you would have to make sure the dummy node's value was set to something that would NEVER be passed to this function.
matt
+1  A: 

Do you have only a singularly linked list?

Have you considered the STL, perhaps a deque?

Or, if you must have a single linked list then why not take something that it 90% STL (;-) and look at the Boost libraries?

You do seem to be reinventing the wheel here. Why not take some existing - and tested - code and use that?

Mawg
See all those comments? You wouldn't get them if you used an existing, tried and tested solution. IS there something unique about your singly linked list that makes it different from all the previously coded and tested singly linked lists? You probably have more important things to spend your time on (just trying to be helpful - honest)
Mawg
I'm guessing this is for school/education.
Justin Peel
mawg - good points, but I assumed this was a learning exercise.
Michael Burr
though you probably should be able to tell the problems with this code too. ESPECIALLY if you're coding in c++
matt
If it's a learning exercise, then perhaps learning the debugger might useful. I would suggest that OP step through his code, examining it as he goes and really understand what it actually does.If the code has no GUI, I strongly recommend using Eclipse as an IDE, especially because of the myriad of plugings available.If OP is not using Linux, then one look at this picture might convince him to do so ... http://www.gnu.org/software/ddd/all.png tehre's nothing better for linked lists (sigh! If only there were a version for windows, or I could get X-windows working with Cygwin)
Mawg
OP, you might also want to get used to the idea of writing unit tests for your code. I strongly recommend CPPunit, but use whatever you prefer (CPPunit also has a good Eclipse plugin).Every time you change your code, just run the tests to see if you broke anything.And, since you are learning and want to do things the right way, you ought to have written a design document (and had it reviewed) before you started testing.
Mawg
A: 

Is "deleting a node from linked list" the issue or "deleting a node from a circular linked list" the issue?

The difference from non-circular to circular is the line:

while(current->next != this->start)

If the list were non circular it would have been

while(current->next != NULL )

Apart from that, there are issues with the code snippet, which others have already pointed out.

ArunSaha
A: 

Another thing: Do you check that you have at least one valid node in your list before you call deleteNode?

I ask this as your function does not check for a NULL-value of the start element.

The solution would be to add the line

if(start == NULL) return

as first line of your method to avoid this.

sum1stolemyname