0

I was working on the codes for Deletion from a Circular Linked List. While working on this code on this, I realized that there was an error while deleting the first element of the list, but I could not find how to fix this error because I am not fully aware of the subject. Help with this

    // C++ program to delete a given key from
// linked list.
#include <bits/stdc++.h>
using namespace std;

/* structure for a node */
class Node {
public:
    int data;
    Node* next;
};

/* Function to insert a node at the beginning of 
a Circular linked list */
void push(Node** head_ref, int data)
{
    // Create a new node and make head as next
    // of it.
    Node* ptr1 = new Node();
    ptr1->data = data;
    ptr1->next = *head_ref;

    /* If linked list is not NULL then set the 
    next of last node */
    if (*head_ref != NULL) 
    {
        // Find the node before head and update
        // next of it.
        Node* temp = *head_ref;
        while (temp->next != *head_ref)
            temp = temp->next;
        temp->next = ptr1;
    }
    else
        ptr1->next = ptr1; /*For the first node */

    *head_ref = ptr1;
}

/* Function to print nodes in a given 
circular linked list */
void printList(Node* head)
{
    Node* temp = head;
    if (head != NULL) {
        do {
            cout << temp->data << " ";
            temp = temp->next;
        } while (temp != head);
    }

    cout << endl;
}

/* Function to delete a given node from the list */
void deleteNode(Node** head, int key) 
{
    
    // If linked list is empty
    if (*head == NULL)
        return;
        
    // If the list contains only a single node
    if((*head)->data==key && (*head)->next==*head)
    {
        free(*head);
        *head=NULL;
        return;
    }
    
    Node *last=*head,*d;
    
    // If head is to be deleted
    if((*head)->data==key) 
    {
        
        // Find the last node of the list
        while(last->next!=*head)
            last=last->next;
            
        // Point last node to the next of head i.e. 
        // the second node of the list
        last->next=(*head)->next;
        free(*head);
        *head=last->next;
    }
    
    // Either the node to be deleted is not found 
    // or the end of list is not reached
    while(last->next!=*head&&last->next->data!=key) 
    {
        last=last->next;
    }
    
    // If node to be deleted was found
    if(last->next->data==key) 
    {
        d=last->next;
        last->next=d->next;
        free(d);
    }
    else
        cout<<"no such keyfound";
    }

/* Driver code */
int main()
{
    /* Initialize lists as empty */
    Node* head = NULL;

    /* Created linked list will be 2->5->7->8->10 */
    push(&head, 2);
    push(&head, 5);
    push(&head, 7);
    push(&head, 8);
    push(&head, 10);

    cout << "List Before Deletion: ";
    printList(head);

    deleteNode(&head, 7);

    cout << "List After Deletion: ";
    printList(head);
    deleteNode(&head, 10);

    cout << "List After Deletion: ";
    printList(head);

    return 0;
}

result must be like this

List Before Deletion: 10 8 7 5 2 
List After Deletion: 10 8 5 2 
After Deletion: 8 5 2 
Program ended with exit code: 0

but result is like this

List Before Deletion: 10 8 7 5 2 
List After Deletion: 10 8 5 2 
no such keyfoundList After Deletion: 8 5 2 
Program ended with exit code: 0

thanks to answer @ashu. I added return.

...
    if((*head)->data.id==key)
    {

        // Find the last node of the list
        while(last->next!=*head)
            last=last->next;

        // Point last node to the next of head i.e.
        // the second node of the list
        last->next=(*head)->next;
        free(*head);
        *head=last->next;
        return;
    }
...
b219
  • 33
  • 4
  • 3
    Off topic, but geeksforgeeks is not a reliable source for learning. – molbdnilo Dec 02 '20 at 08:17
  • why off topic? Although it is not a reliable source, I am trying different resources to learn the codes. Every time I ask a question it's criticized instead of answering. – b219 Dec 02 '20 at 08:34
  • I was reflecting on my own comment. If you want to waste time on a known bad site, go ahead. – molbdnilo Dec 02 '20 at 08:43
  • Your program has undefined behaviour due to your passing a pointer you got from `new` to `free`. – molbdnilo Dec 02 '20 at 08:45
  • @molbdnilo: can you share some good sources to learn c++ in depth(not just writing the code but to give you an idea what's is going on in your system)? Geeksforgeeks is not so reliable I had no clue about that thanks for telling this. – Pygirl Dec 02 '20 at 09:11
  • 1
    There is a list of good books [here](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). There are unfortunately no known good free resources, unless you have ready access to a library. – molbdnilo Dec 02 '20 at 09:14

1 Answers1

1

You need to add a return statement when head node is the one which needs to be deleted.

Also, as @molbdnilo pointed out, you should use (new and delete) or (malloc and free).

ashu
  • 48
  • 7