#include <string.h>
#include <stdlib.h>
typedef struct LinkedList {
char *s;
struct LinkedList *next;
} LinkedList;
void freell(LinkedList *list)
{
LinkedList *cur = list;
if(list == NULL) return;
freell(list->next);
free(list->s);
free(list);
}
LinkedList *createll(char *s)
{
LinkedList *lp;
lp = newll();
lp->s = mystrdup(s);
lp->next = (LinkedList *) 0;
return lp;
}
LinkedList *copyLinkedList(LinkedList *src)
{
LinkedList *ptr;
LinkedList *dest = NULL;
ptr = src;
if(src == NULL) { return NULL; }
else {
do {
dest = pushll(dest,ptr->s);
ptr = ptr->next;
} while(ptr != NULL);
}
return dest;
}
int searchll(LinkedList *list, char *s)
{
LinkedList *cur = list;
while(cur != NULL) {
if(!strcmp(cur->s, s)) return 1;
cur = cur->next;
}
return 0;
}
LinkedList *pushll(LinkedList *list,char *s)
{
char *str;
LinkedList *cur;
if(list == NULL) { list = createll(s); return list; }
else {
cur = list;
while(cur->next != NULL) { cur = cur->next; }
cur->next = createll(s);
return list;
}
}
LinkedList *popll(LinkedList *list)
{
char *str;
LinkedList *prev,*cur;
if(list == NULL) return NULL;
else if(list->next == NULL) {
free(list->s);
free(list);
return NULL;
}
else {
cur = list;
while(cur->next != NULL) { prev = cur;
cur = cur->next; }
free(prev->next->s);
free(prev->next);
prev->next = NULL;
return list;
}
}
char *ll_to_charp(LinkedList *list)
{
LinkedList *arg = list;
char *str;
int len = 0;
if(list == NULL) return NULL;
while(arg) {
len += (strlen(arg->s) + 1);
arg = getNextll(arg);
}
str = emalloc(sizeof(char) * len);
*str = 0;
arg = list;
while(arg != NULL) {
strcat(str, arg->s);
if(arg->next != NULL) strcat(str, " ");
arg = arg->next;
}
return str;
}
void printll(LinkedList *list)
{
LinkedList *arg = list;
while(arg != NULL) {
doutput("%s\n", arg->s);
arg = arg->next;
}
}
char **ll_to_charpp(LinkedList *list)
{
char **array;
int i = 0;
LinkedList *cur = list;
array = emalloc(sizeof(char *) * numEntriesll(list));
while(cur != NULL) {
array[i] = mystrdup(cur->s);
i++;
cur = cur->next;
}
array[i] = NULL;
return array;
}
int numEntriesll(LinkedList *list)
{
int n = 1;
LinkedList *arg = list;
if(arg == NULL) return 0;
for(; arg->next != NULL; arg=arg->next,n++);
return n;
}
LinkedList *getNextll(LinkedList *list)
{
if(list) return list->next;
return NULL;
}
char *getValuell(LinkedList *list)
{
if(list) return list->s;
return NULL;
}
LinkedList *newll(void)
{
LinkedList *ll;
ll = (LinkedList *) emalloc(sizeof(LinkedList));
ll->s = NULL;
ll->next = NULL;
return ll;
}