Deleting every odd positioned node in a linked list in C

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-12 18:03:29

问题


I was trying to create a function in C that deletes every odd positioned node. For example 1,2,3,4 becomes 2,4.

Here is what I tried but it doesn't seem to be working. Im talking about the deletee function. I modified it but the list doesn't seem to be changing.

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

typedef struct node {
    int val;
    struct node *next;
} node;

typedef struct ll {
    node *head;
} ll;

ll *newll() {
    ll *k = malloc(sizeof(ll));
    k->head = NULL;
    return k;
}

void insert(ll *l, int vl) {
    node *tmp = malloc(sizeof(node));
    tmp->next = NULL;
    tmp->val = vl;
    if (l->head == NULL) {
        l->head = tmp;
        return;
    }
    node *s = l->head;
    while (s->next != NULL)
        s = s->next;
    s->next = tmp;
}

void printll(ll *l) {
    node *s = l->head;
    while (s != NULL) {
        printf("%d ", s->val);
        s = s->next;
    }
}

void deletee(ll *l) {
    node *k = l->head;
    while (k != NULL && k->next != NULL) {
        node *tmp = k->next->next;
        k = tmp;
    }
}

int main() {
    ll *ll1 = newll();
    insert(ll1, 5);
    insert(ll1, 6);
    insert(ll1, 8);
    insert(ll1, 9);
    insert(ll1, 10);
    insert(ll1, 11);
    deletee(ll1);
    printll(ll1);
    return 0;
}

回答1:


We'll need to update both ll.head and node.next, so a pointer to node isn't good enough unless you want to special case the head. Instead, let's use a pointer to the pointer we want to update.

void delete_node(node** node_ptr_ptr) {
   node* to_delete = *node_ptr_ptr;
   *node_ptr_ptr = to_delete->next;
   free(to_delete);
}

void delete_every_second(ll* l) {
   node** node_ptr_ptr = &( l->head );
   while (1) {
      if (*node_ptr_ptr == NULL) break;
      delete_node(node_ptr_ptr);
      if (*node_ptr_ptr == NULL) break;
      node_ptr_ptr = &( (*node_ptr_ptr)->next );
   }
}

Say you start with the following:

+------+      +------+      +------+      +------+
| head ------>| val  |  +-->| val  |  +-->| val  |
+------+      +------+  |   +------+  |   +------+
              | next ---+   | next ---+   | next --->NULL
              +------+      +------+      +------+

After node** node_ptr_ptr = &( l->head );:

+------+      +------+      +------+      +------+
| head ------>| val1 |  +-->| val2 |  +-->| val3 |
+------+      +------+  |   +------+  |   +------+
    ^         | next ---+   | next ---+   | next --->NULL
    |         +------+      +------+      +------+
    |
    +-----+
          |
+------+  |
| ptr ----+
+------+

After node* to_delete = *node_ptr_ptr;:

              +------+
              | del ----+
              +------+  |
                        |
                 +------+
                 |
                 v
+------+      +------+      +------+      +------+
| head ------>| val1 |  +-->| val2 |  +-->| val3 |
+------+      +------+  |   +------+  |   +------+
    ^         | next ---+   | next ---+   | next --->NULL
    |         +------+      +------+      +------+
    |
    +-----+
          |
+------+  |
| ptr ----+
+------+

After *node_ptr_ptr = to_delete->next; free(to_delete);:

+------+                    +------+      +------+
| head -------------------->| val2 |  +-->| val3 |
+------+                    +------+  |   +------+
    ^                       | next ---+   | next --->NULL
    |                       +------+      +------+
    |      
    +-----+
          |
+------+  |
| ptr ----+
+------+

After node_ptr_ptr = &( (*node_ptr_ptr)->next );:

+------+                    +------+      +------+
| head -------------------->| val2 |  +-->| val3 |
+------+                    +------+  |   +------+
          +---------------->| next ---+   | next --->NULL
          |                 +------+      +------+
          |
+------+  |
| ptr ----+
+------+



回答2:


in this code of yours:

while(k!=NULL)
{
    if(k->next!=NULL && k->next->next!=NULL)
    k->next=k->next->next;
}

you have an infinite loop there since you don't change the value of k in the loop.

Also: you'd have to delete/free memory for k->next first or you'll get a memory leak.

I'd rewrite it simply as follows:

void deletee(ll *l)
{
  if (l->head == NULL)
    return;

  node* tmp = l->head;
  l->head = l->head->next; // skip first item
  free(tmp);
  node* k=l->head;
  while(k!=NULL && k->next!=NULL)
  {
    tmp = k->next;
    k->next = k->next->next;
    free(tmp);
    k = k->next;
  }
}

result (as expected):

6 9 11
  • tmp stores the next value for future deletion
  • we set next element to the next element of the to-be-deleted element so the latter is unlinked
  • we free tmp
  • then we skip to the new next element and continue



回答3:


You changed the code for your deletee function, but it is still incorrect:

void deletee(ll *l) {
    node *k = l->head;
    while (k != NULL && k->next != NULL) {
        node *tmp = k->next->next;
        k = tmp;
    }
}

You do not modify the list at all.

Here is a solution with a pointer to pointer to avoid making special cases of the empty list and the first node in the list. k points to the link that must be updated when removing a node, it is pushed past the preserved node after each removal except at the end of the list:

void deletee(ll *l) {
    node **k = &l->head;
    while (*k != NULL) {
        /* delete the node */
        node *tmp = *k;
        *k = (*k)->next;
        free(tmp);
        if (*k != NULL) {
            /* skip the preserved node */
            k = &(*k)->next;
        }
    }
}


来源:https://stackoverflow.com/questions/42212813/deleting-every-odd-positioned-node-in-a-linked-list-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!