Hello,
I am a student in a data structures programming class. Our current assignment is to create a linked list in C. I have written and rewritten the code many times and each time I recieve the same series of errors.
In my add function, that adds a node to the list, I allocate memory using malloc but in the end every value stored in the list contains the same value.
Ex:I enter, Red, Is, House (Insert at front of list, should print House is Red. My print statement says.
There are three list values: House House House (Instead of House is Red).
Another problem is after my print function, all the values of the list are empty. I don't assign anything so I'm not sure what is happening.
Any help would be appreciated. I've been beating my head against this for the past two days. I've arranged the code in a way that should be easier to read, the order is correct in my program though.
Code:
int main(void){
List *list = NULL;
listNode *newNode;
list = (List *)malloc( sizeof(List) );
newNode = (listNode *)malloc( sizeof(listNode) );
listInitialize(list);
testAdd(list);
printf("Values are %s , %s , %s \n",list->head->data.str,list->head->next->data.str,list->head->next->next->data.str);
print(list);
printf("Values are %s , %s , %s \n",list->head->data.str,list->head->next->data.str,list->head->next->next->data.str);
}
typedef struct {
//int len;
char *str;
} String;
typedef struct listNode {
String data;
struct listNode *next;
} listNode;
typedef struct list {
listNode *head;
int length;
} List;
void add(List *list, String Item){
int i = 0;
listNode *NewNode;
NewNode = (listNode *)malloc( sizeof(listNode) );
NewNode->data.str = (char *)malloc( sizeof(char[50]));
char newPrompt[50] = "\nPlease Enter Value: ";
promptUser(newPrompt);
Item.str = newPrompt;
NewNode->data = Item;
insertFirst(list, NewNode);
printf("Value in add is: %s\n",list->head->data.str);
}
void print(List *list) {
listNode *temp = list->head;
//printf("There are %d numbers in the list!\n", list->length);
while( temp ) {
printf("%s ", temp->data.str);
temp = temp->next;
}
printf("\n");
}
void testAdd(List *list){
String temp1,temp2,temp3;
temp1.str = (char *)malloc( sizeof(char) * 50);
temp2.str = (char *)malloc( sizeof(char) * 50);
temp3.str = (char *)malloc( sizeof(char) * 50);
add(list, temp1);
printf("Value in main is %s\n",list->head->data.str);
add(list, temp2);
printf("Value in main of head is %s\n",list->head->data.str);
printf("Value in main of head->next is %s\n",list->head->next->data.str);
add(list, temp3);
printf("Value in main of head is %s\n",list->head->data.str);
printf("Value in main of head->next is %s\n",list->head->next->data.str);
}
I'm sorry there is so much code, I'm so lost as to what is causing the problem. The output from running this program would present an user input, then state what it is inside of the add function, then the value in the main function. It would then proceed to do the same twice more, but this time also show the value of the second node(which is always the same as the first )
listInitialize sets head to null. And String is defined that way because my teacher is making us do it that way. Thank you soo much for your time anyone who can help!
The following isn't code, it is a sample output from a test I ran.
Code:
Please Enter Value: : One // This is what I entered: One
Value in add is: One
Value in main is One
Please Enter Value: : Two // This is what I entered: Two
Value in add is: Two
Value in main of head is Two
Value in main of head->next is Two
Please Enter Value: : Three // This is what I entered: Three
Value in add is: Three
Value in main of head is Three
Value in main of head->next is Three
Values are Three , Three , Three
Three Three Three
Values are , , // The values here are all blank and I'm speachless
Thanks again!