简体   繁体   English

删除双向链表中的 function

[英]Delete function in doubly linked list

I am new to C and was trying to create a phonebook application using a doubly-linked list.我是 C 的新手,正在尝试使用双向链表创建电话簿应用程序。 However, I have not been able to figure out how to delete the contact ie the first name, last name, and number of the person and linking the previous node the next one.但是,我无法弄清楚如何删除联系人,即人的名字、姓氏和号码,并将前一个节点链接到下一个节点。 I have attached the code below.我附上了下面的代码。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void create(int,char*,char*);
void search();
void display();
void del();


struct node
{
    struct node *before;
    int data;
    char fname[50];
    char lname[50];
    struct node* after;
};

struct node* head;

int main()
{
    printf("WELCOME TO PHONE DIRECTORY");
    int item,choice;
    char surname[50];
    char lastname[50];
    do
    {
        printf("\n1.CREATE\n2.SEARCH\n3.DELETE\n 4.EXIT\n 5.DISPLAY \n 6.ENTER YOUR CHOICE:");
        scanf("%d",&choice);
        switch (choice)
        {
        case 1:
            printf("ENTER NUMBER:");
            scanf("%d",&item);
            printf("ENTER FIRST NAME:");
            scanf("%s",surname);
            printf("ENTER LAST NAME:");
            scanf("%s",lastname);
            create (item,surname,lastname);
            break;

        case 2:
        search();
        break;

        case 3:
        del();
        break;

        case 4:
        return;
        break;

        case 5:
        display();
        break;

        default:
        printf("\n INVALID NUMBER");
            break;
        }
    } while (choice!=4);
    }

//DISPLAY FUNCTION
void display()
{
    struct node* temp=head;
    if(temp==NULL)
    {
        printf("LIST IS EMPTY");
    }
    while(temp!=NULL)
    {
        printf("%d-> \n",temp->data);
        printf("%s-> \n",temp->fname);
        printf("%s-> \n\n",temp->lname);
        temp=temp->after;
    }
}

//CREATE FUNCTION
void create(int item,char *surname,char *lastname)
{
    struct node *newNode=(struct node*) malloc(sizeof(struct node));
    struct node* temp;
    if(newNode==NULL)
    {
        printf("OVERFLOW");

    }
    else
    {
        newNode->data=item;
        strcpy(newNode->fname,surname);
        strcpy(newNode->lname,lastname);
        if(head==NULL)
        {
            newNode->before=NULL;
            newNode->after=NULL;
            head=newNode;
            printf("FIRST NODE INSERTED %d %s %s",item,surname,lastname);
    }

    else
    {
        temp=head;
        while(temp->after!=NULL)
        {
            temp=temp->after;
        }
        temp->after=newNode;
        newNode->before=temp;
        newNode->after=NULL;
        printf("\n Node inserted %d %s %s",item,surname,lastname);
    }
}
}

//DELETE FUNCTION
void del()
{
    struct node *pretemp,*temp;
    char *f,*l;
    int num;
    temp=head;
    pretemp=head->after;
    printf("Enter name and number :");
    scanf("%s",&f);
    scanf("%s",&l);
    scanf("%d",&num);
    
    while(temp!=NULL)
    {
        if((pretemp->fname==f)&&(pretemp->lname==l)&&(pretemp->data==num))
        {
            printf("%s ",temp->fname);
            printf("%s ",temp->lname);
            printf("%d ",temp->data);
            temp->after=pretemp->after;
            free(pretemp);
            break;
        }
        else
        {
            temp=temp->after;
            pretemp=pretemp->after;
        }
    }
}
//SEARCH FUNCTION
void search()
{
    struct node* temp;
    int item,i=0,flag;
    char name[50];
    temp=head;
    if(head==NULL)
    {
        printf("\n LIST IS EMPTY");

    }
    else
    {
        printf("ENTER THE NUMBER AND FIRST NAME TO BE SEARCHED: ");
        scanf("%d %s",&item,&name);
        while(temp!=NULL)
        {
            if(temp->data==item)
            {
                printf("ITEM FOUND AT LOCATION %d",i+1);
                flag=0;
                break;
            }
            else if(temp->fname==name)
            {
                printf("ITEM FOUND AT LOCATION %s",i+1);
                flag=0;
                break;
            }
            else
            {
                flag=1;
            }
            i++;
            temp=temp->after;

        }
        if(flag==1)
        {
            printf("\n ITEM NOT FOUND");
        }
    }

}


Any help would be appreciated.任何帮助,将不胜感激。 Thanks.谢谢。

To remove a node from a doubly linked list, you need to know the pointer of the node by searching the list.要从双向链表中删除一个节点,您需要通过搜索链表知道该节点的指针。

It is recommended that you link the first and last item of the doubly linked list together to form a ring, so that there are no boundary conditions (where node->before or node->after is NULL ).建议将双向链表的第一项和最后一项链接在一起形成一个环,这样就没有边界条件(其中node->beforenode->afterNULL )。

/* works in a circular doubly linked list, where the first item (*list)
   is linked with the last item of the list */
/* returns the list after the node is removed */
struct node* remove_node(struct node *list, struct node *node)
{
    assert(list != NULL);
    assert(node != NULL);
   
    /* node must be in a list to be removed */
    assert(node->before != NULL);
    assert(node->after != NULL);

    /* removing head */
    if (list == node)
    {
        /* move head to next node */
        list = list->after;

        /* still the same node (only one node in list) */
        if (list == node)
            list = NULL;
    }

    /* Linking the previous and next node together will 
       effectively remove the node from the list */
    node->before->after = node->after;
    node->after->before = node->before;

    /* this is not necessary but recommended for error checking */
    node->before = NULL;
    node->after = NULL;

    return list;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM