views:

152

answers:

4

How can I delete a node (between two nodes) from a single linked list without passing any parameters to the class function?

For example, I have a list of 6 nodes with one head node and i want to delete 2 of them (without prior knowledge of their address or position) from a class function, how would i do that?

void WordList::deleteNode(){
Node *temp;
temp=head;
if(temp->count<=10)
{
//delete this node... not sure how though
}
else
temp=temp->next;
}

where WordList is my class, Node is my struct which holds a word, a count, and a pointer. I want to delete any node that has a counter of 10 or less.

+2  A: 

I find the question too confusing.

Deletion of a node from the list is always based on some criteria e.g. the content of the element, the position of the element etc (unless you are deleting all the elements in the list)

Chubsdad
Thanks, i edited the question. Hopefully it is more clear now. I want to delete it based on the content of the element
Chase Sawyer
+2  A: 

something like this:

void WordList::deleteNode(){
Node *prev=NULL;
temp=head;
bool done=false;
while (!done)
{
  if (temp->count<=10)
  {
    if (prev == NULL)
    {
      head = temp->next;
    } else
    {
      prev->next = temp->next;

    }
    // delete data in temp, and the node if necessary
    temp = temp->next;
    done = (temp==NULL) || // some other condition, like deleted 2
  } else
  {
    prev=temp;
    temp = temp->next;
    done = (temp==NULL);
  }
}
paquetp
+1. But I would just use `while(temp)`.
sje397
@sje397: agreed...I had assumed the OP wanted some other conditions other than 'I've iterated the entire list'. thx for the up
paquetp
A: 

Your edit has prior information, the bit that states "counter <= 10" :-)

Pseudo-code for deleting elements meeting that criteria in a singly-linked list:

def delLessThanTen:
    # Delete heads meeting criteria, stop when list empty.

    while head != NULL and head->count <= 10:
        temp = head->next
        free head
        head = temp
    if head == NULL:
        return

    # Head exists, with count > 10, process starting there (we check
    #    NEXT element for criteria then delete if met).

    ptr = head
    while ptr->next != NULL:
        # If next in list meets criteria, delete it, otherwise advance.

        if ptr->next->count <= 10:
            temp = ptr->next->next
            free ptr->next
            ptr->next = temp
        else:
            ptr = ptr->next

    return
paxdiablo
A: 

Have a previous variable initialized to null. If you delete a node, change previous's next to the element's next, unless previous is null (you are at the start of the list) when you leave previous null and change root to the deleted element's next. If you don't delete the element, change previous to the element.

Here previous will always point to the previous element or be null if you're at the start of the list.

void WordList::deleteNode() {
    Node *temp = head;
    Node *previous = null;
    while (temp != null) {
        if(temp->count <= 10) {
            // delete node
            if (previous == null) {
                // there is no previous node, so point head of list past the current node
                head = temp->next;
            } else {
                // there is a previous node, so just point it past the current node
                previous->next = temp->next;
            }
        } else {
            // not deleting, so set previous to temp
            previous = temp;
        }
        temp = temp->next;
    }
}
Jason Goemaat