|
i'd start looking here:
numStruct* deleteNode(numStruct** firstNode, numStruct* nodeToDelete)
fn takes a pointer to a pointer to a numStruct.
but here:
numStruct* t = anchorNode;
...
deleteNode(anchorNode, t);
you're just passing a pointer to a numStruct.
|
|
|
|
|
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 <stdio.h>
#include <stdlib.h>
#include <string.h>
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");
}
|
|
|
|
|
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 <stdio.h>
#include <stdlib.h>
#include <string.h>
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 (!currNode)
{
*firstNode = newNode;
newNode->Next = NULL;
}
else
{
while (currNode->Next) {
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++;
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
|
|
|
|
|
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;
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!!
|
|
|
|
|
|
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
|
|
|
|
|
Hi
I have a CAsyncSocket Class as a member of a CWinThread Class
To start the TCP/IP conversation I do a PostThreadMessage to a Worker UI thread
which has CAsyncSocket as a member and do a CAsyncSocket::connect
However When the FrameWork calls both the CasyncSocket::OnConnect and CAsynScoket::OnSend
I can tell Visual Studio Debugger -> Thread display that the thread id is that of the main thread
I am having problems doing the subsequest CAsyncSocket::Send and am guessing this more than likely the issue
Thanks
|
|
|
|
|
The
CASyncSOcket notifications OnConnect OnSend owned by main thread
|
|
|
|
|
Your question is not clear...
|
|
|
|
|
I contacted Joseph Flounder and He explained That CAsyncSocket notifications
OnSend, OnConnect are intitally received in the context of the main thread
I detach the socket do a PostThreadMessage to the Thread with the CAsynsocket derived
member do a Attach
and from that point on all notification are received in The UI CWinThread
|
|
|
|
|
#include <stdio.h>
int main(void)
{
int x=2;
printf("%d\n%d\n",x++,x++);
}
why the result is 4 4 not 3 4 ?!
|
|
|
|
|
Google for "order of evaluations." It's a much-talked-about subject.
"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
|
|
|
|
|
The order of evaluation of the two post increment operations (x++) is not guaranteed. Using gcc I get 3, 2, using clang I get 2, 3. Both are valid answers, as are would be any combination of 2, 3 and 4. In fact, it would not be a surprise if different levels of optimization produced different results.
I think the only thing that can be guaranteed is that the value of x will be 4 after the call to printf().
|
|
|
|
|
You got this result because the optimizations are on.
The compiler choose an optimization that leads to the final result not considering what the values could be in the same call. This is consistent with C99, C11 and C++11 standards that to privilege the best optimization of code state that the order of evaluation of the parameters of a function is undefined, and compiler dependent (but also situation dependent different conditions of optimization will lead to different results).
If you test your code with different compilers you'll get different results.
Please consider that even with optimizations off the compiler could give 'strange' values.
BTW the more common output you should expect should be '4 3', because the parameters should normally be evaluated from right to left (incrementing 2 to 3 and pushing it on the stack, then incrementing it to 4 and pushing it on the stack).
When optimization are on basically the compiler decides to make a double increment in one time (instead of move back to the same variable for a second increment).
|
|
|
|
|
The code produces undefined behavior - you are modifying an 'lvalue' twice between 'sequence points'.
Undefined behavior means precisely that; the compiler is at liberty to generate code that will format your disk, or even make demons fly out of your nose. Be thankful that all it did was give you an unexpected result.
If you have an important point to make, don't try to be subtle or clever. Use a pile driver. Hit the point once. Then come back and hit it again. Then hit it a third time - a tremendous whack.
--Winston Churchill
|
|
|
|
|
Hmm, I never had demons fly out of my nose - I think I need to try harder!
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
|
|
|
|
|
One of the regulars on the newsgroup comp.lang.c invented a notional system - the DeathStation 9000. The C compiler for that system would interpret undefined and implementation-defined behavior in the worst possible manner, up to and including creation of nasal demons...
If you have an important point to make, don't try to be subtle or clever. Use a pile driver. Hit the point once. Then come back and hit it again. Then hit it a third time - a tremendous whack.
--Winston Churchill
|
|
|
|