无法从链接列表中删除第一个节点

时间:2016-02-22 13:29:49

标签: c list pointers struct singly-linked-list

今天我有C考试,我无法从链表中删除第一个节点。在我的例子中,它删除了第一个元素,但是head仍然指向第一个节点'0'。我正坐在那里谷歌搜索解决方案,但找不到任何东西。教授已经提供了功能主管(struct t_node *delete_first(struct t_node *head))。

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

struct t_node {
     int number;
     struct t_node *next;
};

struct t_node * insert (struct t_node *head, int num){
  struct t_node * new_node = malloc(sizeof(struct t_node));
  new_node->number = num;
  new_node->next = head;

  return new_node;
}

void printlistvalues(struct t_node *head){
  while (head != NULL){
    printf("%d\n", head->number);
    head = head->next;
  }
}
struct t_node *delete_first(struct t_node *head){
struct t_node *help = head;

head = head->next;
free(help);


return head;
}

int main(){
 struct t_node *list = NULL;

 list = insert(list, 10);
 list = insert(list, 20);
 list = insert(list, 30);
 list = insert(list, 40);
 list = insert(list, 50);

 printlistvalues(list);
 printf("\n");
 delete_first(list);
 printlistvalues(list);

 return 0;
}

1 个答案:

答案 0 :(得分:2)

你必须写

def sum13(nums):
    if 13 not in nums:
        return sum(nums)

    ret = 0
    count_next = True
    for num in nums:
        if num == 13:
            count_next = False
        elif count_next:
            ret += num
        else:
            count_next = True
    return ret

此外,该功能本身应该是

list = delete_first(list);