I have 2 functions, CreateNewNode, a function that creates a new node, and Add_as_Last_Node, a function that adds the node created in CreateNewNode as the last node in the linked list. While running the program I had made using these two functions, the output was not I had expected.
For example, if I was the user, and I wanted to make a linked list of 5 nodes, and my inputs are "apple", "bag", "car", "dress", and "elephant", when I traverse and print the nodes the output is:
elephant elephant elephant elephant elephant
When it should be:
apple bag car dress elephant
After studying my codes for hours, I have deciphered that the problem of my program lies in either CreateNewNode, or Add_as_Last_Node. Or maybe both. I've tried tracing it, but with no luck of finding what's wrong with it. Can someone please help me in finding what wrong? Thank you! :)
typedef char Str30[31];
struct nodeTag {
char *data;
struct nodeTag *pNext;
};
void Traverse(struct nodeTag *pCurrent)
{
while(pCurrent){ // while there's a node
printf("%s\n", pCurrent->data);
pCurrent = pCurrent->pNext; // move to the next node
}
}
struct nodeTag *CreateNewNode(Str30 data)
{
struct nodeTag *pTemp;
//create and initialize a new node
pTemp = malloc(sizeof(struct nodeTag));
pTemp->data = data;
pTemp->pNext = NULL;
return pTemp;
}
struct nodeTag *Add_as_Last_Node(struct nodeTag *pFirst, struct nodeTag *pTemp)
{
struct nodeTag *pCurrent;
if(pFirst == NULL){ // list is empty
pFirst = pTemp;
}
else {
pCurrent = pFirst;
while(pCurrent->pNext != NULL)
pCurrent = pCurrent->pNext;
pCurrent->pNext = pTemp;
}
return pFirst;
}
int main()
{
Str30 data;
struct nodeTag *pFirst = NULL;
struct nodeTag *pTemp;
while(scanf("%s", data) != -1){
pTemp = CreateNewNode(data);
pFirst = Add_as_Last_Node(pFirst, pTemp);
}
Traverse(pFirst);
return 0;
}
This is the actual code I am using:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef char Str30[31];
struct nodeTag {
char *data;
struct nodeTag *pNext;
};
void Traverse(struct nodeTag *pCurrent)
{
// traverse the linked list
while(pCurrent){ // while there's a node
printf("%s\n", pCurrent->data);
pCurrent = pCurrent->pNext; // move to the next node
}
}
struct nodeTag *CreateNewNode(Str30 data)
{
struct nodeTag *pTemp;
//create and initialize a new node
pTemp = malloc(sizeof(struct nodeTag *));
//strcpy(pTemp->data, data);
pTemp->data = data;
pTemp->pNext = NULL;
return pTemp;
}
struct nodeTag *Add_as_Last_Node(struct nodeTag *pFirst,
struct nodeTag *pTemp)
{
struct nodeTag *pCurrent;
if(pFirst == NULL){ // list is empty
pFirst = pTemp;
}
else {
pCurrent = pFirst;
while(pCurrent->pNext != NULL)
pCurrent = pCurrent->pNext;
pCurrent->pNext = pTemp;
}
return pFirst;
}
struct nodeTag *Insert_Sort(struct nodeTag *pFirst,
struct nodeTag *pTemp)
{
struct nodeTag *pTrail;
struct nodeTag *pCurrent;
if(pFirst == NULL)
pFirst = pTemp;
else{
pTrail = NULL;
pCurrent = pFirst;
while(pCurrent->pNext != NULL){
pTrail = pCurrent;
pCurrent = pCurrent->pNext;
}
pTemp->pNext = pCurrent;
if(pTrail != NULL)
pTrail->pNext = pTemp;
else
pFirst = pTemp;
}
return pFirst;
}
int main()
{
int ctr = 0;
Str30 data;
// Str30 universe;
struct nodeTag *pFirst = NULL;
struct nodeTag *pTemp;
while(ctr != 3 && scanf("%s", data) != -1){
printf("\ndata = %s\n", data);
pTemp = CreateNewNode(data);
printf("\nAfter CreateNewNode: \n\n");
Traverse(pFirst);
pFirst = Add_as_Last_Node(pFirst, pTemp);
printf("\nAfter Add_as_Last_Node: \n\n");
Traverse(pFirst);
ctr++;
}
Traverse(pFirst);
printf("\n%d\n", ctr);
return 0;
}
Right now that is what I am using to test what is wrong with my codes.
UPDATE:
My codes are finally working. You guys were right about
strcpy(temp->data, data)
. The real reason why it didn't work was because of other parts in the code.Anyway, thanks for the help! I'll check everyone else in thanks. Danke! :D