I am currently trying to program a function that will cons a new element onto the top of the list, and push the rest of the list back... can anyone help me with this? My program does not work when I try to compile and run it. It goes on an infinite loop. Any help?
#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;
}