I'm getting no errors or warnings but it's not working at all I have no idea where the issue is either
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//Structure for the linked list, the data which would be the string word and node next.
struct node {
char *word;
struct node *next;
};
typedef struct node datatype; //Making struct node under type "datatype"
char *rearrangeWord(char *str) //Function to scramble the word carbon
{
int i, a, b;
for(i=0;i<6;i++){
a = rand() % 6;
b = rand() % 6;
if(a==B)/>{
i--;
continue;
}
char temp = str[a];
str[a] = str[b];
str[b] = temp;
}
return str;
}
void printWords(datatype *root) //Function to print words, traverse list & print to screen
{
datatype *temp;
temp = root;
while (temp != NULL){
printf("%s\n", temp->word);
temp = temp->next;
}
}
void freeList(datatype *root) // Function to free list, traverse list and free memory
{
datatype *temp;
while(root != NULL){
temp = root;
root = temp->next;
free(temp);
}
}
int searchList(datatype *root, char *carb)//Function to search list for duplicates.
{
int FAIL, SUCCESS;
datatype *temp;
FAIL = 1;
SUCCESS = 3;
while (root != NULL){
temp = root;
root = temp->next;
if(strcmp(temp->word, carb) == 0){ //Returns 1 if there's a duplicate
return FAIL;
}
}
if (root == NULL){
return SUCCESS; //If root has reached NULL with no duplicates then it worked
} //and returns 3 to show that the search was successful
}
datatype *makeList(datatype *root, char *carb) //Function to make the list and uses
{ //the search list & word scramble functions
datatype *temp;
char *phrase;
temp = malloc(sizeof(struct node));
temp->next = root;
phrase = malloc(sizeof(char)*7);
temp->word = malloc(sizeof(char)*7);
strcpy(phrase, carb); //Carbon is passed into this function, and then into
// the scramble function
rearrangeWord(phrase);
if (searchList(root, phrase) == 3){ //So if search returns 3, then it SUPPOSED
//to add that word to the list
strcpy(temp->word, phrase);
return root;
}
else if (searchList(root, phrase) != 3){ //Recursion to start function over
//if there was a duplicate
makeList(root, phrase);
}
}
int main (void)
{
datatype *root;
char *str;
root = NULL;
str = malloc(sizeof(char)*7);
strcpy(str, "carbon");
int i;
for (i=0;i<720;i++){ //720 b/c there's supposed to be 6! iterations of carbon
makeList(root, str);
}
printWords(root);
freeList(root);
free(str);
return 0;
}

New Topic/Question
Reply



MultiQuote





|