0

I'm implementing a function where I would need to delete nodes based on given data and add the same time, I'll be carrying out this operating until my linked list reaches a NULL pointer.

At the same time, I need to also test whether my list is empty and print and another message and prevent the process above for being executed is there any way I can implement this?

void del(Node*&p, int k)
{
  if(ptr!=NULL)
  {
    if(ptr->data==k)
    {
      cout<<"Random Data"<<endl;
      Node*temp;
      temp=p;
      p=p->next;
      delete temp;
    }
    else
      del(p->next,k)
  }
}

Is there any way I can implement the same without recursion?

  • My bad I didn't realise, I made an edit thanks – Gary Andrews30 May 09 '17 at 13:54
  • Can you clarify what you are trying to accomplish? It seems to me you want to simply delete a node from your link list that has the variable k which is an int that is part of the Node non recursively. I don't understand why you have ptr and p (you delete p) which has nothing to do with ptr. And what do you mean by "and add the same time"? – Carlos May 09 '17 at 14:06
  • This is just the basic logic I'm implementing. When I invoke this function, I want to first test whether the linked list is empty, if it is, I want it to print so and so and not execute the other part. Is there any way I can implement that? – Gary Andrews30 May 09 '17 at 14:08
  • Yes, but the code will be a little bit messy due to p being passed by reference. – Johan May 09 '17 at 14:15
  • if (p->next == null) ? –  May 09 '17 at 14:17
  • For completeness, what is the argument Node p? And what is ptr? I could assume they are both pointers to the same list at the head? If so what is the point of Node p argument? You also don't need to recursively call the same function; you could just do a while loop. – Carlos May 09 '17 at 14:27
  • 1
    Useful reading: http://stackoverflow.com/questions/12914917/using-pointers-to-remove-item-from-singly-linked-list – user4581301 May 09 '17 at 15:15

3 Answers3

0

while to the rescue:

  while (p!=NULL)
  {
    if(p->data==k)
    {
      cout<<"Random Data"<<endl;
      Node*temp;
      temp=p;
      p=p->next;
      delete temp;
      return;
    }
    else
     p =  p->next,k
  }

this function doesn't needs to be recursive (it doesn't have a recursive nature like trees do) - you can always walk the linked list over a while and delete the nodes there.

Tomaz Canabrava
  • 2,320
  • 15
  • 20
  • If you delete a root node the pointer you've sent into the function will be invalid and the program might crash. – tna0y May 09 '17 at 14:23
0

Code written on a knee, so not guaranteed to work. But the concept is simple: you create a new node which points on the root, then clear the list in while loop and finally return whatever your artificial node is pointing at, that will be the new root of your linked list.

Node *del(Node *p, int k) {
    if (p == NULL) return;

    Node keeper;
    keeper.next = p;
    Node parent = keeper;

    do {
        if (p->data == k) {
            parent.next = p->next;
            delete p;
            p = parent.next;
        }else{
            parent = p;
            p = p->next;
        }
    } while (p != NULL);
    return keeper.next;
}
tna0y
  • 1,842
  • 3
  • 16
  • 33
0

You can do it by loop, example:

del(Node* p, int k) {
    Node head;
    head->next = p;
    head->data = 0;

    Node* pre = &head;
    for (; p != NULL; pre = p, p = p->next) {
        if (p->data == k) {
            pre->next = p->next;
            delete p;
            p = pre;
        }
    }
}
Jcppython
  • 179
  • 12