C语言,查找链表的长度,补全一下我的这个代码

C语言,查找链表的长度,补全一下我的这个代码

问题描述:

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

struct node {
struct node *next;
int data;
};

int length(struct node *head);
struct node *strings_to_list(int len, char *strings[]);

// DO NOT CHANGE THIS MAIN FUNCTION

int main(int argc, char *argv[]) {
// create linked list from command line arguments
struct node *head = strings_to_list(argc - 1, &argv[1]);

int result = length(head);
printf("%d\n", result);

return 0;

}

// Return the length of the linked list pointed by head
int length(struct node *head) {

// PUT YOUR CODE HERE (change the next line!)
return 42;

}

// DO NOT CHANGE THIS FUNCTION

// create linked list from array of strings
struct node *strings_to_list(int len, char *strings[]) {
struct node *head = NULL;
for (int i = len - 1; i >= 0; i = i - 1) {
struct node *n = malloc(sizeof (struct node));
assert(n != NULL);
n->next = head;
n->data = atoi(strings[i]);
head = n;
}
return head;
}

int length(struct node *head) {
  struct node *p = head;
  int count  = 0;
while(p != NULL)
{
    count++;
    p = p->next;
}
  return count;

}