【问题标题】:Delete function in doubly linked list双向链表中的删除函数
【发布时间】:2021-04-01 03:59:49
【问题描述】:

我是 C 的新手,正在尝试使用双向链表创建电话簿应用程序。但是,我无法弄清楚如何删除联系人,即人的名字、姓氏和号码,并将前一个节点链接到下一个节点。我附上了下面的代码。

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

}


任何帮助将不胜感激。谢谢。

【问题讨论】:

  • 提示 #1:pretemp-&gt;fname==f 不是您在 C 中比较字符串的方式。请参阅 strcmp
  • char *f,*l; 在调用printf("Enter name and number :"); scanf("%s",&amp;f); scanf("%s",&amp;l); 时不能用作缓冲区 建议以常规char 数组的形式使用有意义的变量名:例如:char first[80] = {0}; char last[80] = {0};
  • OT: about: void search(); void display(); void del(); 声明前向函数声明时,(原型)不带任何参数,括号之间需要有NULL,否则编译器将生成可以接受的代码任何数量/类型的参数,“可能”有效,但编程习惯非常差。

标签: c data-structures linked-list doubly-linked-list


【解决方案1】:

要从双向链表中删除一个节点,你需要通过搜索链表知道该节点的指针。

建议将双向链表的第一项和最后一项链接在一起形成一个环,这样就没有边界条件(其中node-&gt;beforenode-&gt;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;
}

【讨论】:

    猜你喜欢
    • 2016-06-11
    • 1970-01-01
    • 1970-01-01
    • 2020-12-18
    • 2014-03-04
    • 2016-07-06
    • 2021-11-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多