0

In delete node function for doubly linked list can anyone explain me how (pointer->next)->data != data in while loop work?

   void delete(node *pointer, int data) {
       while (pointer->next!=NULL && (pointer->next)->data != data) {
           pointer = pointer -> next;
       }

       if (pointer->next == NULL) {
           printf("Element %d is not present in the list\n", data);
           return;
       }

       node *temp;
       temp = pointer->next;

       pointer->next = temp->next;
       temp->prev =  pointer;

       free(temp);

       return;
}
13
  • What is pointer->next? Is it the next node in the list? Then think a little more... What could then pointer->next->data be? What could be the reason to compare pointer->next->data with this->data (I assume)? Commented Jul 30, 2016 at 14:31
  • It is the data in the next node got it @JoachimPileborg Commented Jul 30, 2016 at 14:32
  • Also, are you sure you're programming in C++ and not C? There's no C++ specific code, rather you are using delete as a function name which would not be allowed in C++ since delete is a keyword in C++. Please tag your questions appropriately. Commented Jul 30, 2016 at 14:33
  • I have written the code @mik1904 Commented Jul 30, 2016 at 14:34
  • 1
    Read a book on C++. It'll have a far lenghier, and a more detailed explanation of how pointers work, than a 500 character comment on stackoverflow.com Commented Jul 30, 2016 at 14:40

1 Answer 1

0

You can use this code for inserting deleteing and updating .

#include<stdio.h>
struct double_linklist{
    int val;
    struct double_linklist *back;
    struct double_linklist *next;
}*start=NULL;
typedef struct double_linklist dl;

void main()
{
    dl *current,*New,*min;
    int n,ch,i;
    do{
        New = (dl *)malloc(sizeof(dl));
        system("cls");
        printf("enter element");
        New->val=getch()-'0';
        printf("%d",New->val);
        New->back = NULL;
        New->next = NULL;
        if(start == NULL)
        {
            start = New;
            current = New;
        }

        else{
            current->next = New;
            New->back = current;
            current = New;
        }
        printf("\n\nIfyou want to input next element then press y");

    }while(getch()=='y');

    system("cls");
    printf("the elements are : \n");
    current = start;
    while(current->next != NULL)
        {
            printf("%d-->",current->val);
            current = current->next;
        }
        printf("%d",current->val);


        printf("\n\n1. Insert\n2. Delete\n3. Search\n4. Sort\n");
        ch=getch()-'0';
        switch(ch){
            case 1 : printf("enter the position");
                        n=getch()-'0';
                     printf("enter the val");
                     New = (dl *)malloc(sizeof(dl));
                     New->val =getch()-'0';
                     New->back = NULL;
                     New->next = NULL;
                     current = start;
                     for(i=0;i<n-1;i++)
                     {
                        current =current->next;
                     }
                     current->back->next= New;
                     New->back = current->back;
                     New->next= current;
                     current->back=New;

                     printf("after inserting the link list are : ");
                     current =start;
                        while(current->next != NULL)
                    {
                        printf("%d-->",current->val);
                        current = current->next;
                    }
                    printf("%d",current->val);
                    break;

            case 2: printf("enter a value do you want to delete : ");
                    n=getch()-'0';
                    printf("%d",n);
                    current = start;
                    while(current->val!=n)
                        current = current->next;
                    current->back->next = current->next;
                    current->next->back = current->back;
                    current->back = NULL;
                    current->next = NULL;
                     printf("after deleting the link list are : ");
                     current =start;
                        while(current->next != NULL)
                    {
                        printf("%d-->",current->val);
                        current = current->next;
                    }
                    printf("%d",current->val);
                    break;

            case 3: printf("enter a number do you want to search");
                    n=getch()-'0';
                    printf("%d",n);
                    current = start;
                    i=1;
                    while(current->next != NULL)
                    {
                        if(current->val == n)
                            printf("%d\t",i);
                        current = current->next;
                        i++;

                    }
                    if(current->val == n)
                            printf("%d\t",i);
                    break;

            case 4: New=start;
                    current = start;
                    min = current;
                    while(New->next != NULL)
                    {

                        while(current->next != NULL)
                        {
                            current = current->next;
                            if(current->val < min->val)
                                min = current;
                        }
                        ch = min->val;
                        min->val = New->val;
                        New->val = ch;

                        New = New->next;
                        current = New;
                        min = current;
                    }
                    printf("\nAfter sorting:\n");
                     current = start;
                        while(current->next != NULL)
                    {
                        printf("%d-->",current->val);
                        current = current->next;
                    }
                    printf("%d",current->val);
                    break;
            }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.