2

I made a code of deleting a node by its address only and the function deleteNode is deleting the node at that position but it is deleting more values and I am not being able to delete the last node.

#include<bits/stdc++.h>
using namespace std;

struct Node {                                   //node defination
  int data;
  struct Node *next;
  Node(int x) {
    data = x;
    next = NULL;
  }
}*head;

Node *findNode(Node* head, int search_for)     //find the node that will delete
{
    Node* current = head;
    while (current != NULL)
    {
        if (current->data == search_for)
            break;
        current = current->next;
    }
    return current;
}


void insert()                                 //insert the values in node
{
    int n,i,value;
    Node *temp;
    scanf("%d",&n);

    for(i=0; i<n; i++)
    {
        scanf("%d",&value);
        if(i==0)
        {
            head=new Node(value);
            temp=head;
            continue;
        }
        else
        {
            temp->next= new Node(value);
            temp=temp->next;
            temp->next=NULL;
        }
    }
}

void printList(Node *node)                   //print the value in node
{
    while (node != NULL)
    {
        printf("%d ", node->data);
        node = node->next;
    }
    cout << endl;
}
void deleteNode(Node *node_ptr);

int main(void)                              //main starts from here
{

    int k,n,value;

    {
        insert();
        scanf("%d",&k);
        Node *del = findNode(head, k);
        if (del != NULL && del->next != NULL)
        {
            deleteNode(del);
        }
        printList(head);
    }
    return(0);
}
void deleteNode(Node *pos)                  //delete node function
{
   struct Node *temp;
   while(temp->next!=0)
   {
       temp=pos->next;
       pos->data=temp->data;
       pos->next=temp->next;
       pos=temp;
   }
 }

Input

5 (size of a linked list)

1 2 3 4 5 (elements of list)

2 (position to delete)

Expected output

1 3 4 5

Current output

1 3 5

BATMAN_X
  • 371
  • 1
  • 8
  • 1
    Where the "6" in your Expected output come from? – MikeCAT Jul 10 '20 at 17:49
  • 1
    In `deleteNode` shouldn't `temp` start from `head`? Use a debugger to step through your code, and you'll find the error. – cigien Jul 10 '20 at 17:50
  • 3
    `struct Node *temp; while(temp->next!=0)` invokes *undefined behavor* for using (indeterminate) value of uninitialized non-static local variable. – MikeCAT Jul 10 '20 at 17:50
  • @MikeCAT I typed it mistakenly. – BATMAN_X Jul 10 '20 at 17:51
  • 3
    Please read [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) and [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) And stop using online competition sites as a learning resource. Get [some good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) to read instead, or better yet take some classes. – Some programmer dude Jul 10 '20 at 17:52
  • 2
    `struct Node *temp;` Note that in c++ `Node *temp;` is sufficient. You don't need the struct before that. – drescherjm Jul 10 '20 at 17:54

1 Answers1

0

Create a destructor:

struct Node {                                   //node defination
  int data;
  struct Node *next;
  Node(int x) {
    data = x;
    next = NULL;
  }
  ~Node() {    // destructor
    if (next)
      delete next;
  }
}*head;

So when deleting, all nodes will be deleted.

The deleteNode function doesn't need to traverse the list:

void deleteNode(Node *pos)                  //delete node function
{
   struct Node *temp;

   if(pos->next != 0)
   {
       temp = pos->next;
       pos->data = temp->data;
       pos->next = temp->next;
       temp->next = nullptr;  // <- set next to null to avoid next nodes deletion
       delete temp;  // and delete
   }
}

And in main just delete the head:

if (head)
    delete head;
Manuel
  • 2,401
  • 1
  • 11
  • 15