-2

Below program is in c . I want to delete the last element of list .I can't understand why it is not deleted. I am setting the last element as null in delete function which can be later seen in del variable . But the list having last element is not setting it to null.Please anyone explain.

#include <stdio.h>
typedef struct node 
{
    int info;
    struct node *next;
}mynode;

void add(int val,mynode **head,mynode **tail);
void print(mynode *head);
void delete(mynode **head);

//mynode *head,*tail,*temp;
mynode *del;
int main(void) {
    // your code goes here
    mynode *root,*head,*tail,*temp;
    head=(mynode*)0;
    add(2,&head,&tail);
    add(3,&head,&tail);
    add(4,&head,&tail);
    add(5,&head,&tail);
    add(6,&head,&tail);
    print(head);
    del=tail;
    delete(&head);
    print(head);
    return 0;
}
void add(int val,mynode **head,mynode **tail)
{
    mynode *temp;
    temp=(mynode*)malloc(sizeof(struct node));
    temp->next=(mynode*)0;
    temp->info=val;
    if(*head==(mynode*)0)
    {
        *head=temp;
        *tail=temp;
    }
    else
    {
        (*tail)->next=temp;
        *tail=temp;
    }
}

void print(mynode *head)
{
   mynode *temp;
   if(head==(mynode*)0)
   {
        return;
   }
   printf("\n\n");
   for(temp=head;temp!=(mynode*)0;temp=temp->next)
   {
       printf("[%d]->",temp->info);
   }
   printf("NULL\n\n");
}
//deletes elements from linked list
void delete(mynode **head)
{
    if(del==head) 
    {
        *head=del->next;
        del=(mynode*)0;
    }
    else if(del->next==(mynode*)0)
    {
        del=(mynode*)0;
    }
    else 
    {
        del->info=del->next->info;
        del->next=del->next->next;
        del=(mynode*)0;
    }
}

Output:

[2]->[3]->[4]->[5]->[6]->NULL

[2]->[3]->[4]->[5]->[6]->NULL

wildplasser
  • 43,142
  • 8
  • 66
  • 109

2 Answers2

0

It seems you have misunderstood what it takes to delete an element in a link list. Doing:

del=(mynode*)0;

does not remove anything from the list. It only changes the value of del

To remove an element from a link list, you need to change the next pointer of the previous element (i.e. the element before the element you want to delete).

Example deleting B:

List:
A --> B --> C --> NULL

// Do:
A->next = C

New list:
A --> C --> NULL

Since you are using dynamic memory, you also need to free the memory (aka free(B)).

BTW:

1) Having del as a global variable is bad. Pass it as an argument instead.

2) This code: if(del==head) is comparing a mynode* with a mynode** That is probably not what you want. Instead use if(del==*head)

So your delete function could be more like:

void delete(mynode **head, mynode *d)
{
    if (*head == NULL) return;

    if(d==*head) 
    {
        *head=d->next;
        free(d);
        return;
    }

    mynode *p = *head;
    mynode *t = p->next;
    while(t)
    {
        if(t==d) 
        {
            p->next=d->next;
            free(d);
            return;
        }
        p = p->next;
        t = p->next;
    }
}
Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
0

You need to traverse till previous node of del, lets call it 'prev'

prev->next = del->next // assuming del is not null
free(del);
Pras
  • 4,047
  • 10
  • 20