我目前正在尝试编写一个函数,该函数将在列表顶部添加一个新元素,并将列表的其余部分推回......有人可以帮我吗?当我尝试编译和运行它时,我的程序不起作用。它进入一个无限循环。有什么帮助吗?
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
/* linked lists of strings */
typedef struct sll sll;
struct sll {
char *s;
sll *next;
};
/* By convention, the empty list is NULL. */
/* sll_cons : (char*, sll*) -> sll* */
/* build new list with given string at the head */
/* note: copy the given string to the list (deep copy) */
sll *sll_cons(char *s, sll *ss) {
while (ss != NULL) {
char* temp;
temp = malloc(sizeof(char)*strlen(ss->s));
temp = ss->s;
ss->s = s;
ss->next = malloc(sizeof(char)*strlen(ss->s));
ss->next->s = temp;
ss->next->next = NULL;
ss = ss->next;
}
return ss;
}