检查链表是否按递增顺序排列,是的话返回1,不是返回0
能不能帮我找找为什么会这样
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
struct node {
struct node *next;
int data;
};
int increasing(struct node *head);
struct node *strings_to_list(int len, char *strings[]);
//主程序
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 = increasing(head);
printf("%d\n", result);
return 0;
}
//这里有问题
int increasing(struct node *head) {
while (head->next != NULL) {
int v = head->data;
if (v > head->next->data) {
return 0;
head = head->next;
}
}
return 1;
}
//
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;
}