繁体   English   中英

如何删除双向链表中的节点

[英]How to delete nodes in a doubly linked list

我在删除双向链表中的节点时遇到了麻烦,程序崩溃了,我无法解决问题。 你能帮我么? 这是创建新节点,查看它们并删除它们的完整代码。

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

struct Test
{
  int id;
};

typedef struct Node {
  struct Test structure;
  struct Node * next;
  struct Node *prev;

}TNode;
typedef TNode* Node;

void NewNode(struct Test  p, Node *pp)
{
  Node temp;

  temp = (Node)malloc(sizeof(struct Node));

  temp->structure = p;
  temp->next = *pp;
  temp->prev = NULL;

  if(*pp != NULL)
  {
    (*pp)->prev = temp;
  }

  *pp = temp;

}

void ReadStructure(struct Test * p)
{
  printf("\nID:");
  scanf(" %d", &p->id);
}

void ViewList(Node node)
{
  Node temp;
  while(node != NULL)
  {
    temp = node->prev;
    if(node->prev == NULL)
      {
        printf("Prev = NULL\n");
      }
    else
    {
    printf("Prev: %d\n", temp->structure.id);
    }
    printf("Curr: %d\n", node->structure.id);
    node = node->next;
  }
}

void Delete(Node * head, Node del)
{

       if(*head == NULL || del == NULL)
        {
          return;
        }
       if(*head == del)
       {
         *head = del->next;
       }
       if(del->next != NULL)
       {
         del->next->prev = del->prev;
       }
       if(del->prev != NULL)
       {
         del->prev->next = del->next;
       }
       free(del);
       return;

}

int Menu()
{
  int c;

  printf("*** M E N U ***\n"
     "1 - New Node\n"
     "2 - View List\n"
   "3 - Delete\n"
   "0 - Exit\n"
  "\n>> ");
  scanf(" %d", &c);

  return c;
}

int main()
{
  int c;
  struct Test test;
  Node list = NULL;
  Node del = NULL;
  do {
    c = Menu();

    switch (c)
    {
      case 1: ReadStructure(&test);
              NewNode(test, &list); break;
      case 2: ViewList(list); break;
      case 3: printf("\nElement to Delete: ");
              scanf("%d", &del->structure.id);
              Delete(&list, del); break;
      default: c = 0;
    }

  } while (c != 0);

  return 0;
}

我认为问题与Node del的scanf()有关,但我不确定。 当我只是将listlist->next作为第二个参数传递给函数Delete()时,它将起作用。 代码一切正常吗?

int main()
{
  ...
  Node del = NULL;
  ...
  scanf("%d", &del->structure.id);

您的程序应在此处崩溃。 您正在取消引用空指针。

可能您需要将用户输入读入一个临时id变量,然后在列表中搜索匹配的项目,如果找到一个匹配的项目,则可以尝试将其删除。

好的,我添加了一个搜索要删除的节点的函数,并修改了Delete()函数,这是解决方法,谢谢您的建议:

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

struct Test
{
  int id;
};

typedef struct Node {
  struct Test structure;
  struct Node * next;
  struct Node *prev;

}TNode;
typedef TNode* Node;

void NewNode(struct Test  p, Node *pp)
{
  Node temp;

  temp = (Node)malloc(sizeof(struct Node));

  temp->structure = p;
  temp->next = *pp;
  temp->prev = NULL;

  if(*pp != NULL)
  {
    (*pp)->prev = temp;
  }

  *pp = temp;

}

void ReadStructure(struct Test * p)
{
  printf("\nID:");
  scanf(" %d", &p->id);
}

void ViewList(Node node)
{
  Node temp;
  while(node != NULL)
  {
    temp = node->prev;
    if(node->prev == NULL)
      {
        printf("Prev = NULL\n");
      }
    else
    {
    printf("Prev: %d\n", temp->structure.id);
    }
    printf("Curr: %d\n", node->structure.id);
    node = node->next;
  }
}

Node SearchNode(Node head)
{
  int d;
  printf("\nElement to Delete:");
  scanf("%d", &d);

  while(head != NULL)
    {
      if(head->structure.id == d)
        {
          return head;
        }
      head = head->next;
    }
  printf("\nNo Element [%d] Found", d);
  return NULL;
}

void Delete(Node * head, struct Test temp)
{
  Node del = SearchNode(*head);

       if(*head == NULL || del == NULL)
        {
          return;
        }
       if(*head == del)
       {
         *head = del->next;
       }
       if(del->next != NULL)
       {
         del->next->prev = del->prev;
       }
       if(del->prev != NULL)
       {
         del->prev->next = del->next;
       }
       free(del);
       return;

}

int Menu()
{
  int c;

  printf("\n*** M E N U ***\n"
     "1 - New Node\n"
     "2 - View List\n"
   "3 - Delete\n"
   "0 - Exit\n"
  "\n>> ");
  scanf(" %d", &c);

  return c;
}

int main()
{
  int c;
  struct Test test, del;
  Node list = NULL;

  do {
    c = Menu();

    switch (c)
    {
      case 1: ReadStructure(&test);
              NewNode(test, &list); break;
      case 2: ViewList(list); break;
      case 3: Delete(&list, del); break;
      default: c = 0;
    }

  } while (c != 0);

  return 0;
}

del值为NULL,但是在删除时会引用它。 您需要在列表中的一个节点上搜索给定ID,然后将其删除。

暂无
暂无

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

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