Hi,
I'm doing an assignment at uni where I have to take a C string and convert it into a linked list of characters. I'm trying to do the function buildWord recursively but have run into a problem or two, mainly with where I can declare x = 0 I think.
My code is as follows:
Code:
#include "boole.h"
#include "listADT.h"
#include <stdio.h>
#include "wordADT.h"
wordADT createWord()
{
return createList();
}
wordADT buildWord (char str[])
{
if(str[x] != '\0')
return createWord();
else
return consList(str[x++], createWord());
}
void displayWord( wordADT word)
{
if(isEmptyList(word))
printf("\n");
else
printf("%c, ", headList(word)); displayWord(tailList(word));
}
int countChars( wordADT word )
{
if(isEmptyList(word))
return 0;
else
return 1+ countChars(tailList(word));
}
boolean wordLess (wordADT word1, wordADT word2)
{
if(headList(word1)>headList(word2))
return FALSE;
else if(headList(word1)==headList(word2))
return wordLess(tailList(word1), tailList(word2));
else
return TRUE;
}
boolean wordEqual ( wordADT word1, wordADT word2 )
{
if(headList(word1)!=headList(word2))
return FALSE;
else if (headList(word1)==headList(word2))
return wordEqual(tailList(word1), tailList(word2));
else
return TRUE;
}
The rest of it compiles but I've not tested the it so there might be some problems but I'll sort them out when I come to test it.
Also attached the header file for listADT.