簡體   English   中英

無法從鏈表中刪除最低值

[英]Unable to remove lowest value from linked-list

我在 C 中創建了一個鏈表。我正在嘗試創建一個函數,該函數查看鏈表中的最低值(即頭部)並刪除列表中該值的“最右側”實例。

假設鏈表如下所示:

2 -> 2 -> 2 -> 4 -> 5 -> 6

此列表中的頭部是 2。但這不是我要刪除的頭部。 我想刪除 4 之前的 2(它是頭部最右側的實例)。

這是我為實現這一點而創建的函數:

double removeLowestValue() {
  struct node *temp;
  struct node *ptr = head;
  double val = ptr->value;
  if(head == tail)
  {
      free(head);
      head = NULL;
      tail = NULL;
  }
  else
  {
      while(ptr->value == ptr->next->value)
      {
          temp = ptr;
          ptr = ptr->next;
          val = ptr->value
      }
      temp->next = NULL;
      temp->next = ptr->next;
      free(ptr);

      return val;
  }
}

然后我嘗試測試該功能是否有效:

int main() {
  insertNode(18.0);
  insertNode(13.0);
  insertNode(11.0);
  insertNode(11.0);
  insertNode(22.0);

  printf("%d", removeLowestValue());

  return 0;
}

不幸的是,程序沒有按預期打印出“11”。 事實上,它根本不打印任何東西。 這里發生了什么?

編輯:

下面是我如何實現 insertNode 函數:

void insertNode(double value) {
  struct node *new_node = create_new_node(value);
  struct node *temp = head;
  struct node *prev;

  if (head == NULL) {
    head = new_node;
    tail = new_node;
  } else {
    while (value > temp->value && temp->next != NULL) {
      prev = temp;
      temp = temp->next;
    }

    if(value < temp->value || value == temp->value)
    {
        /*If the value of the new node equals to the value of temp
         OR if the value of the new node is less than the value of temp,
         then insert the new node right before temp*/

        new_node->next = temp;
        prev->next = new_node;
    }
    else if(value > temp->value)
    {
        temp->next = new_node;
        tail = new_node;
    }
  }
}

您的功能已更正,當然假設列表已排序:

double removeLowestValue() {
  if (head == NULL)
    return 0; /* ???? */
  else {
    node * ptr = head;
    node * previous = 0; /* the cell before the cell to remove */

    while ((ptr->next != NULL) && (ptr->value == ptr->next->value)) {
      previous = ptr;
      ptr = ptr->next;
    }

    /* ptr is now the cell to remove */

    double val = ptr->value;

    if (ptr == head) {
      /* remove the first cell */
      ptr = head->next;
      free(head);
      head = ptr;
      if (head == NULL)
        /* the list is empty */
        tail = NULL;
    }
    else if (ptr->next == NULL) {
      /* all the values are the same in the list
         ptr is the last cell */
      free(ptr);
      /* previous is now the last cell */
      previous->next = NULL;
      tail = previous;
    }
    else {
      /* ptr is not the first nor the last cell */
      previous->next = ptr->next;
      free(ptr);
    }

    return val;
  }
}

關於insertNode

  • 最好將變量tempprev的聲明移動到它們有用的地方,如果head null 所以在定義的頂部它們是無用的

  • (value < temp->value || value == temp->value)可以只是(value <= temp->value)else if (...) after 可以只是一個else

  • (value <= temp->value) prev仍然可以未設置但用於prev->next = new_node ,當您在main 中插入 18 之后的 13 時附加。 (temp == head)您必須使用new_node更新head設置時

所以更正后的版本可以是:

void insertNode(double value) {
  struct node *new_node = create_new_node(value);

  if (head == NULL) {
    head = new_node;
    tail = new_node;
  } else {
    struct node *temp = head;
    struct node *prev = 0; /* = 0 to be sure to have a crash if something wrong */

    while ((value > temp->value) && (temp->next != NULL)) {
      prev = temp;
      temp = temp->next;
    }

    if (value <= temp->value)
    {
        /* insert the new node right before temp*/
        new_node->next = temp;
        if (temp == head)
          head = new_node;
        else
          /* prev was set */
          prev->next = new_node;
    } else {
        /* insert the new node at end */
        temp->next = new_node;
        tail = new_node;
    }
  }
}

使用附加定義

typedef struct node {
  double value;
  struct node * next;
} node;

node * create_new_node(double value)
{
  node * r = (node *) malloc(sizeof(node));

  r->value = value;
  r->next = 0;

  return r;
}

int main() {
  insertNode(18.0);
  insertNode(13.0);
  insertNode(11.0);
  insertNode(11.0);
  insertNode(22.0);

  printf("%g\n", removeLowestValue());
  printf("%g\n", removeLowestValue());
  printf("%g\n", removeLowestValue());
  printf("%g\n", removeLowestValue());
  printf("%g\n", removeLowestValue());
  printf("%g\n", removeLowestValue());

  return 0;
}

執行寫入(最后一個 0 表示列表為空)

11
11
13
18
22
0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM