删除双向链表中的节点(数据结构)

时间:2016-07-30 14:30:21

标签: c

在双向链表的删除节点功能中,任何人都可以解释一下(指针 - >下一个) - > data!= while循环中的数据吗?

   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;
}

1 个答案:

答案 0 :(得分:0)

您可以使用此代码插入删除和更新。

#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;
            }
}