need help with linked structs at C language
-
i recently studied about this subject and i tried to code a program that does some stuff connected to it but i ran into troubles while tryign to add more structs with numbers i cutted mose of my program (to your convinient) please help me to udnerstand my mistakes http://pastebin.com/nyj3wtXT[^]
-
Please add some proper detail to your question and include your code here rather than offsite.
well i first created the code to work on the main but somehow somethign went wrong when i tried putting the codes in functions < every time i go to the function insertAtEnd but at the second loop of the while it just says error #include #include #include struct numberNode { int number; struct numberNode* Next; }; typedef struct numberNode numStruct; numStruct* createSong(int numbers) { numStruct* newSong = (numStruct*)malloc(sizeof(numStruct)); if (newSong) { newSong->number = numbers; newSong->Next = NULL; } return newSong; } void insertAtEnd(numStruct** firstNode, numStruct* newNode) { numStruct* currNode = *firstNode; // if the linked list is empty // should put the new node as the first if (!currNode) { *firstNode = newNode; newNode->Next = NULL; } else { while (currNode->Next) // problem at the second loop { currNode = currNode->Next; } currNode->Next = newNode; newNode->Next = NULL; } printf("\n\n"); } numStruct* AddSongs(numStruct* anchorNode, numStruct* newNode) { int count = 0; int numbers; printf("\nPlease enter numbers and at the end enter -999\n"); do { count++; _flushall(); numbers = 0; printf("\n\n%d.\n\nnumber: ", count); scanf("%d",&numbers); if (numbers != -999) { newNode = createSong(numbers); insertAtEnd(&anchorNode, newNode); } } while (numbers != -999); printf("\n\n"); } int main(void) { numStruct* anchorNode = NULL; numStruct* newNode; AddSongs(&anchorNode, &newNode); system("PAUSE"); }
-
well i first created the code to work on the main but somehow somethign went wrong when i tried putting the codes in functions < every time i go to the function insertAtEnd but at the second loop of the while it just says error #include #include #include struct numberNode { int number; struct numberNode* Next; }; typedef struct numberNode numStruct; numStruct* createSong(int numbers) { numStruct* newSong = (numStruct*)malloc(sizeof(numStruct)); if (newSong) { newSong->number = numbers; newSong->Next = NULL; } return newSong; } void insertAtEnd(numStruct** firstNode, numStruct* newNode) { numStruct* currNode = *firstNode; // if the linked list is empty // should put the new node as the first if (!currNode) { *firstNode = newNode; newNode->Next = NULL; } else { while (currNode->Next) // problem at the second loop { currNode = currNode->Next; } currNode->Next = newNode; newNode->Next = NULL; } printf("\n\n"); } numStruct* AddSongs(numStruct* anchorNode, numStruct* newNode) { int count = 0; int numbers; printf("\nPlease enter numbers and at the end enter -999\n"); do { count++; _flushall(); numbers = 0; printf("\n\n%d.\n\nnumber: ", count); scanf("%d",&numbers); if (numbers != -999) { newNode = createSong(numbers); insertAtEnd(&anchorNode, newNode); } } while (numbers != -999); printf("\n\n"); } int main(void) { numStruct* anchorNode = NULL; numStruct* newNode; AddSongs(&anchorNode, &newNode); system("PAUSE"); }
Your code is almost correct, just a couple of errors in the main function. The variable newNode have to be initialized to NULL to avoid errors and compiler complainings. anchorNode and NewNode are already pointer to structures of type numStruct, so you have to pass them directly to AddSongs(), but you're passing their address (even if this sounds strange to me because you should have got a compiler error). The correct code is this:
int main(void)
{
numStruct* anchorNode = NULL;
numStruct* newNode = NULL;
AddSongs(anchorNode, newNode);system("PAUSE");
}
-
Your code is almost correct, just a couple of errors in the main function. The variable newNode have to be initialized to NULL to avoid errors and compiler complainings. anchorNode and NewNode are already pointer to structures of type numStruct, so you have to pass them directly to AddSongs(), but you're passing their address (even if this sounds strange to me because you should have got a compiler error). The correct code is this:
int main(void)
{
numStruct* anchorNode = NULL;
numStruct* newNode = NULL;
AddSongs(anchorNode, newNode);system("PAUSE");
}
ok then if thats the case why does this next function doesnt work after making the linked list void printList(numStruct* firstNode) { numStruct* currSong = firstNode; printf("\n\n-------------------------------------\n"); while (currSong) { printf("number= %d \n", currSong->number); currSong = currSong->Next; } printf("-------------------------------------\n\n"); printf("\n\n"); } which in main it will look like this int main(void) { numStruct* anchorNode = NULL; numStruct* newNode = NULL; AddSongs(anchorNode, newNode); printList(anchorNode); system("PAUSE"); }
-
ok then if thats the case why does this next function doesnt work after making the linked list void printList(numStruct* firstNode) { numStruct* currSong = firstNode; printf("\n\n-------------------------------------\n"); while (currSong) { printf("number= %d \n", currSong->number); currSong = currSong->Next; } printf("-------------------------------------\n\n"); printf("\n\n"); } which in main it will look like this int main(void) { numStruct* anchorNode = NULL; numStruct* newNode = NULL; AddSongs(anchorNode, newNode); printList(anchorNode); system("PAUSE"); }
Because just the first variable anchorNode must be passed as pointer to struct pointer to have it assigned. Also AddSongs must pass it directly to insertAtEnd(). I report the whole program here:
#include #include #include struct numberNode {
int number;
struct numberNode *Next;
};typedef struct numberNode numStruct;
numStruct *createSong(int numbers)
{
numStruct *newSong = (numStruct *)malloc(sizeof(numStruct));
if (newSong)
{
newSong->number = numbers;
newSong->Next = NULL;
}
return newSong;
}void insertAtEnd(numStruct **firstNode, numStruct *newNode)
{
numStruct *currNode = *firstNode;// if the linked list is empty // should put the new node as the first if (!currNode) { \*firstNode = newNode; newNode->Next = NULL; } else { while (currNode->Next) // problem at the second loop { currNode = currNode->Next; } currNode->Next = newNode; newNode->Next = NULL; } printf("\\n\\n");
}
numStruct *AddSongs(numStruct **anchorNode, numStruct *newNode)
{
int count = 0;
int numbers;printf("\\nPlease enter numbers and at the end enter -999\\n"); do { count++; //flushall(); fflush(stdin); numbers = 0; printf("\\n\\n%d.\\n\\nnumber: ", count); scanf("%d", &numbers); if (numbers != -999) { newNode = createSong(numbers); insertAtEnd(anchorNode, newNode); } } while (numbers != -999); printf("\\n\\n"); return NULL;
}
void printList(numStruct *firstNode)
{
numStruct *currSong = firstNode;
printf("\n\n-------------------------------------\n");
while (currSong)
{
printf("number= %d \n", currSong->number);
currSong = currSong->Next;
}
printf("-------------------------------------\n\n");
printf("\n\n");
}int main(void)
{
numStruct *anchorNode = NULL;
numStruct *newNode = NULL;
AddSongs(&anchorNode, newNode);
printList(anchorNode);system("PAUSE");
}
Check it against your code. We pass a pointer to a variable when we want access the variable from the called function, in your case to assign to anchorNode the beginning of the memory list.
-
Because just the first variable anchorNode must be passed as pointer to struct pointer to have it assigned. Also AddSongs must pass it directly to insertAtEnd(). I report the whole program here:
#include #include #include struct numberNode {
int number;
struct numberNode *Next;
};typedef struct numberNode numStruct;
numStruct *createSong(int numbers)
{
numStruct *newSong = (numStruct *)malloc(sizeof(numStruct));
if (newSong)
{
newSong->number = numbers;
newSong->Next = NULL;
}
return newSong;
}void insertAtEnd(numStruct **firstNode, numStruct *newNode)
{
numStruct *currNode = *firstNode;// if the linked list is empty // should put the new node as the first if (!currNode) { \*firstNode = newNode; newNode->Next = NULL; } else { while (currNode->Next) // problem at the second loop { currNode = currNode->Next; } currNode->Next = newNode; newNode->Next = NULL; } printf("\\n\\n");
}
numStruct *AddSongs(numStruct **anchorNode, numStruct *newNode)
{
int count = 0;
int numbers;printf("\\nPlease enter numbers and at the end enter -999\\n"); do { count++; //flushall(); fflush(stdin); numbers = 0; printf("\\n\\n%d.\\n\\nnumber: ", count); scanf("%d", &numbers); if (numbers != -999) { newNode = createSong(numbers); insertAtEnd(anchorNode, newNode); } } while (numbers != -999); printf("\\n\\n"); return NULL;
}
void printList(numStruct *firstNode)
{
numStruct *currSong = firstNode;
printf("\n\n-------------------------------------\n");
while (currSong)
{
printf("number= %d \n", currSong->number);
currSong = currSong->Next;
}
printf("-------------------------------------\n\n");
printf("\n\n");
}int main(void)
{
numStruct *anchorNode = NULL;
numStruct *newNode = NULL;
AddSongs(&anchorNode, newNode);
printList(anchorNode);system("PAUSE");
}
Check it against your code. We pass a pointer to a variable when we want access the variable from the called function, in your case to assign to anchorNode the beginning of the memory list.
that worked perfectly thank you very much
-
Because just the first variable anchorNode must be passed as pointer to struct pointer to have it assigned. Also AddSongs must pass it directly to insertAtEnd(). I report the whole program here:
#include #include #include struct numberNode {
int number;
struct numberNode *Next;
};typedef struct numberNode numStruct;
numStruct *createSong(int numbers)
{
numStruct *newSong = (numStruct *)malloc(sizeof(numStruct));
if (newSong)
{
newSong->number = numbers;
newSong->Next = NULL;
}
return newSong;
}void insertAtEnd(numStruct **firstNode, numStruct *newNode)
{
numStruct *currNode = *firstNode;// if the linked list is empty // should put the new node as the first if (!currNode) { \*firstNode = newNode; newNode->Next = NULL; } else { while (currNode->Next) // problem at the second loop { currNode = currNode->Next; } currNode->Next = newNode; newNode->Next = NULL; } printf("\\n\\n");
}
numStruct *AddSongs(numStruct **anchorNode, numStruct *newNode)
{
int count = 0;
int numbers;printf("\\nPlease enter numbers and at the end enter -999\\n"); do { count++; //flushall(); fflush(stdin); numbers = 0; printf("\\n\\n%d.\\n\\nnumber: ", count); scanf("%d", &numbers); if (numbers != -999) { newNode = createSong(numbers); insertAtEnd(anchorNode, newNode); } } while (numbers != -999); printf("\\n\\n"); return NULL;
}
void printList(numStruct *firstNode)
{
numStruct *currSong = firstNode;
printf("\n\n-------------------------------------\n");
while (currSong)
{
printf("number= %d \n", currSong->number);
currSong = currSong->Next;
}
printf("-------------------------------------\n\n");
printf("\n\n");
}int main(void)
{
numStruct *anchorNode = NULL;
numStruct *newNode = NULL;
AddSongs(&anchorNode, newNode);
printList(anchorNode);system("PAUSE");
}
Check it against your code. We pass a pointer to a variable when we want access the variable from the called function, in your case to assign to anchorNode the beginning of the memory list.
there is one more thing that bothers me 1 more problem im having in my full program is to "delete" a part of the linked list and reconnect it to the others by an input i tried to do this next thing: void deleteNode(numStruct** firstNode, numStruct* nodeToDelete) { numStruct* currNode = *firstNode; numStruct* temp; // if the list is not empty if (*firstNode) { // the first node should be deleted if (currNode == nodeToDelete) { *firstNode = (*firstNode)->Next; free(currNode); } else { while (nodeToDelete != currNode->Next && currNode->Next) { currNode = currNode->Next; } if (nodeToDelete == currNode->Next && nodeToDelete) { temp = currNode->Next; currNode->Next = temp->Next; free(temp); } } } inside the main: numStruct* t = anchorNode; int arr[50] = { 261363 }, spot = 0; printf("Please enter all the numbers you want to delete and end it with - 999 \n"); while ((arr[spot] != -999) && (spot != 50)) { scanf("%d", &arr[spot]); if (arr[spot] != -999) { spot++; } } if (arr[spot] == -999) { arr[spot] = 261363; spot--; } for (int i = 0; i < spot; i++) { int flag = 0; while ((t->number != arr[i]) && (t->number != NULL)) { t = t->Next; if (t->number == arr[i]) { flag = 1; } } if (flag == 1) { deleteNode(anchorNode, t); t = anchorNode; } } the problem is that it doesnt really delete it when i go beck to the menu and do the print function again it appears the same
-
there is one more thing that bothers me 1 more problem im having in my full program is to "delete" a part of the linked list and reconnect it to the others by an input i tried to do this next thing: void deleteNode(numStruct** firstNode, numStruct* nodeToDelete) { numStruct* currNode = *firstNode; numStruct* temp; // if the list is not empty if (*firstNode) { // the first node should be deleted if (currNode == nodeToDelete) { *firstNode = (*firstNode)->Next; free(currNode); } else { while (nodeToDelete != currNode->Next && currNode->Next) { currNode = currNode->Next; } if (nodeToDelete == currNode->Next && nodeToDelete) { temp = currNode->Next; currNode->Next = temp->Next; free(temp); } } } inside the main: numStruct* t = anchorNode; int arr[50] = { 261363 }, spot = 0; printf("Please enter all the numbers you want to delete and end it with - 999 \n"); while ((arr[spot] != -999) && (spot != 50)) { scanf("%d", &arr[spot]); if (arr[spot] != -999) { spot++; } } if (arr[spot] == -999) { arr[spot] = 261363; spot--; } for (int i = 0; i < spot; i++) { int flag = 0; while ((t->number != arr[i]) && (t->number != NULL)) { t = t->Next; if (t->number == arr[i]) { flag = 1; } } if (flag == 1) { deleteNode(anchorNode, t); t = anchorNode; } } the problem is that it doesnt really delete it when i go beck to the menu and do the print function again it appears the same
There are a lot of errors and redundant code. See the following solution, study it and go on on yourself now.
void deleteNode(numStruct **firstNode, numStruct *nodeToDelete)
{
numStruct *temp = NULL;if (!\*firstNode) return; //Empty list // Special case the node we must delete is the first one if (\*firstNode == nodeToDelete) { temp = \*firstNode; \*firstNode = (\*firstNode)->Next; free(temp); return; } for (numStruct \* currNode = \*firstNode; currNode->Next; currNode = currNode->Next) { if (nodeToDelete == currNode->Next) { temp = currNode->Next; currNode->Next = (currNode->Next)->Next; free(temp); return; } }
}
void RemoveNodes(numStruct **anchorNode)
{
numStruct *t = *anchorNode;
int arr[50] = {0};
int spot = 0;printf("Please enter all the numbers you want to delete and end it with - 999 \\n"); for (spot=0; spot < 50; spot++) { fflush (stdin); scanf("%d", &arr\[spot\]); if (arr\[spot\] == -999) break; } if (spot >= 50) spot--; for (int i = 0; i < spot; i++, t = \*anchorNode) { while (t != NULL) { if (t->number == arr\[i\]) { deleteNode(anchorNode, t); break; } t = t->Next; } }
}
int main(void)
{
numStruct *anchorNode = NULL;
numStruct *newNode = NULL;
AddSongs(&anchorNode, newNode);
printList(anchorNode);RemoveNodes(&anchorNode); printList(anchorNode); system("PAUSE");
}
-
There are a lot of errors and redundant code. See the following solution, study it and go on on yourself now.
void deleteNode(numStruct **firstNode, numStruct *nodeToDelete)
{
numStruct *temp = NULL;if (!\*firstNode) return; //Empty list // Special case the node we must delete is the first one if (\*firstNode == nodeToDelete) { temp = \*firstNode; \*firstNode = (\*firstNode)->Next; free(temp); return; } for (numStruct \* currNode = \*firstNode; currNode->Next; currNode = currNode->Next) { if (nodeToDelete == currNode->Next) { temp = currNode->Next; currNode->Next = (currNode->Next)->Next; free(temp); return; } }
}
void RemoveNodes(numStruct **anchorNode)
{
numStruct *t = *anchorNode;
int arr[50] = {0};
int spot = 0;printf("Please enter all the numbers you want to delete and end it with - 999 \\n"); for (spot=0; spot < 50; spot++) { fflush (stdin); scanf("%d", &arr\[spot\]); if (arr\[spot\] == -999) break; } if (spot >= 50) spot--; for (int i = 0; i < spot; i++, t = \*anchorNode) { while (t != NULL) { if (t->number == arr\[i\]) { deleteNode(anchorNode, t); break; } t = t->Next; } }
}
int main(void)
{
numStruct *anchorNode = NULL;
numStruct *newNode = NULL;
AddSongs(&anchorNode, newNode);
printList(anchorNode);RemoveNodes(&anchorNode); printList(anchorNode); system("PAUSE");
}
thats brilliant thank you very very much!!
-
thats brilliant thank you very very much!!
-
there is one more thing that bothers me 1 more problem im having in my full program is to "delete" a part of the linked list and reconnect it to the others by an input i tried to do this next thing: void deleteNode(numStruct** firstNode, numStruct* nodeToDelete) { numStruct* currNode = *firstNode; numStruct* temp; // if the list is not empty if (*firstNode) { // the first node should be deleted if (currNode == nodeToDelete) { *firstNode = (*firstNode)->Next; free(currNode); } else { while (nodeToDelete != currNode->Next && currNode->Next) { currNode = currNode->Next; } if (nodeToDelete == currNode->Next && nodeToDelete) { temp = currNode->Next; currNode->Next = temp->Next; free(temp); } } } inside the main: numStruct* t = anchorNode; int arr[50] = { 261363 }, spot = 0; printf("Please enter all the numbers you want to delete and end it with - 999 \n"); while ((arr[spot] != -999) && (spot != 50)) { scanf("%d", &arr[spot]); if (arr[spot] != -999) { spot++; } } if (arr[spot] == -999) { arr[spot] = 261363; spot--; } for (int i = 0; i < spot; i++) { int flag = 0; while ((t->number != arr[i]) && (t->number != NULL)) { t = t->Next; if (t->number == arr[i]) { flag = 1; } } if (flag == 1) { deleteNode(anchorNode, t); t = anchorNode; } } the problem is that it doesnt really delete it when i go beck to the menu and do the print function again it appears the same
a random user wrote:
the problem is that it doesnt really delete it
So have you stepped through each line of code using the debugger? Short of doing that, asking questions and pasting others' code snippets into your project are just a waste of time.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
a random user wrote:
the problem is that it doesnt really delete it
So have you stepped through each line of code using the debugger? Short of doing that, asking questions and pasting others' code snippets into your project are just a waste of time.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
esecuse me kind sir but thats not really true after each and every answer i get i dont just copy it, i also try to udnerstand where i went wrong and try to udnerstand it , I am learning from my mistakes after all i only started with the programming in this year